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

2014/6/4

How to remember certain int's after new worlds

1
2
3
danpost danpost

2014/6/6

#
OK. Change it back to 'public BackgroundLvL1()'. You will not be passing anything to this world. You will be passing this world through other worlds.
SAAEngineer SAAEngineer

2014/6/6

#
Will telll you the outcome as soon as i can!
SAAEngineer SAAEngineer

2014/6/6

#
I tried changing it back but now i get an error in Countdown_Go , where it has to launch the world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Countdown1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Countdown_Go extends World
{
    public int countdowndelay = 40;
    public int countdownteller = countdowndelay;
    GreenfootSound go = new GreenfootSound("go.mp3");
    private World previousWorld;  
    // add instance field
    /**
     * Constructor for objects of class Countdown1.
     * 
     */
    public Countdown_Go()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(480, 800, 1);
        go.play();
    }
    public void act(){
       if (countdownteller > 0) {
           countdownteller--;
    }
    if (countdownteller == 0) {
           Greenfoot.setWorld(previousWorld); 
    }
   
}}
On rule 31 i get the error: java.lang.NullPointerException: The given world cannot be null.
danpost danpost

2014/6/6

#
This is one of the world you need to pass the BackgroundLvL1 world to:
// change line 20 to
public Countdown_Go(World world)
// and insert at line 24
previousWorld = world;
Your Countdown_1 world will have to pass it using 'new Countdown_go(previousWorld)' when creating this world.
SAAEngineer SAAEngineer

2014/6/6

#
the transition from countdown_1 to countdown_go is perfectly fine, it's the transition from Countdown_go to BackgroundLvL1 that's the problem
danpost danpost

2014/6/6

#
The transition may be fine; but, the needed field value is not!
SAAEngineer SAAEngineer

2014/6/6

#
then i get the error in Countdown_1 and if we change that we'll probably get it in Countdown_2 aswell.
danpost danpost

2014/6/6

#
The BackgroundLvL1 class should instantiate a Countdown3 world object with 'new Countdown3(this)', which passes itself to the Countdown3 class object created; the Countdown3 object should store the value and pass it to the Countdown2 world object when it is created; this world object should do the same to pass it to Countdown1 and then to Countdown_go, where the passed world object will finally be returned from.
danpost danpost

2014/6/6

#
SAAEngineer wrote...
then i get the error in Countdown_1 and if we change that we'll probably get it in Countdown_2 aswell.
What error are you getting and what is the code you are using there.
SAAEngineer SAAEngineer

2014/6/6

#
danpost wrote...
The BackgroundLvL1 class should instantiate a Countdown3 world object with 'new Countdown3(this)', which passes itself to the Countdown3 class object created; the Countdown3 object should store the value and pass it to the Countdown2 world object when it is created; this world object should do the same to pass it to Countdown1 and then to Countdown_go, where the passed world object will finally be returned from.
Can you give me the exact lines pleas?
danpost danpost

2014/6/6

#
SAAEngineer wrote...
< Quote Omitted > Can you give me the exact lines pleas?
If you show the classes, I can point out the lines.
SAAEngineer SAAEngineer

2014/6/6

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Countdown1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Countdown3 extends World
{
    public int countdowndelay = 40;
    public int countdownteller = countdowndelay;
    GreenfootSound beep = new GreenfootSound("beep.mp3");
    /**
     * Constructor for objects of class Countdown1.
     * 
     */
    public Countdown3()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(480, 800, 1);
        beep.play();
    }
    public void act(){
       if (countdownteller > 0) {
           countdownteller--;
    }
    if (countdownteller == 0) {
           Greenfoot.setWorld(new Countdown2()); 
    }
}} 
All Countdown classes are the same
danpost danpost

2014/6/6

#
Add 'World world' inside the parenthesis of line 18 (in all Countdown classes). Insert 'previousWorld = world;' at line 23 (inside the constructor block of all Countdown classes). Insert 'private World previousWorld;' at line 14 (in all Countdown classes).
SAAEngineer SAAEngineer

2014/6/7

#
Okay i added everything but now in BackgroundLvL1 i get the error : on this line:
Greenfoot.setWorld(new Countdown3());
danpost danpost

2014/6/7

#
Again, you need to pass the BackgroundLvL1 world on to the next world.
Greenfoot.setWorld(new Countdown3(previousWorld));
You need to login to post a reply.
1
2
3