How to access to the colliding entity's variables when a collision happens


(Antonin Adeline) #1

Hello guys,

I’m currently developping a shooter game and I’m facing a tricky problem : I’ve a player which fires colored bullets (BulletPlayer) who collide with enemies (Enemy) and destroy them. I’d add a colored FX (DamageFX) when a bullet hits an enemy but I cannot access to my bullet variables during the collision in order to get its color.

Here is my Enemy’s code, where I check collisions :

var bullet : Entity = collide("bulletPlayer", x, y);
                if (bullet)
                {
                        life--; 
                        var damageFX : DamageFX = new DamageFX (bullet.bulletColor);                   
                        bullet = (bullet as BulletPlayer);
                        FP.world.add(damageFX);
                        // OR
                        // bullet.GenerateFX();
                        FP.world.remove(bullet);
                    }

Here is my BulletPlayer’s code :

override public function added():void 
        {
            super.added();
            img = new Image(IMG_BULLETPLAYER);
            graphic = img;   
            setHitbox(img.width, img.height);
            type = "bulletPlayer";

            img.color = FP.choose(0x334D5C, 0x45B29D, 0xEFC94C, 0xE27A3F, 0xDF4949);
            bulletColor = img.color;
        } 

       public function GenerateFX():void
       {
            var damageFX : DamageFX = new DamageFX (bulletColor);
            FP.world.add(damageFX);
       }

Errors are: "Access of possibly undefined property bulletColor through a reference with static type net.flashpunk:Entity." and : "Call to a possibly undefined method generateFX through a reference with static type net.flashpunk:Entity."

I tried to figure this out for a long time, and still no solution just for having a FX with the same color than the bullet which hit the enemy … Do you have any ideas or suggestions ?

Thanks !


(Martí Angelats i Ribera) #2

You forgot to pass the bullet to BulletPlayer:

This is the line you have to change:

var damageFX : DamageFX = new DamageFX ((bullet as BulletPlayer).bulletColor); 

But usually it’s better to do this:

var bullet:BulletPlayer = collide("bulletPlayer", x, y) as BulletPlayer;
if (bullet)
{
    life--; 
    var damageFX : DamageFX = new DamageFX (bullet.bulletColor);
    FP.world.add(damageFX);
    FP.world.remove(bullet);
}

This way you avoid this kind of errors.


(Antonin Adeline) #3

It perfectly works, thanks a lot ! I don’t exactly get the way how does this exactly works but actually it’s the grammar of the code which was troubling me.


(Martí Angelats i Ribera) #4

OK so you don’t seem to understand how as works. Let me explain:

as is an operator that performs a casting from an object to another class (casting is changing the object’s class). If the cast couldn’t be made it return null. Obviously, if you don’t save the casted object you’ll have to cast it again the next time you want.

So when you do

collide("bulletPlayer") as BulletPlayer

you are getting an Entity from collide(...) but you know that BulletPlayer is a subclass of it. So you cast from Entity to BulletPlayer.

If you save the collided entity as Entity you’ll have to cast to BulletPlayer every time you have to call a function. But it’s better not to do it. Remember, if it wasn’t able to cast the Entity to BulletPlayer it will become null. The line

if (bullet)

will make sure you have a value there, avoiding possible anoying 1009 errors (null reference error).

PD: I don’t know what project are you doing but collide only detects if some entity is colliding another. But when it founds one it stops to look for more. In the other hand, 'collideInto` saves all the entities that are colliding into an array or vector.


(Antonin Adeline) #5

My main mistake was I typed the casting as BulletPlayer at the wrong place when I was trying to figure out the problem. I used to place it after checking if my enemy was colliding with any bullet.

I never really used casting before, it’s why I’m a bit disappointed about how it’s working and even more about what are the possibilities.

I’m developing a vertical shooter so there is a spaceship moving and firing bullets on enemies who are coming from the top of the screen. It’s very basic on paper but I’m focused on the game feel so each bullet has to have feedbacks when hitting an enemy.


(Martí Angelats i Ribera) #6

Even if you’d use the line

bullet = bullet as BulletPlayer;

at the top it wouldn’t work. bullet is declared as Entity. You can’t change this, so you’ll had to make another variable.

Also, for the colliding consider detecting it the other way arround: Make the bullets detect the player or enemy. I know this sound a bit odd but it can avoid errors.