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

2017/4/22

"YouLost" not working

1
2
Ostarius Ostarius

2017/4/22

#
Any idea why my "You Lost" screen is not working? In MyWorld class:
public void act()
    {
        this.showText("Punkte: " + DonkeyKong.score, 50, 10);
        this.lost();
    }
    
    public void lost()
    {
        YouLost youlost = new YouLost();
        if (!getObjects(DonkeyKong.class).isEmpty())
        {
            this.addObject(youlost, this.getWidth()/2, this.getHeight()/2);          
        }
    }
You
public void act() 
    {
        setImage(new GreenfootImage("You Lost!!! ", 60, Color.WHITE, Color.BLACK));
    } 
Lost Class:
Ostarius Ostarius

2017/4/22

#
And if someone wants to solve this too: I have a Boss.class and a DkBoss.class I want the boss to die if he has 0 lives. This is my Code:
public void act() 
    {
        move(4);
        this.touchingBoss();
    }    
    
    public void touchingBoss()
    {
       Actor boss = getOneIntersectingObject(Boss.class);
       if (boss != null) {
       if(this.isTouching(Boss.class) && Boss.lives > 0) {    
           Boss.lives = Boss.lives -1;
           getWorld().removeObject(this);
       }
       }    
       else if(this.atWorldEdge())
       {
            this.getWorld().removeObject(this);
       } 
       
       if (Boss.lives == 0)
       {
           getWorld().removeObject(boss);
       }
    }
Super_Hippo Super_Hippo

2017/4/22

#
Define "not working". What is happening, what should happen?
Super_Hippo Super_Hippo

2017/4/22

#
For the boss thing, don't make 'lives' static. Use 'boss' (the object) instead of 'Boss' (the class) when accessing the 'lives' variable. Check if the variable is 0 right after decreasing it.
Ostarius Ostarius

2017/4/22

#
If i DonkeyKong.class dies it should show: "You Lost!!!" But "You Lost!!!" shows all the time and it starts lagging.
Ostarius Ostarius

2017/4/22

#
Super_Hippo wrote...
For the boss thing, don't make 'lives' static. Use 'boss' (the object) instead of 'Boss' (the class) when accessing the 'lives' variable. Check if the variable is 0 right after decreasing it.
I didn't understand what you wanted me to do. Well if the bullet hits him, he loses 1 life, and if he gets 0 lives the game doesn't work.
Super_Hippo Super_Hippo

2017/4/22

#
Remove the ! in line 10.
Ostarius Ostarius

2017/4/22

#
It shows a "NullPointerException" If i run the game again and hit him one more time, he dies, java.lang.NullPointerException at DkBoss.touchingBoss(DkBoss.java:37) at DkBoss.act(DkBoss.java:18)
Ostarius Ostarius

2017/4/22

#
Super_Hippo wrote...
Remove the ! in line 10.
If you meant that in "MyWorld", i did and it doesn't show "You Lost"
danpost danpost

2017/4/22

#
Change line 10 to:
if (getObjects(DonkeyKong.class).isEmpty() && getObjects(YouLost.class).isEmpty())
You only want to create and add a YouLost object when the player is not in the world AND a YouLost object has not been added yet.
Ostarius Ostarius

2017/4/22

#
So the thing is I did what danpost told me to do and it only works when i die i click "run" again
Super_Hippo Super_Hippo

2017/4/22

#
public void touchingBoss()
{
    Actor boss = getOneIntersectingObject(Boss.class);
    if (boss != null)
    {
        boss.loseLife(1);
        getWorld().removeObject(this);
    }
    //remove the rest in the method
    //checking if it is at the edge of world shouldn't be checked here
}
//in Boss class
private int life = 3;

public void loseLife(int amount)
{
    life -= amount;
    if (life < 1) getWorld().removeObject(this);
}
Ostarius Ostarius

2017/4/22

#
Super_Hippo wrote...
public void touchingBoss()
{
    Actor boss = getOneIntersectingObject(Boss.class);
    if (boss != null)
    {
        boss.loseLife(1);
        getWorld().removeObject(this);
    }
    //remove the rest in the method
    //checking if it is at the edge of world shouldn't be checked here
}
//in Boss class
private int life = 3;

public void loseLife(int amount)
{
    life -= amount;
    if (life < 1) getWorld().removeObject(this);
}
"Cannot find Symbo - method loseLife(int)l"
Ostarius Ostarius

2017/4/22

#
danpost wrote...
Change line 10 to:
if (getObjects(DonkeyKong.class).isEmpty() && getObjects(YouLost.class).isEmpty())
You only want to create and add a YouLost object when the player is not in the world AND a YouLost object has not been added yet.
Danpost you here?
danpost danpost

2017/4/22

#
Ostarius wrote...
Danpost you here?
For the moment. I am getting ready to depart for a while. Line 6 of what Hippo supplied should be:
((Boss)boss).loseLife(1);
There are more replies on the next page.
1
2