PDA

View Full Version : Classes and Hit inside


Kimchee
12-06-2008, 05:16 AM
So I create a class Bullet.

I then created a class Baddie, representing my bad guys.

How might one write a hitTest code in the Baddie Class to see if they are ever hit by the class Bullet? Can I reference a class inside another class?

EightySeven
12-06-2008, 02:44 PM
public var bullets:Array = new Array();

var b:Bullet = new Bullet(what ever your bullet params are);
addChild(b);
bullets.push(b);

for (var i:int = 0;i < bullets.length - 1; i++){
if (enemy.hitTestObject(bullets[i])){
//do somethign witht he collision
}
}

Ok so thats just a quick glance at how I handle bullet collision.

basicly ur just checking an array to see if anything in it is touching the enemy then if it is do something about it

Kimchee
12-06-2008, 04:58 PM
Thanks for the reply. I just wasn't very specific, I am able to do bullet listeners in the same program where the bullets are created using almost an identical code to the one you provided.

However what I am trying to do is the following.

I have a character class that when I hit spacebar he shoots bullets created from a bullet class. ( I can do this)
Then I load a bad guy class to the stage.(I can do this).

I want the badguy class to have an internal program that listens for the bullet class automatically without having to write the listening program in the stage program. So basically all I want on the stage's program is

var g: character = new character();
addChild(g);
var q: baddie= new baddie();
addChild(baddie);

and then the classes programs will run silently behind listening for the user to fire and listening for collisions of bullets. :)

I just can't get the listerner to work inside a class...