PDA

View Full Version : [AS3] Collision Detection


Ciubhran
02-13-2009, 12:42 PM
I have tried using hitTestObject now many, many times, without any success.

I cant get anything to collide.

I am using all MovieClips in my application, one MovieClip is moving towards another (a wall) and when it collides, it is supposed to bounce off, but it just runs straight through it.

How would you suggest doing it in AS3; which functions etc?

Right now I am trying this:


// inside the Projectile class (which extends MovieClip)
if(this.hitTestObject(wall)) {
trace("bong");
}

Cauterize
02-13-2009, 12:45 PM
Are you referencing the wall correctly?
If this is within the Projectile class, it wont the scope of where wall is.

Ciubhran
02-13-2009, 12:57 PM
Everytime I create a Projectile, I send the root stage as an argument, and then store it inside the Projectile object.

Right now it is done in the Projectile class, in the same function that moves the projectiles forward.

Projectile objects do not exist until I spawn them by pressing SPACE, they are created using a function within the class Chaingun and added to the parent, and then added to an array inside the Chaingun class (for control).

The thing it will collide with is created using a class called Enemy that attaches itself to the stage in the constructor:

stage.addChild(this);

Ciubhran
02-13-2009, 02:34 PM
Is there some general rule you must follow regarding design when it comes to collisions?

EightySeven
02-14-2009, 03:15 AM
http://www.emanueleferonato.com/2006/10/29/flash-game-creation-tutorial-part-1/

start there around page 3 he gets into collision but for u i woudl say start on page 1 its not as3 but the principals of colision detection are teh same and u can figure it out from there


edit: i actually think hes done an as3 version if you don't understand actionscript very well

Ciubhran
02-15-2009, 11:25 AM
please link me the as3 version then because that one doesn't help me at all, since it seems to be in as2 or something (_root).

I am trying to get two Objects to collide. A bullet (public class Projectile extends MovieClip) and an enemy (public class Enemy extends MovieClip).

aaron_da_killa
02-23-2009, 06:07 AM
I've been working with flash for about 20 days now and am making a platform game. I've managed to get collison detection to work in my game, however it looks as though your putting your code on the timeline instead of in a .as file. Anyway, I'll post some examples and let you figure the rest out! :P

Code to do something if two movieclips overlap


if(player.hitTestObject(level2.trigger01)){
jasonSoundPlaying = true;
jasonSoundEffect.play();
}
}


With this code, if the movieclip player and the movieclip trigger01 (which is inside the movieclip level2 - took me a long time to work out you can have movieclips inside movieclips) than it changes the state of a variabl and plays a sound.

Here is my collison detection code which is more complex. I have had to work out collison detection on each of the four side of my movieclip.

Firstly, I have my collison detection listner function, this function is triggerd every frame:

public function ifplatformCollide(event:Event):void{
if (player.hitTestObject(level2.platform)){
playerTouchingPlatform = true;
platformSit();
}
else {
playerTouchingPlatform = false;
}
}


If the player and platform collide, than this function is triggerd:


public function platformSit():void{
//TOP SIDE
if (player.y + player.height <= level2.platform.y + level2.platform.height/2 && player.x + player.width > level2.platform.x + 8 && player.x < level2.platform.x + level2.platform.width - 8){
playerJumping = false;
player.y = level2.platform.y - player.height;
}
//BOTTOM SIDE
else if (player.y >= level2.platform.y + level2.platform.height - level2.platform.height/2 && player.x + player.width > level2.platform.x + 8 && player.x < level2.platform.x + level2.platform.width - 8){
mainSpeedLeft = 4;
mainSpeedRight = 4;
jumpSpeed = 2;
}
//RIGHT SIDE
else if (player.x >= level2.platform.x + level2.platform.width - level2.platform.width/2 && player.y + player.height > level2.platform.y + 5){
mainSpeedLeft = 0;
playerTouchingPlatformSide = true;
}
//LEFT SIDE
else if ((player.x + player.width) <= (level2.platform.x + (level2.platform.width/2)) && player.y + player.height > level2.platform.y + 5){
mainSpeedRight = 0;
playerTouchingPlatformSide = true;
}
else {
playerTouchingPlatformSide = false;
mainSpeedLeft = 4;
mainSpeedRight = 4;
}

}


It may seem daunting at first but its very simple code. If you look closly, you will notice that I have left alot of "leeway" in the collison detection. If I have not left this leeway, than it is possible for the player to travel right through the block if he is traveling too fast. Infact, with this code, it is still possible for the player to pass through the block, so you have to make sure the player can not move to fast!