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

2022/2/15

Calling a method in a different class to change an integer

teobox teobox

2022/2/15

#
I have this code in a class that calls a method in the Hearts class once when it is touching Bob
boolean isTouchingBob = false;
    Town town = (Town)getWorld();
    
    
    public void act() 
    {
        damageBob();
        
    }   

    public void damageBob()
    {
        if(isTouching(Bob.class))
        {
            
            
            if(isTouchingBob == false)
            {
                 
                    town.returnHearts().bobLives(-1);
                    isTouchingBob = true;
                
            }
            else
            {
                isTouchingBob = false; 
            }
        }
    }
The method below changes the amount of lives... Town town = (Town)getWorld(); public void bobLives(int num) { lives += num; if(lives>=6) { setImage("heartsfull.png"); } if(lives==5) { setImage("2hearts1half.png"); } if(lives==4) { setImage("2hearts.png"); } if(lives==3) { setImage("1heart1half.png"); } if(lives==2) { setImage("1heart.png"); Greenfoot.playSound("lowhealthmusic.wav"); } if(lives==1) { setImage("halfheart.png"); } if(lives<=0) { setImage("nohearts.png"); Greenfoot.setWorld(new GameOver()); Greenfoot.playSound("jingles_NES15.wav"); Greenfoot.delay(12); Greenfoot.playSound("jingles_NES15.wav"); Greenfoot.delay(12); Greenfoot.playSound("jingles_NES15.wav"); } } ...but whenever I run the method I get
java.lang.NullPointerException
	at Damage.damageBob(Damage.java:33)
	at Damage.act(Damage.java:20)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
I've been trying a lot of different things, none of them working, so it would be great if someone could help me
teobox teobox

2022/2/15

#
*correction*
 Town town = (Town)getWorld();

    public void bobLives(int num)
    {
        lives += num;
        if(lives>=6)
        {
            setImage("heartsfull.png");
        }
        if(lives==5)
        {
            setImage("2hearts1half.png");
        }
        if(lives==4)
        {
            setImage("2hearts.png");
        }
        if(lives==3)
        {
            setImage("1heart1half.png");
        }
        if(lives==2)
        {
            setImage("1heart.png");
            Greenfoot.playSound("lowhealthmusic.wav");
        }
        if(lives==1)
        {
            setImage("halfheart.png");
        }
        if(lives<=0)
        {
            
            setImage("nohearts.png");
            
            Greenfoot.setWorld(new GameOver());
            Greenfoot.playSound("jingles_NES15.wav");
            Greenfoot.delay(12);
            Greenfoot.playSound("jingles_NES15.wav");
            Greenfoot.delay(12);
            Greenfoot.playSound("jingles_NES15.wav");
        }

    }
Super_Hippo Super_Hippo

2022/2/15

#
Start by removing “Town town = (Town)getWorld();” and replace “town.” with “((Town) getWorld()).”.
teobox teobox

2022/2/16

#
Thank you so much!
danpost danpost

2022/2/16

#
teobox wrote...
I have this code in a class that calls a method in the Hearts class once when it is touching Bob
Actually, "once" may not be accurate. Your code will call the method every other frame when touching Bob. That is, for every even number of consecutive frames Bob is being touched, the method will be called half that number of times. As I do not believe that is what was intended. Here is a way to have the method called once each time Bob is initially touched:
public void damageBob()
{
    if (isTouchingBob != isTouching(Bob.class))
    {
        isTouchingBob = ! isTouchingBob;
        if (isTouchingBob)
        {
            ((Town)getWorld()).returnHearts().bobLives(-1);
        }
    }
}
teobox teobox

2022/2/17

#
Thanks again
You need to login to post a reply.