00:00
00:00
Newgrounds Background Image Theme

SpeakyDooman 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!

AS: Collision Detection

18,908 Views | 57 Replies
New Topic Respond to this Topic

Response to AS: Collision Detection 2005-08-28 17:15:22


At 8/28/05 04:05 PM, BleeBlap wrote:
At 8/25/05 10:54 AM, Glaiel_Gamer wrote: for the double bounding box check, why not make a version that loops through every 10th pixel? It wiuld still be accurate but it would also reduce the number of loops greaty (25 vs. 5000)
Because that would be too practical.

PPFFFFTTTTTT yeah good reason.

lol you just didn't think of it. Admit it.

Response to AS: Collision Detection 2005-08-28 17:19:58


oh i have a question. I make a enime and hes a mc and i double click and go inside and i make him walk in some places and then thats it i go back to main and put this script:

if(this.hitTest(that)){
gotoAndPlay();
}

------
and in the () i put the frame to go if the character dies. So is that right?

and whats with the: hitTest(that))

it has: that. what is that. DO i just keep it there. Or what?

Response to AS: Collision Detection 2005-08-29 15:17:15


12-24+ hours past...

Oh nvm for the thing because some ones teaching me and they are just giveing me links. etc.. thanks anyway and there not exactly teaching me but im asking.

Response to AS: Collision Detection 2005-09-11 09:59:32


Optimising on Collision Detection

Not seen anyone make this point yet so thought I would.
Not going to be of use all the time but will save a litle on unnecessary loops running constantly.

Say you are making a scrolling space shooter such as Project monochrome or Raiden X.

You have many enemies on screen at a time and need to detect if your ship or its bullets are making contact with each one.

This could mean you run a for loop every frame on each bullet, and the player ship
e.g
onClip(enterFrame){
for(var i:Number = 0;i < enemyNo;i++){
if (_root["enemy" + i].hitTest (this._x,this._y,true)){
//actions;
}
}
}

Instead of looping to check whether 10's of mcs are colliding wih eachother every frame, you can add all your "enemy"+i mc's into one single parent mc called "enemyParent".

This means each of your bullets, obstacles or player ship hit points can be tested against this single mc every framebefore looping through it to see which exact child mc it is touching , instead of being looped to check every frame by default.

e.g
onClipEvent(enterFrame){
if(_root.enemyParent_mc.hitTest(this._x,th
is._y,true)){
for (var i:Number = 0;i < enemyNumber;i++ ){
if(_root.enemyParent_mc["enemy" + i].hitTest(this._x,this._y,true)){
//actions;
}
}
}
}
This checks for detection with enemyParent_mc every frame, if collision evaluated to true, a loop is then run to check if each child mc of enemyParent_mc is collising with the _x _y co ordinates of your object. As the loop isnt run every frame, just when hitTest with the parent is true, processing power is saved.

Add your destruction/whatever actions in.

This is just the basic concept, you may have to mess with localToGlobal/globalToLocal to get hitTests to work on objects on different timelines depending on your game layout.

You may also use a for in loop to loop through each object in enemyParent_mc ,though I have yet to experiment with this technique.

AS:Loops
AS:Main
AS:For...in Loops
AS:Performance & Optimisation

Response to AS: Collision Detection 2005-09-28 06:56:32


bleeblap thx man i know this is a long ago topic but thx u really helped me

Response to AS: Collision Detection 2005-10-06 13:36:04


onClipEvent (enterFrame) {
if (hitTest(_root.finish)) {
gotoAndPlay(2);
}
}


Sup, bitches :)

BBS Signature

Response to AS: Collision Detection 2005-10-28 18:24:08


I think my flash is retarted(or maybe im retarted?). i have MX and i put in this code to the wall boject(i want the character to die when he hits the wall.)

onClipEvent (load) {
if (_root.Player.hitTest(_root.wall)) {
(_root.Symbol 1.gotoAndPlay (18);}

18 is the frame the character dies on.
it gives me this error.

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: ')' expected
if (_root.Symbol 1.hitTest(_root.wall)) {

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: ')' or ',' expected
(_root.Symbol 1.gotoAndPlay (18);}

Total ActionScript Errors: 2 Reported Errors: 2


///////////////////////////////////

///////////////////////////////////

\\\\\\\\\\\[magic]\\\\\\\\\\\

BBS Signature

Response to AS: Collision Detection 2005-10-28 19:00:57


At 10/28/05 06:24 PM, Droneavp23 wrote: I think my flash is retarted(or maybe im retarted?). i have MX and i put in this code to the wall boject(i want the character to die when he hits the wall.)

onClipEvent (load) {
if (_root.Player.hitTest(_root.wall)) {
(_root.Symbol 1.gotoAndPlay (18);}

18 is the frame the character dies on.
it gives me this error.

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: ')' expected
if (_root.Symbol 1.hitTest(_root.wall)) {

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: ')' or ',' expected
(_root.Symbol 1.gotoAndPlay (18);}

Total ActionScript Errors: 2 Reported Errors: 2

Two things, first of all you will want to change the first line to:

onClipEvent (enterFrame) {

if you are using it in a game and the second thing is that instance names can't have a space in them so change it to Symbol1 or something else that starts with a letter and is only letters and numbers and it should work.

Response to AS: Collision Detection 2005-10-28 20:14:29


now i have this error

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: ')' expected
(_root.Symbol1.gotoAndPlay (18);}}

Total ActionScript Errors: 1 Reported Errors: 1

is it saying that i need to put in a ")"???


///////////////////////////////////

///////////////////////////////////

\\\\\\\\\\\[magic]\\\\\\\\\\\

BBS Signature

Response to AS: Collision Detection 2005-10-28 20:16:24


ahh i am retarted i forgot to put a ) before the } lol


///////////////////////////////////

///////////////////////////////////

\\\\\\\\\\\[magic]\\\\\\\\\\\

BBS Signature

Response to AS: Collision Detection 2005-10-28 20:22:49


At 9/11/05 09:59 AM, T-H wrote: such as Project monochrome

well all I did was limit it to 20 bullets at a time. no funky junk. It still ran fine.

Response to AS: Collision Detection 2005-11-02 01:56:46


I wonder if there's any way to hit test with objects that are not rectangular, that is if I have two triangles which are moving towards eachother, you could tell when they really collide, not just their rectangular borders. Probably there isn't...

Response to AS: Collision Detection 2005-12-17 05:44:29


At 8/25/05 10:54 AM, Glaiel_Gamer wrote: for the double bounding box check, why not make a version that loops through every 10th pixel? It wiuld still be accurate but it would also reduce the number of loops greaty (25 vs. 5000)

how could you go about doing this?
plz tell...


I have done the deed. Didst thou not hear a noise?

BBS Signature

Response to AS: Collision Detection 2005-12-17 06:48:15


ok i did it
sory for asking
i just jumped for loop by 10 insted of one
,i>x+= 10
y+= 10,/i>
nice tut


I have done the deed. Didst thou not hear a noise?

BBS Signature

Response to AS: Collision Detection 2005-12-17 09:49:25


At 9/11/05 09:59 AM, T-H wrote: Optimizing collision detection

I'm doing that in my christmas collab minigame =)


BBS Signature

Response to AS: Collision Detection 2005-12-19 12:37:34


At 11/2/05 01:56 AM, triumpth wrote: I wonder if there's any way to hit test with objects that are not rectangular, that is if I have two triangles which are moving towards eachother, you could tell when they really collide, not just their rectangular borders. Probably there isn't...

Thats where things start to get fun! It is possible as seen in commercial games released in the last decade but it involves some advanced mathmatics. If you know that you are using unrotated equalateral triangles you could do triangle collisions with just a little trig (or any regular unrotated n-gon for that matter) but to incorporate all shapes you need to use vectors. I'm currently working on an engine that uses this stuff so if I ever get it to work I'll be sure to post examples and a follow up advanced collision detection AS: topic.

Response to AS: Collision Detection 2006-01-13 02:42:14


Nice work, still a little stuck but nice.

Response to AS: Collision Detection 2006-04-13 10:52:01


Um... just a thought of mine...

I'm still pretty new at AS, so feel free to correct me if I'm talking rubbish...

I've been reading these collision threads, and the Advanced Hittest was great for accuracy... but going through every pixel really lagged this flash I'm making, as there was a very large MC (thus many pixels to loop through, especially if using onClipEvent (enterFrame), rather than (load).

A very quick and simple method I used was instead of converting the entire thing to an MC, I just used the outline/border... converted that to an MC, as this made my flash much faster (less pixels).

I don't know if this would work nicely in all circumstances, but my large MC was static anyway... and the lagginess is gone.

Also I know that there were other methods of keeping accuracy but reducing the loops (such as looping through every 10th pixel), but for me it is more simple to just use the outline of my MC.

Response to AS: Collision Detection 2006-05-18 03:49:07


At 6/23/05 12:28 PM, BleeBlap wrote:
onClipEvent (load) {
xArray = new Array();
yArray = new Array();
for (x= _root.object1.getBounds(_root).xMin; x<=_root.object1.getBounds(_root).xMax; x++) {
for (y=_root.object1.getBounds(_root).yMin; y<=_root.object1.getBounds(_root).yMax; y++) {
if (_root.object2.hitTest(x, y, true)) {
xArray = xArray.concat(x);
yArray = yArray.concat(y);
}
}
}

ok i have a question. why is it that whenever i try to use this it does not work. anything i freakin try with stuff like this NEVER works. can someone please help?


// Harvest++ coming soon, more modes, more stuff, customizable character, achievements and more!

BBS Signature

Response to AS: Collision Detection 2006-12-15 15:59:27


my code:

onClipEvent (enterFrame) {
if (!this.hitTest(_root.lines._x, _root.lines._y, true)) {
this._y += _global.velocity;
_global.velocity = _global.velocity+_global.gravity;
} else {
this._x += speed;
}
}

doesn't work, if i change the hittest to a normal one, then it is fine but the more advanced one doesn't work, anyone know why?

Response to AS: Collision Detection 2007-05-19 16:35:30


ok i need serious help ive made a game with a cannon and when the cannonball hits the enemy the enemy should blow up but it is a big but
i have made it so the enemy moves towards the cannon whitch is staionnary useing this code

onClipEvent (enterFrame) { if (_root.tank._x > _x) { _x += 5; }}onClipEvent (enterFrame) { if (_root.tank._x<_x) { _x -= 5; }}onClipEvent (enterFrame) { if (_root.tank._y>_y) { _y += 5; }}onClipEvent (enterFrame) { if (_root.tank._y<_y) { _y -= 5; }}

looks complicated but it aint simply put the object with that code on it follows the object with a instance name of "tank"

anyway to the point i need a hit test so help plz
and some reason i cant upload the swf....

Response to AS: Collision Detection 2007-08-09 09:44:06


As far as walls go, I made the walls one object. I tried using code copied from other posts, and it's still not working. the character goes right through the walls.
it's all valid, but It doesn't work. A small note: for noobs such as me, you might want to consider explaining the code, so they can use it and tweak it, knowing what it does. also, i'm trying to make a maze with levels, but I cant get things to advance once the player comes in cantact with the goal.

Response to AS: Collision Detection 2007-08-24 18:12:04


lol Old thread revival. Oh well it's cool this is the most appropriate for it.

Anyways I just need a lil' help with hitTest. I pretty much just started learnin' 'em a week ago.

'Kay the code is this.

onClipEvent (enterFrame) {
if (_root.enemya.hitTest(_root.base)) {
gotoAndPlay(2);
}
}

Alright, in the game the enemys are running toward the base which you are defending. This code so far makes it when the enemy touches the base the base goes ro frame 2 of the base MC. That's not what I'm tryin' to do. What I need is to make the main timeline goto the second frame aka the "Game Over" screen. Now do I have to add a var. or somethin' to this? Or maybe make some sort of function?

I'm not too sure what to do or how to do this. So I'm askin'. :P


www.DuderEntertainment.com/ | Makin' Laughs and Kickin' Ass! >:3

BBS Signature

Response to AS: Collision Detection 2007-09-11 19:07:29


Hi and nice to meet you all,since it's my first post! I know you've heard this a million times but I need serious help here,my head is hurting! I am as noob as it gets,this is my first try, I started it two days ago and I must finish it cause it's a school project.

So, here it goes: The basic concept is that there is a happy man who is colliding with some apples and earning points. But I can't get the collision to work. My code is:

///IN THE FIRST FRAME

onLoad = function ()
{ i = 0; };

onEnterFrame = function ()
{
i++;
if (random(50) == 0) //Kabomb's code helped a lot here
{
duplicateMovieClip(_root.inst_apple, "inst_apple"+i, i);
_root["inst_apple"+i]._x = Math.random()*550;
_root["inst_apple"+i]._y = Math.random()*400;
}
}

///IN THE CHARACTER

onClipEvent(load){
speed=10;
}

onClipEvent (enterFrame)
{
_x+=Key.isDown(Key.RIGHT)*speed;
_x-=Key.isDown(Key.LEFT)*speed;
_y+=Key.isDown(Key.DOWN)*speed;
_y-=Key.isDown(Key.UP)*speed;

for(i=0; i<100; i++)
{
if (this.hitTest(_root["inst_apple"+i]))
{_root.score++;}

}
}

I tried to make it very primitive to see what's wrong, so that's pretty much all.

If I write:

if (this.hitTest(_root.inst_apple))
{ _root.score++; }

it works fine for my first apple, but when I write the above with the for loop, it doesn't work neither for the first nor for the duplicated.

Anyway, that's it, maybe it is very obvious and I don't see it, any help would be very appreciated. Feel free to laugh at me but please help!

P.S. Sorry for the long post, we women tend to talk a lot!

Response to AS: Collision Detection 2008-07-31 18:00:39


hey i was wondering i have a bush and i need my guy not to walk through it :<<<<

so any flash wizards want to help?

AS: Collision Detection

Response to AS: Collision Detection 2011-12-16 14:27:45


Im trying to learn flash again so im working on as2. I making a simple get to get back in AS2. I have a "player" that I want to walk over to a "door" and gotoAndStop(2); I just want to go to the next frame when they touch can any one help?


rate my art movies and games and give feed back