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.