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

2012/11/1

Resetting Static Variables after game

ryanr4788 ryanr4788

2012/11/1

#
I've gotten to the point to where when I click reset my counters go to 0, but the second I hit Run they count right back to the previous games score. Please help, thanks. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class SpaceWorld here. * * @author (your name) * @version (a version number or a date) */ public class SpaceWorld extends World { static final int INTERVAL = 40; long beginTime = System.currentTimeMillis(); public static int Number; /** * Constructor for objects of class SpaceWorld. * */ public SpaceWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 800, 1); resetStaticVariables(); prepare(); } private void resetStaticVariables() { Counter2.value = 0; Counter.value = 0; } public void act() { if ((System.currentTimeMillis() - beginTime) / 12 >= INTERVAL) { addRandomAsteroid(); beginTime = System.currentTimeMillis(); } } private void addRandomAsteroid() { addObject(new Asteroid(), Greenfoot.getRandomNumber(600), -795); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Rocket rocket = new Rocket(); addObject(rocket, 276, 759); rocket.setRotation(270); rocket.setLocation(280, 740); rocket.setLocation(277, 696); Score score = new Score(); addObject(score, 73, 37); score.setLocation(61, 754); Counter counter = new Counter(); addObject(counter, 87, 759); counter.setLocation(87, 751); Missed missed = new Missed(); addObject(missed, 505, 757); Counter2 counter2 = new Counter2(); addObject(counter2, 533, 760); resetStaticVariables(); } private void checkHeight() { } } import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Font; /** * Counter that displays a number. * * @author Michael Kolling * @version 1.0.1 */ public class Counter extends Actor { public static int value = 0; private static int target = 0; private String text; private static int stringLength; public Counter() { this(""); } public Counter(String prefix) { text = prefix; stringLength = (text.length() + 2) * 16; setImage(new GreenfootImage(stringLength, 26)); GreenfootImage image = getImage(); Font font = image.getFont(); image.setFont(font.deriveFont(26.0F)); // use larger font updateImage(); } public void act() { if(value < target) { value++; updateImage(); } else if(value > target) { value--; updateImage(); } } public static void add(int score) { target += score; } public void subtract(int score) { target -= score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1, 18); } }
SPower SPower

2012/11/1

#
You forgot something:
private void resetStaticVariables()
{
    Counter.value = 0;
    Counter.target = 0; // you forgot this
    Counter2.value = 0;
    Counter2.target = 0; // also forgot this!
}
the target value of the standard greenfoot Counter class is the value the counter is animating towards, so that also has to be 0.
You need to login to post a reply.