This site requires JavaScript, please enable it in your browser!
Greenfoot back
nizate
nizate wrote ...

2017/7/29

Removing an object (this) after being hit and losing health.

nizate nizate

2017/7/29

#
I have made multiple different variations of this, so I turned to a book example. (Asteroids 3) in which when the Spaceship gets destroyed after running into an asteroid, so I used that as a base:
    private void checkHit(){
        EnergyBeam a = (EnergyBeam) getOneIntersectingObject(EnergyBeam.class);
        if (a != null) 
        {
            Namek namek = (Namek) getWorld();
            namek.addObject(new Explosion(), getX(), getY());
            health -= 1;
            Greenfoot.playSound("explode.wav");
            removeTouching(EnergyBeam.class);
            if(health <= 0){
                namek.removeObject(this);
            }
        }
}
This still causes me to get a null actor error (From the explosion getting x and y. What am I doing wrong here? Any and all help is greatly appreciated!
nizate nizate

2017/7/29

#
I found out the issue, it was referencing from multiple spots, so I created an if HP<0 then dead =1 and created if dead== statements to counter this issue.
You need to login to post a reply.