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

2013/3/5

Health for enemies

1
2
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
You should put the if statement in the act method at the end of it. (if (getX() > ...). Then the error should no longer occour.
Nostic Nostic

2013/5/17

#
I don't get what you mean.. I should but the if statement in the act method which is:
if(getX()>790)
        {
            getWorld().removeObject(this);
            return;
        }
Exactly where?
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
Put this at the end of the act method.
Nostic Nostic

2013/5/17

#
Like:
setLocation(getX() + speed, getY()); 
        kollisionDetection();
        hitAlien1();
        hitAlien2();
        if(getX()>790)
        {
            getWorld().removeObject(this);
            return;
        }
Still getting the same error before and Alien2 enemy2 = (Alien2) getOneObjectAtOffset(0, 0, Alien2.class); highlighted, when I click to see the error.
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
Oh then the problem is that your laser gets removed if he hits an alien. You should try to make one method out of the two hitAlienMethods like this:
public void hitAlien()   
   {    
       Alien1 enemy = (Alien1) getOneObjectAtOffset(0, 0, Alien1.class);   
       Alien2 enemy2 = (Alien2) getOneObjectAtOffset(0, 0, Alien2.class);   
            if (enemy != null) {    
                enemy.setHealth(-1); 
                getWorld().removeObject(this); 
            }  
            else if (enemy2 !=null) {  
                enemy2.setHealth2(-1);  
                getWorld().removeObject(this);  
            }    
        } 
}      
Nostic Nostic

2013/5/17

#
Yes it's working! If I want to add 1 more, what should I do then? (Now I've my two different enemies, I want a boss 1 too)
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
You should do it the same way like the second enemy. Add it in the existing method like in the method I posted before.
Nostic Nostic

2013/5/17

#
Okay, thanks for your help! I really appreciate it! This game I'm making, is a school project, which I've 5,5 hours back before I've to turn it in >.< You just shortened the work alot! :)
danpost danpost

2013/5/17

#
You could keep all the methods separate and call them all in one 'if' statement from the act, and remove the object from within the 'if' block. You will need to change the return type of those methods from 'void' to 'boolean' and return the appropriate values. Your methods would then be:
public void act()
{
    setLocation(getX() + speed, getY()); 
    if (getX()>790 ||
        kollisionDetection() ||
        hitAlien1() ||
        hitAlien2())
            getWorld().removeObject(this);
}

public boolean kollisionDetection()  
{  
    Actor bullet = getOneIntersectingObject(Laser.class);  
    if (bullet != null) { 
        getWorld().removeObject(bullet);  
        return true;
    }
    return false;
}

public boolean hitAlien1() 
{
    Alien1 enemy = (Alien1) getOneObjectAtOffset(0, 0, Alien1.class);  
    if (enemy != null) {
        enemy.setHealth(-1);
        return true;
    }
    return false;
}

public boolean hitAlien2()
{           
    Alien2 enemy2 = (Alien2) getOneObjectAtOffset(0, 0, Alien2.class);
    if (enemy2 !=null) {
        enemy2.setHealth2(-1);
        return true;
    }
    return false;
}
gonnaownurass gonnaownurass

2013/5/24

#
when i try to call this public void setHealth(int points) { health += points; it keeps saying .class expected
gonnaownurass gonnaownurass

2013/5/24

#
for the int points
danpost danpost

2013/5/24

#
You need to show more of the code surrounding this method. It could be something like improper bracketing.
You need to login to post a reply.
1
2