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

2015/2/6

Problem with having bullet hit different enemies

DarkGhost DarkGhost

2015/2/6

#
Hey everyone, I'm having some trouble with making my bullet.class taking away health from multiple enemies classes... It works if I have only one hitEnemy method but once I have 2 or more in there it gives me an error. In my game I have 4 enemies RahikiLvl1, RahikiLvl2, RahikiLvl3 and the Boss. I think the problem is with hitEnemy(s) in the act method. bullet.class:
public class Bullet1 extends Cymeriadau
{
    private int direction, speed;
    public Bullet1(int dir)
    {
        direction = dir;
        speed = 15;
    }

    public void act()
    {
        setRotation(direction);
        move(speed);
        if( atWorldEdge() ) {
            getWorld().removeObject(this);
            return;
        }
        
        hitEnemy();
        hitEnemy2();
        hitEnemy3();
        hitEnemy4();
        return;
    }

    public void hitEnemy() {
        RahikiLvl1 enemy = (RahikiLvl1) getOneObjectAtOffset(0, 0, RahikiLvl1.class);
        if (enemy != null) {
            enemy.setHealth(-5);
            getWorld().removeObject(this);
        }

    }
        public void hitEnemy2() {
        RahikiLvl2 enemy = (RahikiLvl2) getOneObjectAtOffset(0, 0, RahikiLvl2.class);
        if (enemy != null) {
            enemy.setHealth(-5);
            getWorld().removeObject(this);
        }

    }
        public void hitEnemy3() {
        RahikiLvl3 enemy = (RahikiLvl3) getOneObjectAtOffset(0, 0, RahikiLvl3.class);
        if (enemy != null) {
            enemy.setHealth(-5);
            getWorld().removeObject(this);
        }

    }
        public void hitEnemy4() {
        Boss enemy = (Boss) getOneObjectAtOffset(0, 0, Boss.class);
        if (enemy != null) {
            enemy.setHealth(-5);
            getWorld().removeObject(this);
        }

    }

}
danpost danpost

2015/2/6

#
The reason for the 'return' statement at line 16 after 'getWorld().removeObject(this)' is to ensure that further lines of code within the method are not executed after the bullet is removed from the world when a world edge is encountered. Each 'hit' method has a chance to remove the actor from the world. If any (but the last one) does remove the bullet from the world, the following one will fail to execute because you cannot get objects at an offset in a world that does not exist for the bullet. You must ensure the actor is still in the world before each 'hit' method call (the 'getWorld' method of the Actor class will return 'null' if the actor is not in the world).
DarkGhost DarkGhost

2015/2/6

#
Ah I see, sorry I only started coding recently, what would I need to write in order for it to work? Would I need to add a return statement after each hitEnemy method?
DarkGhost DarkGhost

2015/2/6

#
Ok so I changed the act method to this: It fixed the error message, but the player can only kill the RahikiLvl1 and for all the other enemies the bullets just go straight over them...
    public void act()
    {
        setRotation(direction);
        move(speed);
        if( atWorldEdge() ) {
            getWorld().removeObject(this);
            return;
        }

        if (RahikiLvl1.class != null) {
            hitEnemy();
            return;
        }
        
        if (RahikiLvl2.class != null) {
            hitEnemy2();
            return;
        }
        
        if (RahikiLvl3.class != null) {
            hitEnemy3();
            return;
        }
        
        if (Boss.class != null) {
            hitEnemy4();
            return;
        }
    }
danpost danpost

2015/2/6

#
You have a misconception here. '????.class' will never be 'null' as the compiler will complain if the class does not exist. In other words, you are not checking for objects of those classes; you do that within the 'hit' methods anyway. Change all '???.class' in your last code post to 'getWorld()' and remove all the 'return' statements.
DarkGhost DarkGhost

2015/2/7

#
Sweet, thanks so much! It works now! Thanks for your help!
You need to login to post a reply.