HitTest. (On action commands)
Before beginning this tutorial. Please change your Flash publish settings to Action Script 2 if you're using CS3. If you have a lower version. Your settings will be default and you cant skip this.
To do this:
File > Publish Settings > "Flash" > ActionScript version: ActionScript 2.0.
Now let's begin.
If you do not know how to make an object move.
Please follow this tutorial. I will use the movement tutorial in relation to this tutorial.
Step 1:
- Click on your movie clip and in the "Properties" box at the bottom of the screen (CTRL + F3)
you should see a box saying: <Instance Name>. This is like, naming your pet. When you call
the name, you want your pet to do something.

lol.
Name your movieclip anything you like, but remember it, and remember it's case sensitive, also
do not use words like "scale" or "rotation" if you do, stick something infront of it like "my-scale",
this is so that when you address it in coding, it doesn't think it's a property of scale for size
or rotationg to turn it. The most common and well known name is "
mc". Main Character. So for this
tutorial I am going to refer it to
mc. Type mc in the box.
Step 2:
- Make a new layer (it's recommended to make new layers for each unique item. Like "Backgrounds" "Guards"
etc, this will keep it very clean, tidy and easy to navigate. Do this by clicking the box with a smaller
box icon, (furthest to the left) under the layer pallet. Or right click "Layer 1" and click "Insert Layer".
You will now have Layer 2 and it should be highlighted. Make a box somewhere on your stage. and Colour it
blue or whatever. Doesn't matter. (You can make a whole object like a wall/door/enemy).
Step 3:
- Right click your new object and make it a movieclip by converting it's symbol. Click your new movieclip
and press F9 (rightclick > actions) to bring up the ActionScript window.
Now this isn't going to be alike to the movement with "on". This time we want to define it
as a specific
clip event and then some form of event. We do this like so:
Code:
onClipEvent () {
}
So now it waits for an event that you specify to triger. Then it will do the "
do actions"
part. Make sure you have the case correct. Capital C and E. Now we need it to load this command as soon as the
frame enters. So in () type
enterFrame like so:
Code:
onClipEvent (enterFrame) {
}
Now it will instantly run the commands we tell it to do, when the timeline appears on the frame the clip is.
which will be frame1.
Step 4: - Now to tell it what to do. We want it to detect a collition which is known as a "hitTest". This code
is quite long, but I will explain it bit by bit. Place in this code:
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.mc)) {
}
}
The
if part is like saying "if so and so happens, then . . ." Then we need event, which goes
in brackets
() so we say
(this.hitTest) which is same as saying (i-get.hit).
Then, we need to tell it what will hit it, so we add in another event
(_root.mc). _root brings
it back to the first stage. (If you double click your movie clip, you will go into your movie clip. This would be
'yourclip' or whatever you named your movieclip. Then if you go back to Scene1 it would be '_root.yourclip'.) It's
always good to just stick in _root.whatever to make sure it goes to where you want it.
The we need it to do an action when mc hits "this". So we open up another "do action" tag with
{ }Step 5: - In this example, I'm just going to make it go to frame2. Click on frame 2 in any of the layers, right click then
press "
Insert Keyframe" (F6.) This will make a new frame.
Within that frame type something like "Frame2." Anywhere, doesn't matter.
Now go back to frame1 and back onto your actions, and place in the this code:
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.mc)) {
_root.gotoAndStop(2);
}
}
Now we're telling the code to go to
_root. scene position, then
gotoAndStop on frame:
(2);If you press
CTRL + Enter. move your mc over object and you should fire of to frame2!
And that is that. If your timeline shoots back to frame1 again. Click on Frame2, press F9 (rightclick > Actions)
and type in:
Code:
stop();
This will make it stop on frame2.
- - -
Heres a bit more advanced hitTest with more commands changing multiple things at once.
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.me)) {
_root.score += 10;
_root.score10._x = 100;
_root.score10._y = 350;
this.unloadMovie();
}
}
What this does is, When the movieclip "me" hits this movieclip, it will add on
10 score onto the variable "score" which i set on another frame. Then it will
move the movieclip "score10" to a specific x and y location, then unload itself
so you cannot gain more than 10 points and can't be hit again.
Enjoy and look out for more on Game Dev in flash.