00:00
00:00

AS3: HitTesting

8,711 Views | 25 Replies
New Topic Respond to this Topic

AS3: HitTesting 2007-05-03 14:12:34


AS3:Main

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.

Response to AS3: HitTesting 2007-05-03 16:56:26


Wow AS3 is totally different than AS2. I have Flash 8 and i would love a better version but AS3 is totally different.

Well almost.

Response to AS3: HitTesting 2007-05-03 17:06:13


does all these AS3 threads mean AS2 will become opsalete?

Response to AS3: HitTesting 2007-05-03 17:37:01


At 5/3/07 05:06 PM, Ertyguy wrote: does all these AS3 threads mean AS2 will become opsalete?

no, a lot of people will still have flash 8 so they will use as main for as2


-

BBS Signature

Response to AS3: HitTesting 2007-05-03 17:39:15


At 5/3/07 05:06 PM, Ertyguy wrote: does all these AS3 threads mean AS2 will become opsalete?

To my knowledge, you will still be able to use AS2 for many years to come. I think in CS3, you can opt to use AS2, just like you can use AS1 in Flash 8 if you so choose.

Also, nice tut. Ima stick with AS2 though 8-)


...

BBS Signature

Response to AS3: HitTesting 2007-05-03 20:00:05


I dont know wats the big deal, learn as2 well and u can learn as3 in a day. Then just practice and find best methods in the new lanuage. The syntax is still obviouse, so you can kinda guess wat to do. For example, the first time i wanted to do a hitTest, i went through the refernce, looked for hitTest, and then discovered there where 3 hitTest methods, i read up on all of them, easy. You dont need a tutorial really expliaing all this stuff again. The people learning AS3, imo, are the better coders anyway. If this is actually helping sum1, chances are its being wasted anyway, coz they dont know coding enough.

Response to AS3: HitTesting 2007-05-03 20:23:24


From the looks of this tutorial, AS3 doesn't seem that much different from AS2, just more proper in my opinion. If somebody now would want to go from AS3 to a different programming language then they should understand much better. Being able to write code on movie clips kind of spoiled people. Btw xWELSHxDRAGONx, what is the 3rd hitTest method?

Tutorial seems fine (very easy to read/organized), but you didn't show an example for the advanced collision. Plus the example you showed, despite probably being 12 fps, also has problems with it (including logic problems).


AS2 Main | AS3 Main | Flash Wiki There is no excuse for not trying to learn. Linux.

BBS Signature

Response to AS3: HitTesting 2007-05-03 20:37:32


At 5/3/07 04:56 PM, Frozen-Fire wrote: Wow AS3 is totally different than AS2.

This doesn't really demonstrate a striking difference, couldn't you do it like that with AS2 also?

And what was the point of splitting one function that did two things into two functions that do two things?!?!


BBS Signature

Response to AS3: HitTesting 2007-05-03 20:53:06


At 5/3/07 08:37 PM, Coaly wrote: And what was the point of splitting one function that did two things into two functions that do two things?!?!

more organized and neat? maybe slightly more efficient (doesn't have to check which test to perform)? Personally I think that checking if a bounding box of one object is colliding with a binding box of another is very different from checking if a pixel location is a part of an object.


AS2 Main | AS3 Main | Flash Wiki There is no excuse for not trying to learn. Linux.

BBS Signature

Response to AS3: HitTesting 2007-05-04 00:36:08


At 5/3/07 05:06 PM, Ertyguy wrote: does all these AS3 threads mean AS2 will become opsalete?

Nah. there're a select few here who're trying to learn the language for 2 reasons:

~ To help others learn it.
~ To get the hang of programming games with it.

Fro what I've seen, this language isn't gonna be any harder to learn than AS2, the changes are still easy, just Adobe likes to use longer words.

I'm sticking with Flash 8 for now, but I'm learning AS3 in my spare time.

Response to AS3: HitTesting 2007-05-04 00:47:19


i no dubble post lawl

@xWELSHxDRAGONx: Well, I've learnt AS2, it's been 2 days since trying and I still haven't learnt AS 3 ;).

If you look at the cheat sheet you'll find some things ARE different and will take a short while to get used to

As for the code, I've adjusted it, using the hitTestPoint and a while condition:

var spd:Number = 1;
stage.addEventListener(Event.ENTER_FRAME, blkfall);
function blkfall(Event) {
blk.y += spd;
spd++;
if (floor.hitTestPoint(blk.x, blk.y, true)) {
spd = -spd/1.2;
while (blk.hitTestObject(floor)) {
blk.y--;
}
}
}

Something to note: while conditions only work in an if condition, otherwise they'll probably cause flash to abort the script or just give you a compile error.

New Example

I'm pretty surprised that 64 fps runs faster online that it does off, that never happened in flash 8 :o.

Response to AS3: HitTesting 2007-05-04 01:32:54


Well, I just changed it so that the ball does stop (by using if statement to see if speed is a small enough number), put the 'spd++' before the addition to y (which got rid of abnormal bounce height keeping constant), and added a "blk.y++" after the while loop to make it land on the floor and not 1 pixel higher.

Btw using the hitTest assumes that registration point is on the bottom of the bouncing square.

var spd:Number = 1;
stage.addEventListener(Event.ENTER_FRAME, blkfall);
function blkfall(Event) {
spd++;
blk.y += spd;
if (floor.hitTestPoint(blk.x, blk.y, true)) {
if(spd < 1.5 && spd > -1.5)spd = 0;
else spd = -spd/1.2;
while (blk.hitTestObject(floor)) {
blk.y--;
} blk.y++;
}
}

AS2 Main | AS3 Main | Flash Wiki There is no excuse for not trying to learn. Linux.

BBS Signature

Response to AS3: HitTesting 2007-05-28 00:10:59


I fucking hate AS3.....


AS: Main || AS3: Main || <-- Useful links.

Response to AS3: HitTesting 2007-09-30 13:25:52


Good tut but i reckon you should turn up the framerate


lol

Response to AS3: HitTesting 2008-03-09 09:56:17


At 5/3/07 04:56 PM, Frozen-Fire wrote: Wow AS3 is totally different than AS2. I have Flash 8 and i would love a better version but AS3 is totally different.

Well almost.

its not better its different. learning as3 sucks... im so glad its not the standard yet.

Response to AS3: HitTesting 2008-12-09 07:36:48


At 5/3/07 08:00 PM, xWELSHxDRAGONx wrote: I dont know wats the big deal, learn as2 well and u can learn as3 in a day.

This is true of many languages.
Once you have learned one or 2 languages, you can traverse languages with relative ease.
I started tremulously poking the aggressive rhino I perceived AS3 to be yesterday.
Now I am pretty much as confident in AS3 as I am in AS2.
Pretty much. -_-

Response to AS3: HitTesting 2009-04-05 23:56:42


Hi there. This thread may be dead now for a few months but its in the AS3:Main so i dont see a problem reviving it.

The thing is, that this code doesnt properly work. >_<

The bounce gets balanced at a specific heigh and goes into a bounce loop.

I think that its because you simply always devide the spd variable by 1.2, while you add 1, in each frame.
Because of that, as an example:
Lets say spd is 2.4. It gets devided by 1.2 thus gets 2. It gets added 1 and the final spd is 3, while we initially had 2.4, so it increased (by 0.6). (Thats because when you devide 2.4 by 1.2, it only decreases by 0.4, in case you couldnt figure yet.)

Following that logic, i guess that top spd (the spd reached exactly on the ground hit) gets balanced at 6.
Because if you devide 6 by 1.2 its 5, then add 1 and its 6 again.

Another reason added onto all this could be this universal (in all prog. languages) problem with floating point numbers. Dont really know but what i'll describe now ((an)other problem(s)) seems rather because of this:

I see my little player block go "into" the ground a bit when it hits the ground. (I doubt its just an illusion. =P Doesnt happen on every bounce.)
And also, before top spd gets balanced at 6 and the object goes into a bounce loop, there are some very small and rapid "bounces" and first after that it increases again (up to 6, in this case i guess).
I dont know if these issues are tied together.
Also, i tried to play around with those registration point or transformation point things (the crosshair and the circle, did i call them correctly?) and it doesnt seem to change anything.

This "going into the other object" problem or these rapid bounces bug doesnt seem to have anything to do with the problem caused by "division and addition of static numbers" (aka "what if the devision decraeses less than the increase?" =P).
Otherwise it would simply increase a bit again after going below 6 (and above 5) from say 6.5 and not do these strange bounces.
Another reason i think this is tied to something else - than the first problem i described - is, that i experienced these rapid bounces "bug" happening sometimes while it could normally bounce a lot more (in other words, while (top) spd/1.2 was still clearly above 6) and not always after a bounce where top spd was probably somewhere just slightly above 6 (so it would decrease below 6 (and above 5)).
(Remember with "top spd" i mean the spd it reaches exactly on the ground hit.)

Actually the "AS3: Basic Platform" tutorial DOES have a correct code, so i dont have any problems i want you to help me with, but i just thought i should say this in case some newbs (like me) come here and wonder why it doesnt work.

It would be very very nice if someone could bring some clarification on this so we newbs (ok, noobs >_>) get a better idea on how exactly all this works.

And just in case, heres my .fla file: http://spamtheweb.com/ul/upload/060409/2 4788_Platform.fla
Note that there are also already boolean variables and such for movement keys and those arent completed yet. Im working on the movement keys but this problem is irrevelant from them so nevermind those variables and functions. The actual part about all this starts at line number 23 of the code.

Regards, and thanks for any help.

Response to AS3: HitTesting 2009-04-06 11:22:56


Oh come on someone please help. =P
Though ok i think ive solved some things.
(LOL it feels like im talking to myself here, or use this board as a way to write down my own thoughts...)
I think i shouldve done more experimenting/thinking first.
WHATEVER

Now the actual things i got to say (i was supposed to write short >_>):

I assumed that, as an example:
The spd value would increase from 2.4 to 3 after the hit test because it gets devided by 1.2 (is 2 now) and added 1 (is 3 now).
And i also assumed it would balance at 6 because of the same reason (6/1.2=5 and 5+1=6 again).

Thats WRONG, because it gets devided by negative 1.2 (and not positive 1.2).

Important question:
But what if -spd/1.2 gets something in between -1 and 0 in which case spd would stay positive after adding 1?

I would now assume that thats the reason my player block sinks into the ground, because after all spd stays positive thus the block keeps going down, into the ground.

BUT im experiencing the same problem (sinking into ground after hit) even on very high drops, where spd would have increased clearly enaugh for its negative devided by 1.2 not to be in between -1 and 0.

Ultimate question =P :

WHY THE HELL DOES MY DROPPING BLOCK SINK INTO THE GROUND AFTER THE GROUND HIT?!?

The following is the code im currently using.
The variable "spd" that was mentioned in the tutorial (and i talked about) is "$grav" in here.
$objPlayer, $objGround etc. should be pretty self explanatory.

var $grav:Number=1;
stage.addEventListener("enterFrame", $fall);
function $fall(Event) {
	$objPlayer.y+=$grav;
	$grav++;
	if ($objPlayer.hitTestObject($objGround)) {
		$grav=- $grav/1.2;
		if ($grav<=6 && $grav>=-6) {
			$grav=0;
		}
	}
}

Thanks.

Response to AS3: HitTesting 2009-04-06 14:39:56


Dude, first of all flash isnt php, the $ sign looks really nasty.
Second, Screw hittest and learn how to program instead.

Third: Stop posting that "grav++" bullshit, GRAVITY IS A CONSTANT! 9.81 m/s^2 !!!!

-> position = x
-> dx / dt = v change of position over a time interval is velocity
-> dv / dt = a change of velocity over a time interval is acceleration
-> F = m * a => a = F / m

Hence (if i am not wrong):

x' = x0 + ( ( v + ( ((F / m) + g) * dt )) * dt )

with
dt the time step
x' as the new position vector (meters)
v as the velocity vector (meters / seconds)
F = the force on an object (Newton)
m = the mass of an object (kg)
g = gravity = 9.81 m/s^2

Just as a short physics recap.

Response to AS3: HitTesting 2009-04-06 21:55:51


I thought you can use $ sign in front of something without having to worry about your variable/function name being a keyword of AS3 already??? (Because no set keyword starts with $ and its accepted in names...)

Whatever.

So you say i should teach the program real physics? WTF? =P
I dont really get how serious you are with what you say.

Response to AS3: HitTesting 2009-04-07 00:46:41


Sorry for double post but i guess im better off creating a new thread on all my problems LOL.
The codes in the Basic Platformer tutorial didnt fix my problems %100 either. X(
(I wonder what silly mistakes i made...)

Response to AS3: HitTesting 2009-04-07 03:43:38


I just can't stand the php $ notation thats all.

You don't need to program real physics but at least you could let gravity be a constant and add it to the velocity, which you add to the characters position. That way you can also easily change the gravity at runtime and for example enable 0 gravity, reversed gravity ect.

Short:
initialisation:

gravity = 0.981;
velocityX = 0;
velocityY = 0;

then in the update function:

velociyY += gravity;
player._y += velocityY;

It really hurts seeing "grav++" and the like if you know physics :P
Also you could use the concept of acceleration and force to give the player momentum, combined with friction you will get a much cooler movement in a platformer for example.

Response to AS3: HitTesting 2009-04-07 10:55:07


Oh thats pretty awesome actually. Thanks.

And dont worry my physics isnt that bad. =P
(Actually a field im pretty damn good in. I just love logic&knowledge. Science FTW. XD)

I will definitely keep those tips you gave in mind.

After all i think i just need more experience and nothing else...
Seemingly there are just so many ways to create a physics engine (dunno if this really counts as one though, LOL).
I shouldnt limit myself to a beginner tutorial after all.
Im sure that the creator of Fancy Pants used quite some different techniques for his physics engine. XP

Oh and, as this is a tutorial thread, i guess its better we carry further discussion into this thread here: http://www.newgrounds.com/bbs/topic/1047 066

Response to AS3: HitTesting 2010-08-18 19:53:01


To make this simple, yo can add

if (spd <= 0.8){
		spd = 0;
	}
}

to your blkfall function so this way it doesn't bounce all the time, leaving your code like this:

var spd:Number = 1;
spd.toString();
addEventListener(Event.ENTER_FRAME, blkfall);

function blkfall(Event) {
	valuespd.text = String(spd);
	blk.y += spd;
	spd++;
	if (blk.hitTestObject(flr)) {
		spd = -spd/1.5;
	}
	if (spd <= 0.8){
		spd = 0;
	}
}

I changed the 1.2 with a 1.5 because of previous tests I did, but the result is the same.


..::: AS3: Little Essay :::....:::AS3: Programming Sex:::..

||Nosejobs only result in bigger noses.||

Response to AS3: HitTesting 2010-08-18 22:10:20


At 4/7/09 10:55 AM, Kenzai93 wrote: Im sure that the creator of Fancy Pants used quite some different techniques for his physics engine. XP

Brad Borne already stated that all of the physics in his games are completely fake.

Response to AS3: HitTesting 2011-07-10 02:11:10


I don't know if any one said this but if you but the falling half of the funtion on the bottom, it stops the bouncing like so

function GravFall(Event) {
if(Player.hitTestObject(Floor)){
gravity=0
}
Player.y+=gravity;
gravity++;
}

you also wont need to divide it anymore if you just make it equal to zero( not i used the variables gravity, and player instead of flr and blk, and I changed the function name, this was just for my personal tastes). other than that great tutorial thanks(still learn as3 found that out by fiddling).