Next we are going to create two different movie clips. We will
see how to detect when one of the clips actually collides with
the other movie clip. This can be great for Flash interactive
games and some cool navigation. For this example we are going to
have one of the clips remain stationary while the other one is
going to be moving around using the startDrag
function. For this example we are going have an instance name of
"good_guy" on the stationary clip and
"bad_guy" on the movie clip that will ultimately
collide with our good one.
On the very first frame of the movie we are going to add some
ActionScript to allow the bad_guy movie clip to be
dragged by the user around the screen. To do this we simply add
startDrag ("_root.bad_guy", true); in the
very first frame of the movie. The true variable
tells Flash to lock the mouse to the center of the movie clip. In
the normal mode you will be able to see the option at the bottom
of the ActionScript panel. If you want to test the movie at this
point go ahead and you will see that you can how move the object
around with the mouse pointer. In the bad guy clip we want to put
in a "stop" action in the first frame of the clip so that once it
starts it won't advance past the beginning. Then in the second
frame of the movie we will create another view of the image that
looks as though it exploded so that when it does collide with the
good guy clip it will look different.
Next we want to determine when the actual collision occurs. When
you create something using the collision detect you will need to
decide which clip is going to detect the other clip. For this
example we are going to have the bad guy doing the detection. So,
edit the bad_guy movie clip and add an extra frame.
This is done so that it will loop and constantly check for
collision. In the first frame of the clip enter the following
code:
flag = this.hitTest("/good_guy");
if (flag == true) { tellTarget ("/") { gotoAndStop
("hit"); } }
[The lines above are one continuous line. It has been split
for formatting purposes.]
This is done by selecting setVariable from the
Actions menu. Then in the Variable section, enter:
flag Next, in the Value section, enter:
this.hitTest("/good_guy") Then enter the
following code: