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

2020/6/17

score counter

JohnBis JohnBis

2020/6/17

#
i want to create score counter which add score when it touch left edge
public class MyWorld extends World
{
public static int countA;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    
    private void prepare()
    {
        Baby1 baby1 = new Baby1();
        addObject(baby1,108,200);
        Baby2 baby2 = new Baby2();
        addObject(baby2,528,207);
        baby2.setLocation(519,205);
        Ball ball = new Ball();
        addObject(ball,310,188);
    }
    
    
    public void act(){
        showText("A"+countA,290,300);
        
}
public void increaseCountA(){
  
    countA=countA+1;
}
}
danpost danpost

2020/6/17

#
Each MyWorld instance should have its own countA field, as if you had two worlds, their counts may be different. This means that the field should not be a static one and you would use the following in an Actor subclass to increase the counter:
((MyWorld)getWorld()).increaseCountA();
You need to login to post a reply.