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

2015/3/22

Counter doesen't increase when item is collected

1
2
3
Project Project

2015/3/26

#
danpost, your first reply I semi understood. I don't really understand what you are saying after the code you wrote, can you please explain it more or give a code example. Also can you explain how I can get the Heart counter to count down because I still can't get it to decrease when the bug is touched by a lizard?
danpost danpost

2015/3/26

#
You gave this code for your MazeWorld constructor earlier:
public MazeWorld()
{    
    super(500, 500, 1);
    levelOne();
    prepare();

    // create variable to hold ScoreBoard class
    ScoreBoard scoreboard = new ScoreBoard("Score:" + 0);
    // constructor for ScoreBoard class
    addObject(scoreboard, 54, 25);
    addObject(new Bug(scoreboard), 46, 443);
    setPaintOrder(Bug.class, Home.class);
     
     
    HeartScore heartscore = new HeartScore("Hearts:" + 3);
    addObject(heartscore, 54, 40);
     
    //setUpHeart(numHeart);
}
Switch lines 11 and 12 with lines 15 and 16. Then make this code change:
// change this line (originally line 11 above)
addObject(new Bug(scoreboard), 46, 443);
// to this
addObject(new Bug(scoreboard, heartscore), 46, 443);
Next, I would like to see your HeartScore class code.
Project Project

2015/3/26

#
Here is my HeartScore class code.
Project Project

2015/3/26

#
public class HeartScore extends Actor
{   
    public HeartScore (String text)
    {
       GreenfootImage img = new GreenfootImage (70, 25);       
       img.drawString(text, 5, 20);
       setImage(img);
    }   
    
    // update score using setText() - see the Bug checkCollision() method
    public void setText(String text) 
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(text, 5, 20);
    }    
}
Project Project

2015/3/26

#
I actually did change the earlier code to what you suggested, I figured out what you meant after I sent my post. I still do need help with the Heart counter though.
danpost danpost

2015/3/26

#
Please show what you now have for your 'touchLizard' method in the Bug class.
Project Project

2015/3/26

#
I just left it like this because it's easier for me to read, and I tried to figure out the counter, and couldn't so I left it blank. But here you go.
public void touchLizard()
    {
        Actor lizardlunch=getOneIntersectingObject(Lizard.class);
        if (lizardlunch !=null)
        {           
           Greenfoot.setWorld(new MazeWorld());           
           
        }
    }    
danpost danpost

2015/3/26

#
Okay. I did not say to change it to that (I only said that what you had, with all the code you had in there, was equivalent to that). The one line you kept is probably the one line you should get rid of (leaving most everything else). Do, however, remove the line 'counter2 = 3;'. At the top of the class where you define the 'counter2' field, use 'private int counter2 = 3;'. You can remove 'MazeWorld w = (MazeWorld) getWorld();' from the middle of the method, also.
Project Project

2015/3/26

#
This is what it looks like now.
public void touchLizard()
        {
            Actor lizardlunch=getOneIntersectingObject(Lizard.class);
            if (lizardlunch !=null)
            {
                setLocation(46, 443);
                getWorld().addObject(new Cherries(), 250, 250);
                Greenfoot.setWorld(new MazeWorld());           
                getWorld().removeObjects(getWorld().getObjects(Home.class));

                
                counter2--;
                //score.setText("Score: " + counter);
                heart.setText("Hearts: " + counter2);
                 
            }
        }
Project Project

2015/3/26

#
Now what do you think needs to be improved to get the counter to work?
danpost danpost

2015/3/26

#
Please remove line 8 from the 'touchLizard' method.
Project Project

2015/3/26

#
Okay, did that, now what?
danpost danpost

2015/3/26

#
Hopefully, now your heart counter should be working.
Project Project

2015/3/26

#
Now it works, but when some code was removed it messed with my score counter that counts the number of cherries eaten .Now it stays with number that was eaten before it was touched by a lizard. I need the score to go back down to zero each time the bug is touched by a lizard, but the heart counter is working great though thank you with that part. I just need a little help with what might be missing now haha.
danpost danpost

2015/3/26

#
Okay. Add the following into the 'touchLizard' method:
counter = 0; // you never had this line that I know of
score.setText("Score: " + counter); // you had this one commented out
There are more replies on the next page.
1
2
3