ForumsProgramming ForumFollow Path

4 3248
LennonTheMage
offline
LennonTheMage
55 posts
Nomad

Hello,
I'm trying to make a code in actionscript 2.0 "where when a button is clicked a movieclip appears at an area, then follows the line(path)"and then once it hits an object it will then go back to the frame where you don't see it and it doesn't move till the button is clicked.

I know how to make everything after the quotes. I appreciate any help, thank you.

  • 4 Replies
PrideRage
offline
PrideRage
148 posts
Nomad

Well. First, you have to draw the path. Then you have to set 2 coordinates, the first one is the beginning, the second one is the end.
And then you have to make a while loop, which checks if the button is still pressed. And if it is, you have to increase/decrease the x or y position of your movieclip, depending on where it should move to. But befor moving, you need to check if the 2 coordinate is reached.
Unfortunately, i don't know how to check if a button is still pressed or not. But you should know now how to do this.

driejen
offline
driejen
486 posts
Nomad

separate the task into different parts:
1)clicking a button spawns moving object
2)moving object moves and checks collision with another object, clicking the button does nothing while the object is active
3)when collision occurs, the moving object is removed and the button can be clicked again

start by adding

stop();
to the main timeline to stop it from refreshing the frame or moving to the next frame.

1) make sure you create the moving object dynamically so that you can remove it later, give the moving object a suitable library name, and the button a suitable instance name, in this example the moving object has a library name "Object1" and the button has an instance name "startButton". then add this code to your main timeline:
[/quote]startButton.onRelease=function(){
_root.attachMovie("Object1","movingObject",1)
movingObject._x=50
movingObject._y=50
movingObject.onEnterFrame=function(){
moveObject()
}
}


now clicking on startButton will spawn a copy of Object1 from the library and give it an instance name movingObject. movingObject's x and y coordinates are then set (you can change the numbers to position where the movingObject should appear). movingObject will call the function moveObject() each time in enters the frame.

2)clicking the startButton has spawned the movingObject and now you need to make it move and check for collision. Give the obstacle a suitable instance name, "Obstacle" in this example. add this code to your main timeline:
function moveObject(){
movingObject._x++
if(movingObject.hitTest(Obstacle)){
movingObject.removeMovieClip()
}
}

So each frame, moveObject() is called upon and movingObject moves to the right by 1 pixel(you can change this value or make it move in different directions) and also deletes the movingObject when it hits the Obstacle. But you arent done, if you click the button multiple times now, some funky stuff might happen because you are trying to make several movingObject, you want to stop the button from attempting to create a moving object while there is already one on stage. So far your code should look like this:
stop();

startButton.onRelease=function(){
_root.attachMovie("Object1","movingObject",1)
movingObject._x=50
movingObject._y=50
movingObject.onEnterFrame=function(){
moveObject()
}
}

function moveObject(){
movingObject._x++
if(movingObject.hitTest(Obstacle)){
movingObject.removeMovieClip()
}
}


You need to add a variable that determines wether your button should function or not, so change your code to this:
stop();
clickable=1

startButton.onRelease=function(){
if(clickable==1){
clickable=0
_root.attachMovie("Object1","movingObject",1)
movingObject._x=50
movingObject._y=50
movingObject.onEnterFrame=function(){
moveObject()
}
}
}

function moveObject(){
movingObject._x++
if(movingObject.hitTest(Obstacle)){
movingObject.removeMovieClip()
clickable=1
}
}[quote]

A variable has been added, clickable, which turns to 0 when a movingObject is created and stops subsequent clicks from doing anything. When the movingObject hits Obstacle, the movingObject disappears and clickable turns to 1 and makes the button functional again.

Hope that helps.
LennonTheMage
offline
LennonTheMage
55 posts
Nomad

@Driejen

When the button is clicked it isn't appearing, this is the code so far.


startButton.onRelease=function(){
_root.attachMovie(&quotinkBunny","PinkBunny",1)
PinkBunny._x=363
PinkBunny._y=297
PinkBunny.onEnterFrame=function(){
PinkBunny()
}
}
function moveObject(){
PinkBunny._x++
if(PinkBunny.hitTest(bluebase)){
PinkBunny.removeMovieClip()
}
}
driejen
offline
driejen
486 posts
Nomad

Go to your library and on properties for your &quotinkBunny" symbol, tick 'Export for ActionScript'. Also change the function from moveObject to PinkBunny because that is the function you are calling each frame now, otherwise your spawns PinkBunny won't move.

startButton.onRelease=function(){
_root.attachMovie(&quotinkBunny","PinkBunny",1)
PinkBunny._x=363
PinkBunny._y=297
PinkBunny.onEnterFrame=function(){
PinkBunny()
}
}
function PinkBunny(){
PinkBunny._x++
if(PinkBunny.hitTest(bluebase)){
PinkBunny.removeMovieClip()
}
}
Showing 1-4 of 4