Again, I didn't see any threads on this, if it has been done then let me know :P.
---
HitTest In Flash CS3
As we know, AS3 is very different to AS2, but not all things are completely complex, like the way we code hitTests now.
It's the same as always, but now they've got 2 functions/classes/w.e:
obj1.hitTestObject(ob2j) - This is the average collision between 2 objects.
obj1.hitTestPoint(x, y, shapeFlag) - This is your average advanced collison with shapeFlag parameter, useful for Mouse Avoiders and such ;).
We'll be focusing on the first one for now, hitTestObject.
How It Works
Here's a quick look at how the code works:
obj1 this is clearly the first object, instance name being obj1.
hitTestObject() This is the new code we use for a hitTest.
obj2 THIS is clearly the 2nd object, instance name being obj2.
Pretty easy huh? Now about actually using it.
What We're Doing
We're going to make a simple bouncing script between a block and the ground, the bounce being less stronger each time round, a fairly easy thing to do to begin with.
Prepare the MCs
Make a small square around 30x30, instance name blk. This is going to be a block ;).
Make a larger rectangle covering the length of the stage, place it towards the bottom of the stage, this will be the floor. Instance name flr.
Now For The Code
As you may already know, AS3 can no longer be coded in your beloved MCs, only on frames. So here's the code, broken down and explained line (or part) by line (or part):
var spd:Number = 1;
We must define any variables necessary to begin with, this is the spd variable, which states that it is a number with an initial value of 1
stage.addEventListener(Event.ENTER_FRAME, blkfall);
This is how we setup onEnterFrame's and such now, the EventListeners.
stage is where we ADD the listener.
We then specify what type of event, in this case Event.ENTER_FRAME, which will make the listener check the function constantly.
Then we specify the function name, which in this case is blkfall.
You can just use an ENTER_FRAME listener for a function with all the onEnterFrame code inside it, you don't have to make seperate functions for the different things you're going to code, it just makes it more organized in this case.
function blkfall(Event) {
Here we setup the blkfall function, note that the (Event) is necessary.
blk.y += spd;
This makes the block fall, since it's in an ENTER_FRAME function, it'll constantly happen.
spd++;
This increases the value of spd by 1, which is how fast the blk will fall, making it fall faster the longer its in the air.
if (blk.hitTestObject(flr)) {
Here's where we do the hitTest, between the blk instance and the flr instance, a very basic hitTest.
spd = -spd/1.2;
If the hitTest code is found true, then the spd variable will revert itself, causing the block to go up instead, but the /1.2 makes it go up a little less, so that it doesn't go in a loop. And because spd is constantly adding 1 on, it'll slow down and then go down.
}
This ends the hitTestObject condition.
}
This ends the function.
---
And if you test that, you should get something like This.
Hope this helps :P.