00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3: Collision Detection

6,176 Views | 7 Replies
New Topic Respond to this Topic

AS3: Collision Detection 2008-03-04 22:21:22


AS3: Main

Collision Detection:

What is it?: Collision Detection can be useful in many ways. When an object is touching another object, flash tells you.

Start:

First, draw two circles. one named circle1 and one named circle2. Next, make two dynamic text boxes, and name them messageTEXT1 and messageTEXT2

Let's create a simple example that examines the cursor location and the location of a moving object every frame.

addEventListener(Event.ENTER_FRAME, checkCollision);

The checkCollision function starts by using hitTestPoint looking at the cursor location to whether it hits the circle movie clip on the stage. The first two parameters of the HitTestPoint function are the X andY location point on the stage. The 3rd location is the type of boundary to use. The default value of false, which means only the bounding rectangle of the display object should be taken into account.

Unless the object is square shaped, this wont be good for gaming. Instead, by stting a 3rd parameter to true hittestpoint will use the actual shape of the display object.

We'll put different text into a message field, depending on the results of the hittestpoint:
function checkCollision(event:EVENT) {

//check the cursor location against the object
if (circle.hitTestPoint(mouseX, mouseY, true)) {
messageText1.text = "hitTestPoint: YES";
} else {
messageText1.text = "hitTestPoint; NO";
}

The hittestobject function doesn't have a shape option. It only compares the two bounding boxes of two sprites. But, it can still be useful in some cases.

The following part the code makes a circle2 movie clip follow the cursor, and puts a different message in another text field depending on whether their bounding boxes intersect.

// Move circle 2 with mouse
circle2.x = mouseX;
circle2.y = mouseY;

// test circle2 versus circle1
if (circle2.hittestObject(circle1)) {
messageText2.text = "hitTestObject: YES";
} else {
messageText2.text = "hitTestObject: NO";
}
}

I hope this has helped!

-MSMstudios


Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

BBS Signature

Response to AS3: Collision Detection 2008-03-04 22:37:33


example of what we're making would be nice


sig'd

Response to AS3: Collision Detection 2008-03-04 22:56:37


At 3/4/08 10:37 PM, chickendance333 wrote: example of what we're making would be nice

What? If you can't tell what he's talking about from the topic and the few words between the AS I think you should go back to elementary school..


Ghost/Supernat. Club | Join the Family | The FlashBase.

You always need at least one asshole moderator for your forum. Its not opinion, its fact

Response to AS3: Collision Detection 2008-03-04 23:02:22


At 3/4/08 10:56 PM, Mercenary81 wrote:
At 3/4/08 10:37 PM, chickendance333 wrote: example of what we're making would be nice
What? If you can't tell what he's talking about from the topic and the few words between the AS I think you should go back to elementary school..

I know what he's talking about, I just want to make sure that the engine is good beforehand


sig'd

Response to AS3: Collision Detection 2008-03-05 07:51:00


most people know what it does.


Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

BBS Signature

Response to AS3: Collision Detection 2008-03-05 08:18:26



BBS Signature

Response to AS3: Collision Detection 2008-03-05 19:41:42


I dont think thats collision "detection".


Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

BBS Signature

Response to AS3: Collision Detection 2008-03-05 19:46:08


At 3/5/08 07:41 PM, pivot11 wrote: I dont think thats collision "detection".

That's EXACTLY what collision "detection" is. Detection is just checking to see if it's hit and that's exactly what any form of hitTesting is about. Jindo wins, no offense. You should've checked to see if it had been done. Also, this tutorial should have gone much more in depth [otherwise write "- Basic" at the thread title].


MY E-PENIS IS BIGGER THAN YOURS

8=================================>

...and this is my fag...

BBS Signature