00:00
00:00
Newgrounds Background Image Theme

Arnahan 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: Collisions

15,788 Views | 26 Replies
New Topic Respond to this Topic

AS: Collisions 2005-06-28 13:47:59


-INTRO-
So, what is a collision?

A collision is to check whether 2 objects are touching.

Flash has a built-in collision detection function called hitTest(). It has some uses, but we are concentrating more here on advanced stuff.

-hitTest()-
Hit Test? Well, to start it has the following form

instance.hitTest(instance)
or
instance.hitTest(pointx, pointy, shapeflag)

The first method tests whether 2 symbols are touching and returns true if they are. There are huge problems with this, though. Say you have 2 rectangles. One named "r1" and one named "r2". On r1 put the following code:

onClipEvent(enterFrame){
_x = _root._xmouse
_y = _root._ymouse
if(hitTest(_root.r2){
trace("xx")
}

This should work fine. When the 2 rectangles collide, it traces xx to the output window.

Now change the shape of the rectangles. Make it like a blobshape or something.

The code won't work as well, because it draws a rectangle around your objects when using this function.

The second way is like so. hitTest(x,y,shapeflag)
Set shapeflag to true and the point to 275, 200

if(hitTest(275,200,true){
trace("xx")
}

If the symbol is touching the point (275,200) it traces "xx" to the output window. The problem is is that this only works with a single point, but leaves out the bounding box of the symbol (the virtual rectangle drawn around it)

So how do we make it better? We code our own functions or use simple tricks.

-TRICKS USING hitTest()-

You can use mutiple points on an object to test against another object.

OR You can hitTest against a symbol inside another symbol that is invisible and shrunk a little.

-ADVANCED HIT TEST-

*Circle vs Circle
Let's see.... To accurately test 2 circles together, all we need to do is find the distance between them. Make 2 circles, 1 named c1 and 1 named c2 Put this code on c1

onClipEvent(enterFrame){
_x = _root._xmouse
_y = _root._ymouse
radius = _width/2
radius2 = _root.c2._width/2
dist = Math.sqrt(Math.pow(_x-_root.c2._x, 2)+Math.pow(_y-_root.c2._y, 2))
if(dist<radius+radius2){
trace("xx")
}

Test your movie. If the circles are hitting, it will trace "xx". The word hitTest is not found in that code once. What is does is finds the distance using the pythagorean therom. d=[radical]deltax[squared]+deltay[squared]
If the distance is less than the sum of the radiuses, the circles are touching.

By using math you can test collision detection against almost any shapes.

For further reading:
Beizer Curve Collision
http://www.gotoandplay.it/_articles/2004/07/collisions.php
http://www.gotoandplay.it/_articles/2004/07/3dBalls.php
http://www.gotoandplay.it/_articles/2004/09/collisions.php
http://www.gotoandplay.it/_articles/2003/09/collisions.php

~a GG tutorial

Response to AS: Collisions 2005-06-28 13:50:13


Holy shit! Another AS thread! I still liked this becuase it helped me a lot! Thanks.

Response to AS: Collisions 2005-06-28 13:52:57


you're welcome.

Response to AS: Collisions 2005-06-28 13:55:51


Heh, AS: threads seem to be going wild today... nice to see it catching on, and the Flash forum finally starting to have a decent collection of specific Actionscripting threads.

Collision detection was actually already covered (AS: Collision Detection by BleeBlap), but I guess there's no harm in having two different explanations of the same code. You covered some points that BleeBlap missed, and vice versa.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Collisions 2005-06-28 13:59:58


yeah, I guess. I just looked and saw the double shape flag hitTest thing and was like wow.

Response to AS: Collisions 2005-10-08 15:57:58


to start off, the code with the two retangles isn't workign =/

Response to AS: Collisions 2005-10-08 16:41:09


At 10/8/05 03:57 PM, Dipshitprod wrote: to start off, the code with the two retangles isn't workign =/

He just missed a bracket and an end parenthese (sp?):

onClipEvent(enterFrame){
_x = _root._xmouse;
_y = _root._ymouse;
if(hitTest(_root.r2)) trace("xx");
}


BBS Signature

Response to AS: Collisions 2006-02-23 15:07:17


finnaly a good hit test thread/ tut thing cool!

Response to AS: Collisions 2006-02-23 15:09:07


Great job, really explained it in detail. There were a few suntax errors, was that intentional (so nobody can copy and paste)? Just wondering.

Another AS thread to add to my favorites.


wew

Response to AS: Collisions 2006-03-15 22:15:41


Thank you so much! this is just the help i needed with 2 circles! thanks again!

Response to AS: Collisions 2006-03-30 17:37:10


Hey all,

I'm having some problems of my own with the hittest...

When using:

bc=1000; //bulletcount
onMouseDown=function(){
bc++;
if(bc>1050){bc=1000;}
duplicateMovieClip("bullet", "b"+bc, bc);
with(_root["b"+bc]){
spd = 20;
_x=gun._x;
_y=gun._y;
_rotation = _root.gun._rotation;
}
_root["b"+bc].onEnterFrame=function(){
with(this){
if (_rotation>180) {
_y += (spd*Math.cos( Math.PI/180*_rotation));
_x -= (spd*Math.sin( Math.PI/180*_rotation));
} else {
_y -= (spd*Math.cos( Math.PI/180*_rotation));
_x += (spd*Math.sin( Math.PI/180*_rotation));
}
if(_x>Stage.width || _x<0 || _y<0 || _y>Stage.height){
this.removeMovieClip();
}
}
}
}

onMouseMove=function(){
with(gun){
this.Xd =_root._xmouse-_x;
this.Yd =_root._ymouse-_y;
radAngle = Math.atan2(Yd, Xd);
_rotation = int((radAngle*360/ (2*Math.PI))+90);
}
}

I used this ^ AS as above to duplicate a 'bullet' instance from the gun in my project. Yet, when trying to combine this with a hittest, it doesn't work. That is, the original bullet MC has the following code in it, and if that is in contact with 'enemy' it registers a hit... however the duplicates do not register. I made it so that it plays a MC if the hittest registers, and as I say the original bullet does register, but the duplicates, on hitting the target, do not cause the MC 'hitbox' to play.

onClipEvent (load) {
xArray = new Array();
yArray = new Array();
for (x= _root.bullet.getBounds(_root).xMin; x<=_root.bullet.getBounds(_root).xMax; x++) {
for (y=_root.bullet.getBounds(_root).yMin; y<=_root.bullet.getBounds(_root).yMax; y++) {
if (_root.enemy.hitTest(x, y, true)) {
xArray = xArray.concat(x);
yArray = yArray.concat(y);
}
}
}
for (z=0; z<xArray.length; z++) {
if (_root.bullet.hitTest(xArray[z], yArray[z], true)) {
_root.hitbox.gotoAndPlay("2");

break;
}
}
}

Why is this? I'm not very advanced with AS, but I'm trying to learn and any help with a fix to these codes, or advice to understand better this AS, would be much appreciated.

Thanks in advance,

James.

Response to AS: Collisions 2006-03-31 06:50:07


Any help please?

Response to AS: Collisions 2006-12-07 20:48:19


damn it this didnt help me
i tryed 1 tut but it didnt work
i tried the ultimate tut collab nd that jst let me psh it
cn u tell me how to just make a god damn wall


ninja-pirate-monkey

BBS Signature

Response to AS: Collisions 2006-12-10 20:57:35


what he said

Response to AS: Collisions 2006-12-10 21:48:28


In response to ninja-pirate-monkey and Hawkfire, You can just put

onClipEvent (enterFrame){if (hittest(_level0.mc) == true)

in the object you want to hittest, and then replace mc with the object you want to hit against. Then you add the code to stop the object. If you are using a variable to control movement, make that var = 0, or if you are using some simple this._x+=4 junk or something like that, you can add

this._x-=4;}}

to the end. That is just for the rectangle around the object. no problems for squares. If that doesn't work, check the semicolons and paranthesis. If that doesn't work, it could just be my program.

If i wasn't clear enough,

onClipEvent(enterFrame){if(hittest(_level0.mc ) == true){_root.variable = 0;}}

edit that code to fit your situation

Response to AS: Collisions 2006-12-10 22:38:27


At 12/10/06 09:48 PM, 2MuchCaffeineGames wrote: onClipEvent (enterFrame){if (hittest(_level0.mc) == true)

you dont have to put == true since the == operater and the hittest function both return a boolean which is what the if() reads. also hitTest() not hittest()

if (hitTest(_level0.mc))

works just fine

Response to AS: Collisions 2007-05-22 14:39:28


HELP

in many games you drive a movieclip and when another movieclip (enemy) collides with each other. but instead of a box saying touching etc. its plays to a different frame where it may say "game over" or "your dead".

I want to know how to do this. could you reply in email either with a flash file on a example game or a fully explanation on how to do it. please .

please could you reply A.S.A.P

MANY THANKS CALUM

Response to AS: Collisions 2007-05-22 15:52:14


At 5/22/07 02:39 PM, calsterman wrote: please could you reply A.S.A.P

You do realize that you're reviving a thread that has been dead for six months?

Response to AS: Collisions 2007-07-12 21:02:10


could you post a link to an SWF?

Response to AS: Collisions 2007-07-12 21:04:34


At 7/12/07 09:02 PM, senior-twinki wrote: could you post a link to an SWF?

Why the hell did you bump this?

Response to AS: Collisions 2008-07-23 19:38:34


What do you mean by trace XX? I don't get it.


If at first you don't succeed, skydiving is not for you.

Response to AS: Collisions 2008-07-24 07:25:10


Another great tutorial glaiel this helped me a lot.

Response to AS: Collisions 2008-07-24 20:28:28


Could you explain each part of the actionscript, so I can figure out how to put it in my game? It's so confusing for me. Partly because I'm probably because I'm a total nubcake at actionscript.


If at first you don't succeed, skydiving is not for you.

Response to AS: Collisions 2008-07-24 21:06:23


Huh? what's bumping?


If at first you don't succeed, skydiving is not for you.

Response to AS: Collisions 2008-07-25 01:23:25


At 7/24/08 08:28 PM, mechias3 wrote: Could you explain each part of the actionscript, so I can figure out how to put it in my game? It's so confusing for me. Partly because I'm probably because I'm a total nubcake at actionscript.

If this is hard for you, you need to go back to the basics first. You need to learn what the heirarchy is, that will help you out a lot in understanding code


.

BBS Signature

Response to AS: Collisions 2010-05-24 00:26:17


Thanks for this, it helped.

Response to AS: Collisions 2010-05-24 00:32:44


At 5/24/10 12:26 AM, BeefJuice wrote: Thanks for this, it helped.

Pointless 2 year bump...


#include <stdio.h>

char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";

main() {printf(p,10,34,p,34,10);}

BBS Signature