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

2024/6/18

score counter dosnt work

JesseJW09 JesseJW09

2024/6/18

#
I've followed a video about how to implement a score counter but the counter doesn't update when i hit the enemies. Here is the world code
{
    public static int score= 0;
    HealthBar healthBar = new HealthBar();
    public BackGround()
    {    
        super(560, 534, 1); 
        prepare();
    }
    
    public HealthBar getHealthBar()
    {
        return healthBar;
    }

    private void prepare()
    {
        addObject (healthBar, 200, 40);
        showText("Score: " + score, 50, 25);
        SlimyBoi slimyBoi = new SlimyBoi();
        addObject(slimyBoi,350,181);
        SlimeBoolet slimeBoolet = new SlimeBoolet();
        addObject(slimeBoolet,260,223);
        slimeBoolet.setLocation(392,197);
        RadishLad radishLad = new RadishLad();
        addObject(radishLad,285,241);
        radishLad.setLocation(132,382);
        slimyBoi.setLocation(128,255);
        removeObject(slimeBoolet);
        addObject(radishLad,183,218);
        radishLad.setLocation(108,203);
        addObject(slimyBoi,110,301);
        slimyBoi.setLocation(107,313);
        a a = new a();
        addObject(a,280,267);
        a.setLocation(232,410);
        a.setLocation(202,399);
        a.setLocation(202,399);
        a.setLocation(498,324);
        a.setLocation(-3,454);
        a.setLocation(204,4);
        a.setLocation(196,267);
        a a2 = new a();
        addObject(a2,196,267);
        a.setLocation(414,220);
        a2.setLocation(11,366);
        a.setLocation(62,0);
        a.setLocation(370,0);
        a a3 = new a();
        addObject(a3,186,318);
        slimyBoi.setLocation(0,168);
        a2.setLocation(234,420);
        a2.setLocation(186,460);
        removeObject(a2);
        a3.setLocation(0,462);
        a3.setLocation(1,447);
        a.setLocation(313,318);
        a.setLocation(294,446);
        a.setLocation(287,434);
        removeObject(a);
        slimyBoi.setLocation(117,291);
        slimyBoi.setLocation(118,346);
        slimyBoi.setLocation(67,286);
        a3.setLocation(459,354);
        slimyBoi.setLocation(118,334);
        a3.setLocation(200,188);
        a3.setLocation(4,176);
        slimyBoi.setLocation(88,313);
        slimyBoi.setLocation(100,301);
    }
}
Bullet code
public class SlimeBoolet extends Actor
{
    public SlimeBoolet()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/18;
        int myNewWidth = (int)myImage.getWidth()/18;
        myImage.scale(myNewWidth, myNewHeight);
    }
    BackGround thisGame;
    public void act()
    {
        World w = getWorld();
        move (5);
        if (isAtEdge())
        {
          w.removeObject(this);
        }
         else
         if(isTouching(Bad.class)) 
        {
           thisGame.score++;
           removeTouching (Bad.class);
           w.removeObject(this);
        }
    }    
}
No errors are showing up so i don't know what's wrong with it.
danpost danpost

2024/6/18

#
JesseJW09 wrote...
I've followed a video about how to implement a score counter but the counter doesn't update when i hit the enemies. << Code Omitted >> No errors are showing up so i don'know what's wrong with it.
In the Background class, remove "static" from line 2. Then add the following line at the end of the prepare method:
slimyBoi.thisGame = this;
You should have been getting a run-time error: NullPointerException, as the field wasn't assigned a value.
JesseJW09 JesseJW09

2024/6/18

#
Its saying that ".thisGame" in the code you sent isn't a variable and i haven't made it one. am i supposed to replace it with another variable that I've already made or do i need to set it up as a new variable for something.
danpost danpost

2024/6/19

#
JesseJW09 wrote...
Its saying that ".thisGame" in the code you sent isn't a variable and i haven't made it one. am i supposed to replace it with another variable that I've already made or do i need to set it up as a new variable for something.
Well, no. Forget my last post. Try this:
public class SlimeBoolet extends Actor
{
    public SlimeBoolet()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = myImage.getHeight()/18;
        int myNewWidth = myImage.getWidth()/18;
        myImage.scale(myNewWidth, myNewHeight);
    }
    
    public void act()
    {
        World w = getWorld();
        move (5);
        if (isAtEdge())
        {
            w.removeObject(this);
            return;
        }
        if(isTouching(Bad.class)) 
        {
            ((Background)w).score++;
            removeTouching (Bad.class);
            w.removeObject(this);
        }
    }    
}
JesseJW09 JesseJW09

2024/6/19

#
It didn't work but its ok because I've found a different method for the score counter. Thanks for the help though!
You need to login to post a reply.