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

2014/10/15

Invincibility?

CKnox CKnox

2014/10/15

#
In my Space shooter game there is an explosion code that when the rocket explodes any nearbyy objects are also exploded. My problem is that I had to create an actor class for the stars so I could move them, and they explode aswell, exploding everything. Is there a way to make the star class invincible so they aren't affected by the explosion? This is the coed for explosing others.
 /**
     * Explode all intersecting objects.
     */
    private void explodeOthers()
    {
        List explodeEm = getIntersectingObjects(null);
        Iterator i = explodeEm.iterator();
        while(i.hasNext()) {
            Actor a = (Actor) i.next();
            if( ! (a instanceof Explosion)) {//dont explode other explosions
                int x = a.getX();
                int y = a.getY();
                //Replace other objects with an explosion
                getWorld().removeObject(a);
                getWorld().addObject(new Explosion(), x, y);
            }
        }
    }
Super_Hippo Super_Hippo

2014/10/15

#
You could either do it like in line 10 that you say "only do it if it isn't a star" or instead of 'null' in line 6 you write in the class which should explode.
CKnox CKnox

2014/10/16

#
Do you have any suggestions for the "only if it isn't a star"? I already tried replacing null with the star class but it didnt work.
Super_Hippo Super_Hippo

2014/10/16

#
In line 10 you check if the intersecting object is a Explosion and only if it isn't, the next block will be executed. So if you replace line 10 with 'if( !(a instanceof Explosion) && !(a instanceof Star))', then it checks for both and only executes the next lines if it isn't an Explosion or a Star.
CKnox CKnox

2014/10/21

#
Thankyou Hippo, it works now!
You need to login to post a reply.