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

2014/1/6

Scoreboard.

Jabadu Jabadu

2014/1/6

#
I'm programming for my school project. And I decide to make a snake game like "Curve fever". So I made snake, which leaves it's trails behind, and when it crash in oponnent or wall or itself it die. And here comes the problem.. When snake die, the counter gave a new number, and than to start all over again I have to reset the World and by that my Counter resets too... Is there any possible solution to this ? How should I resolve this problem? PS: Sorry for my bad english.
Gevater_Tod4711 Gevater_Tod4711

2014/1/6

#
If you don't want your counter to reset you just need to declare it static. Using this keyword the variable is no longer a object variable (which would be reseted after initialising a new world) but a class variable (which only is reseted after compiling or restarting the game). So if you've got an int that counts up your points you change the declaration of this int like this:
private static int scoreCounter = 0;

//instead of:
private int scoreCounter = 0;
danpost danpost

2014/1/6

#
Gevater_Tod4711 wrote...
a class variable (which only is reseted after compiling or restarting the game).
You will find that even when restarting the game (using the reset button) a static field is not reset. A better way to handle this problem is to set the new counter in the new world to the value of the counter in the old world:
WorldClassName wcn = new WorldClassName();
wcn.getScoreCounter().add(((WorldClassName)getWorld()).getScoreCounter().getValue());
Greenfoot.setWorld(wcn);
Gevater_Tod4711 Gevater_Tod4711

2014/1/6

#
I know that a static field is not resetet when you restart the game using the reset button. What I ment was that when you restart the game by closing greenfoot and starting the project agiain the field is reseted.
danpost danpost

2014/1/6

#
Gevater_Tod4711 wrote...
I know that a static field is not resetet when you restart the game using the reset button. What I ment was that when you restart the game by closing greenfoot and starting the project agiain the field is reseted.
Well, that is the same as compiling again. I thought you were trying to express another way (not the same way) -- sorry.
Jabadu Jabadu

2014/1/6

#
I actually have this counter http://www.greenfoot.org/support_classes/Counter.java. And in world class I have this:
 public Zid()
    {    
        super(1000, 600, 1); 
       
        prepare();
        
        Counter counter = new Counter();
        addObject(counter, 60, 23);
        Pika pika = new Pika(counter);
        addObject(pika, 300, 300);
        
        Counter1 counter1 = new Counter1();
        addObject(counter1, 940, 530);
        counter1.setLocation(153, 22);
        Pika1 pika1 = new Pika1(counter1);
        addObject(pika1, 862, 252);
    }
And than in my actor I have :
public class Pika extends Pikice
{
   
    private  Counter counter;
   
    
    public Pika(Counter pointCounter)
    {
        counter = pointCounter;
       
    }
    
and this:
public void obRobu()
    {
          if (obRobuSveta())
        {
            Greenfoot.stop();
            Greenfoot.setWorld(new Zid());
            counter.add(1);
        }
    }
and this is for finish:
public void konecRunde()
    {
        if(canSee(Pika1.class))
        {
            eat(Pika1.class);
            counter.add(1);
            Greenfoot.setWorld(new Zid());
            zacetekNoveRunde();
        }
        if (counter.getValue() >= 10)
        {
            gameOver();
        }
    }
So where should I actually make changes ?
Jabadu Jabadu

2014/1/8

#
Any idea ?
danpost danpost

2014/1/8

#
You probably need to do something like what I did in my Level Changing Demo scenario. In it, I pass an actor from world to world and, in doing so, other object(s) are automatically passed along with it. The first step will be adding references (instance fields) to your main actors in you world class.
You need to login to post a reply.