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

2018/3/26

Variables not resetting when senario stopped, reset then run

Code8Paramedic Code8Paramedic

2018/3/26

#
So I noticed with my game Ambulance Run! that when running it in the greenfoot program if I let the time run out, and the game execute the game over code. when I hit reset and then play it again im getting my time is up message. I tested it out and stopped it at a set time, reset it, and then played and the time was in fact at the same value as when I stopped. I have a couple of theories as to why. My time var is declared and initialized at the top of the code to my world class but is not in the constructor, could that be why?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**.
 * Ambulance Run!!
 * 
 * Drive your ambulance and resuce the injured
 * Eat some burgers 
 * Don't get hit by a car
 * 
 * Flashing ambulance lights added
 * Custom Zombie 
 * Custom payvement background
 * Ambulance Siren added.
 * Multiple car colors added
 * 
 * @author BMR 
 * @version 5.0
 * 
 * ###UPDATE 2/23/18
 * Added text displaying how to play and controls
 * #####
 * 
 * ##UPDATE 3/19/2018
 * Levels have been added
 * Scoring is Manged in the World class
 * cleaning up some code
 */
public class MyWorld extends World
{

    private Counter levelCounter = new Counter("Level: ");
    
    private Counter scoreCounter = new Counter("Score: ");

    private Counter timeCounter = new Counter("Time: ");
    private Counter healthCounter = new Counter("Health: ");
    private Ambulance ambulance = new Ambulance();
    private static int time = 5000;
    private static int level = 1;
    private static int score = 0;
    private static int health = 100;
    private static int y = 1;
    private static int x = 1;
 
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(780, 430, 1); 
        prepare();
        
    }

    /**
     * Create new Floating Burger and Cars at random.
     * 
     * Version 1
     * create burgers and cars 
     * Brett
     */
    public void act()
    {
        showTime();
        showLevel();
        gameStatus();
        showHealth();
        
        if (Greenfoot.getRandomNumber(300) < y)
        {
            addObject(new Burger(), 779, Greenfoot.getRandomNumber(430));

        }
        if (Greenfoot.getRandomNumber(200) < x)
        {
            addObject(new Car(),Greenfoot.getRandomNumber(600) +100, Greenfoot.getRandomNumber(430));
        }
        

    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        setPaintOrder(Counter.class,StatusBar.class);
        StatusBar statusbar = new StatusBar();
        addObject(statusbar, 386, 19);

        
        addObject(ambulance,110,269);
        Zombie zombie = new Zombie();
        addObject(zombie,655,377);
        Car car = new Car();
        addObject(car,601,300);
        Car car2 = new Car();
        addObject(car2,600,250);
        
        addObject(scoreCounter, 77, 20);
        addObject(healthCounter, 556, 20);
        addObject(levelCounter, 205, 20);
        addObject(timeCounter, 702, 20);

        
        Hospital hospital = new Hospital();
        addObject(hospital,704,108);
        
        
        
    }
    /**
     * Add a Game time function
     * 
     */
    private void countTime()
    {
        time--;
           
    } 
    /**
     * Show the time of the game
     * 
     */
    private void showTime()
    {
        countTime();
        timeCounter.setValue(time);
        
        
    }  
    /**
     * Show score of trips to the Hospital
     * 
     */
    public void showScore(int points)
    {
        
        scoreCounter.add(points);
           
    } 
    /**
     * Add to the score
     * 
     */
    public void addScore(int newpoints)
    {
        score = score + newpoints;
        showScore(newpoints);
        
        
    } 
    /**
     * Game Status
     * 
     */
    private void gameStatus()
    {
        
        if (health <= 0)
        {
            gameOver();
            
        } 
        if (time < 1)
        {
            timesUp();
            
        } 
        if (score > 100000)
        {
            youWin();
            
            
        }   
    } 
    /**
     * Keep track of your health
     * 
     */
    public void showHealth()
    {
        healthCounter.setValue(health);
        
    }
    /**
     * Adjust your Health
     * 
     */
    public void addHealth(int newhealth)
    {
        health = health + newhealth;
        healthCounter.add(health);
        
        
    } 
    /**
     * Keep track of your Level
     * 
     */
    public void showLevel()
    {
        levelCounter.setValue(level);
        
    } 
    public void levelUp()
    {
        level++;
        time = 6000;
        x = x + 1;
    }   
    /**
     * Provide Access to the score
     * 
     */
    public int getScore()
    {
        return score;
        
        
    } 
    /**
     * Game Over How the game ends
     * 
     */
    public void gameOver()
    {
        String gameover = "Game Over!! ";
        GameOverScreen gameoverscreen = new GameOverScreen(gameover,score, level);
        addObject(gameoverscreen,780/2,430/2);
        Greenfoot.playSound("game-over.wav");
       
        Greenfoot.stop();
        
    }
    public void youWin()
    {
        String youwin = "You Win!!";
        Greenfoot.playSound("fanfare.wav");
        GameOverScreen gameoverscreen2 = new GameOverScreen(youwin, score, level);
        addObject(gameoverscreen2, 780/2, 430/2);
        Greenfoot.stop();
           
        
        
           
        
    }
    public void timesUp()
    {
        String timesUp = "Time is Up!";
        Greenfoot.playSound("fanfare.wav");
        GameOverScreen gameoverscreen3 = new GameOverScreen(timesUp, score, level);
        addObject(gameoverscreen3, 780/2, 430/2);
        Greenfoot.stop();
        
        
    }   
  
}
danpost danpost

2018/3/26

#
The static fields are created when the project is compiled. You can only "reset" them using program code between compilations. Note that resetting a project does not recompile it. I doubt seriously, however, that any of the fields in your MyWorld class should be static ones. You should not make a field static just to make it easier for other classes to access it. If you had two MyWorld objects side by side (pretend you had two of your games running), would you want them both to always be on the same level or always share the same score? Of course not. You would want them to be independent of each other. That thought process tells you that those particular fields should not be static.
You need to login to post a reply.