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

2020/4/19

World not being set

DaRafster DaRafster

2020/4/19

#
No errors in my code, no problems with timers decrementing, it's just the world not changing. Any ideas of why the world is not setting? World class code (act method is at the bottom - you can see the conditions I put for my if statement):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Galaxy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Galaxy extends World
{
    private GreenfootImage title = new GreenfootImage("Space Rush.png");
    private GreenfootImage bg = getBackground();
    private Score score;
    private Timer timer; 

    private Spacecraft spacecraft;
    private HealthBar healthBar;
    private Picture logo;
    private int asteroidCounter, speedCounter, healthCounter = 0;

    public Galaxy()
    {    
        super(1000, 900, 1); 

        logo = new Picture(title,2,2);
        timer = new Timer();
        spacecraft = new Spacecraft();
        score = new Score();
        healthBar = new HealthBar(300, 20, spacecraft);
        
        prepare();
    }
    
    public Galaxy(Spacecraft sc, HealthBar hb, Picture p, Score s)
    {
        super(1000, 900, 1);
        
        this.spacecraft = sc;
        this.healthBar = hb;
        this.logo = p;
        this.score = s;
        prepare();
    }
    
    private void prepare()
    {
        addObject(logo, getWidth()/2, getHeight()/10);                 
        addObject(timer, getWidth()/2 - getWidth()/3, getHeight()/15);        
        addObject(spacecraft,getWidth()/5,getHeight()/2);        
        addObject(score, getWidth()/2 + getWidth()/3, getHeight()/15);        
        addObject(healthBar, getWidth()/2, getHeight() - getHeight()/22);

        bg.setColor(Color.WHITE);
        bg.drawRect(0, 115, getWidth(), 15);
        bg.drawRect(0, 800, getWidth(), 15);
        bg.setColor(new Color(83, 64, 34));    
        bg.fillRect(0, 116, getWidth(), 14);
        bg.fillRect(0, 801, getWidth(), 14);
    }

    public void scoreOverTime()
    {
        if(timer.getTimeLeft() > 0 && timer.getInitialCount() <= 0)
        {
            score.addScore(1);
        }
    }

    public Score getScore()
    {
        return score;
    }

    /*
     * Adds asteroids into the world, controlling the randomization 
     * and delay between asteroids spawned
     */ 
    public void addingAsteroid()
    {
        asteroidCounter++; 
        if(Greenfoot.getRandomNumber(30) < 1 && asteroidCounter > 10 )
        {
            addObject(new Asteroid(), getWidth(), getRandomNumber(160, 760));
            asteroidCounter = 0;
        }
    }

    /*
     * Adds speed boosts into the world, controlling the randomization
     * and delay between speed boosts spawned
     */
    public void addingSpeedPowerup()
    {
        speedCounter++;
        if(Greenfoot.getRandomNumber(500) < 1 && speedCounter > 350)
        {
            addObject(new Speed(), getWidth(), getRandomNumber(160, 760));
            speedCounter = 0;
        }
    }

    public void addingHealthPowerup()
    {
        healthCounter++;
        if(Greenfoot.getRandomNumber(300) < 1 && healthCounter > 500)
        {
            addObject(new Health(), getWidth(), getRandomNumber(160, 760));
            healthCounter = 0;
        }
    }

    public int getRandomNumber(int start,int end)
    {
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
    
    public int getDelay()
    {
        return timer.getDelayLvl2();
    }
    
    public void act()
    {
        if(timer.getInitialCount() <= 0)
        {
            if(timer.getTimeLeft() > 0)
            {
                addingAsteroid();
                addingSpeedPowerup();
                addingHealthPowerup();
                if(timer.getDelayLvl2() <= 0)
                {
                    Greenfoot.setWorld(new Galaxy(spacecraft, healthBar, logo, score));
                }
            }
        }
        scoreOverTime();
    }   
    
}
And here's the timer class code just in case:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends UI
{
    public int initialCount = 660;
    public int timeLeft = 3660;
    public int delayLvl2 = 320;

    public int getInitialCount()
    {
        return initialCount/60; 
    }

    public int getTimeLeft()
    {
        return timeLeft/60;
    }
    
    public int getDelayLvl2()
    {
        return delayLvl2/60;
    }

    public void displayTimers()
    {
        initialCount--; 
        setImage(new GreenfootImage("GAME STARTS IN: " + getInitialCount(), 25, Color.WHITE, new Color(0,0,0,0)));
        if(initialCount <= 0)
        {
            timeLeft--;
            if (getTimeLeft() == 60)
            {
                setImage(new GreenfootImage("TIME REMAINING: 1:00", 25, Color.WHITE, new Color(0,0,0,0)));
            }
            else if(getTimeLeft() < 60)
            {
                displayTimeLeft();
            }

            if(getTimeLeft() <= 0)
            {
                nextLvlMsg();
                if(getTimeLeft() <= -5)
                {
                    delayLvl2--;
                    nextLvlTime();
                }
            }
        }
    }

    public void displayTimeLeft()
    {
        setImage(new GreenfootImage("TIME REMAINING: " + getTimeLeft(), 25, Color.WHITE, new Color(0,0,0,0)));
    }

    public void nextLvlTime()
    {
        setImage(new GreenfootImage("NEXT LEVEL BEGINS IN: " + getDelayLvl2(), 25, Color.WHITE, new Color(0,0,0,0)));
    }

    public void nextLvlMsg()
    {
        setImage(new GreenfootImage("Level Complete!", 25, Color.WHITE, new Color(0,0,0,0)));
    }

    public void act() 
    {
        displayTimers();
    }
}
RcCookie RcCookie

2020/4/19

#
It’s not setting a new world because timer.getDelayLvl12 is never less it equal to zero. This is because it is not decreasing: it only is if getTimeLeft() is less or equal to 5, but that if statement is only being executed the number of initial counts, and that is less than timeLeft.
Super_Hippo Super_Hippo

2020/4/19

#
A good way to figure out what the problem might be is to print out variables. You can add the following before ‘if(timer.getDelayLvl2() <= 0)‘:
System.out.println(timer.getDelayLvl2());
Is it printing what you expect it should? Found the error? If not, try to find the root of the problem by adding more print statements. You can also do something like the following, so you know what you are printing if you have several print statements:
System.out.println("timer.getDelayLvl2() = "+timer.getDelayLvl2());
danpost danpost

2020/4/19

#
Your Timer class would be a whole lot easier to write if you use just one int value. Start value at 4640 and then after being decremented: -- if value > 3980, set game start image; else -- if value > 320, set time remaining image; else -- if value > 0, set next level image; else set new level
danpost danpost

2020/4/19

#
The following should work while keeping all 3 timer fields:
if (initialCount > 0 && --initialCount > 0) gameStartTime();
else if (timeLeft > 0 && --timeLeft > 0) displayTimeLeft();
else if (delayLvl2 > 0 && --delayLvl2 > 0) nextLvlTime();
else startNextLevel();
I realize you do not yet have a gameStartTime nor a startNextLevel method. But, my point was directed toward the structure of the code; not necessarily the content.
DaRafster DaRafster

2020/4/21

#
danpost wrote...
The following should work while keeping all 3 timer fields:
if (initialCount > 0 && --initialCount > 0) gameStartTime();
else if (timeLeft > 0 && --timeLeft > 0) displayTimeLeft();
else if (delayLvl2 > 0 && --delayLvl2 > 0) nextLvlTime();
else startNextLevel();
I realize you do not yet have a gameStartTime nor a startNextLevel method. But, my point was directed toward the structure of the code; not necessarily the content.
This definitely cleaned up my code, everything works fine. Instead of starting the next level in the timer class, I put in the act method of the Galaxy-class, but now I get an error. Did not get this error before, nor do I know what it necessarily means. I'm getting a null pointer exception error, here is a screen shot of my error: Here is my somewhat updated world-class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Galaxy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Galaxy extends World
{
    private GreenfootImage title = new GreenfootImage("Space Rush.png");
    private GreenfootImage bg = getBackground();
    private Score score;
    private Timer timer; 

    private Spacecraft spacecraft;
    private HealthBar healthBar;
    private Picture logo;
    private int asteroidCounter, speedCounter, healthCounter = 0;

    public Galaxy()
    {    
        super(1000, 900, 1); 

        logo = new Picture(title,2,2);
        timer = new Timer();
        spacecraft = new Spacecraft();
        score = new Score();
        healthBar = new HealthBar(300, 20, spacecraft);
        
        prepare();
    }
    
    public Galaxy(Spacecraft sc, HealthBar hb, Picture p, Score s)
    {
        super(1000, 900, 1);
        
        this.spacecraft = sc;
        this.healthBar = hb;
        this.logo = p;
        this.score = s;
        prepare();
    }
    
    private void prepare()
    {
        addObject(logo, getWidth()/2, getHeight()/10);                 
        addObject(timer, getWidth()/2 - getWidth()/3, getHeight()/15);        
        addObject(spacecraft,getWidth()/5,getHeight()/2);        
        addObject(score, getWidth()/2 + getWidth()/3, getHeight()/15);        
        addObject(healthBar, getWidth()/2, getHeight() - getHeight()/22);

        bg.setColor(Color.WHITE);
        bg.drawRect(0, 115, getWidth(), 15);
        bg.drawRect(0, 800, getWidth(), 15);
        bg.setColor(new Color(83, 64, 34));    
        bg.fillRect(0, 116, getWidth(), 14);
        bg.fillRect(0, 801, getWidth(), 14);
    }

    public void scoreOverTime()
    {
        if(timer.getTimeLeft() > 0 && timer.getInitialCount() <= 0)
        {
            score.addScore(1);
        }
    }

    public Score getScore()
    {
        return score;
    }

    /*
     * Adds asteroids into the world, controlling the randomization 
     * and delay between asteroids spawned
     */ 
    public void addingAsteroid()
    {
        asteroidCounter++; 
        if(Greenfoot.getRandomNumber(25) < 1 && asteroidCounter > 10 )
        {
            addObject(new Asteroid(), getWidth(), getRandomNumber(160, 760));
            asteroidCounter = 0;
        }
    }

    /*
     * Adds speed boosts into the world, controlling the randomization
     * and delay between speed boosts spawned
     */
    public void addingSpeedPowerup()
    {
        speedCounter++;
        if(Greenfoot.getRandomNumber(500) < 1 && speedCounter > 350)
        {
            addObject(new Speed(), getWidth(), getRandomNumber(160, 760));
            speedCounter = 0;
        }
    }

    public void addingHealthPowerup()
    {
        healthCounter++;
        if(Greenfoot.getRandomNumber(300) < 1 && healthCounter > 500)
        {
            addObject(new Health(), getWidth(), getRandomNumber(160, 760));
            healthCounter = 0;
        }
    }

    public int getRandomNumber(int start,int end)
    {
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
    
    public void act()
    {
        int initialCount = timer.getInitialCount();
        int timeLeft = timer.getTimeLeft();
        int delayLvl2 = timer.getDelayLvl2();
        
        if(initialCount <= 0)
        {
            if(timeLeft > 0)
            {
                addingAsteroid();
                addingSpeedPowerup();
                addingHealthPowerup(); 
            }
            if(delayLvl2 == 0)
            {
                Greenfoot.setWorld(new Galaxy(spacecraft, healthBar, logo, score));
            }
        }
        scoreOverTime();
    }   
    
}
Here is my updated timer class which danpost suggested:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends UI
{
    public int initialCount = 660;
    public int timeLeft = 3660;
    public int delayLvl2 = 640;

    public int getInitialCount()
    {
        return initialCount/60; 
    }

    public int getTimeLeft()
    {
        return timeLeft/60;
    }
    
    public int getDelayLvl2()
    {
        return delayLvl2/60;
    }
    
    public void displayTimers()
    {
        if (initialCount > 0 && --initialCount > 0) gameStartTime();
        else if (timeLeft > 0 && --timeLeft > 0) displayTimeLeft();
        else if (delayLvl2 > 0 && --delayLvl2 > 0) nextLvlTime();
    }

    public void displayTimeLeft()
    {
        setImage(new GreenfootImage("TIME REMAINING: " + getTimeLeft(), 25, Color.WHITE, new Color(0,0,0,0)));
    }

    public void nextLvlTime()
    {
        setImage(new GreenfootImage("NEXT LEVEL BEGINS IN: " + getDelayLvl2(), 25, Color.WHITE, new Color(0,0,0,0)));
    }

    public void gameStartTime()
    {
        setImage(new GreenfootImage("GAME STARTS IN: " + getInitialCount(), 25, Color.WHITE, new Color(0,0,0,0)));
    }

    public void act() 
    {
        displayTimers();
    }
}
danpost danpost

2020/4/21

#
You should copy/paste the error message here. I am not familiar with ibb.co and am considering it an untrusted site.
DaRafster DaRafster

2020/4/21

#
Sorry, I no longer need to address this problem. This post took a long time to get approved, so I had to find my own way around it.
You need to login to post a reply.