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

2018/5/14

Transferring Variables from one world to the next...

1
2
Shade1177 Shade1177

2018/5/14

#
I have 3 variables in my world Level1, but i don't know of a way to transfer it to my alternate worlds and save the variable. i would just like help on how to accomplish this so that i can have something like score and health transferred between worlds. thanks and i hope this helps... public class Level1 extends World { public int score = 0; public int Life = 4; public int money = 0; /** * Constructor for objects of class Level1. * */ public Level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 800, 1); prepare(); } public void act() { Spawn(); } public void adjustScore(int amount) { score += amount; showText("Score:" + score, 940, 40); } public void adjustMoney(int amount1) { money += amount1; showText("Money:" + money, 940, 780); } public void adjustLife(int amount2) { Life += amount2; } i want to transfer these to my other world Level2
danpost danpost

2018/5/14

#
Shade1177 wrote...
I have 3 variables in my world Level1, but i don't know of a way to transfer it to my alternate worlds and save the variable. i would just like help on how to accomplish this so that i can have something like score and health transferred between worlds. thanks and i hope this helps... << Code Omitted >> i want to transfer these to my other world Level2
Where are you going to your Level2 world?
Shade1177 Shade1177

2018/5/14

#
yea although not immediately. first, I go to a standby shop level that also needs at least two of the variables and then it goes to two. then comes back to the shop and then goes to Level3... etc.
Shade1177 Shade1177

2018/5/14

#
its really just a progression system between levels for variables in general that I'm having trouble with
Yehuda Yehuda

2018/5/14

#
You can have a constructor in your other Worlds with parameters for these values.
private int score;
private int life;
private int money;

public Level2() {
}

public Level2(int score, int life, int money) {
    this.score = score;
    this.life = life;
    this.money = money;
}
Shade1177 Shade1177

2018/5/14

#
although I must ask how in detail would I use that? just so that way I have all the bases checked... (sorry this is my first year and I'm just trying to be thorough, but thank you for helping.)
Yehuda Yehuda

2018/5/14

#
Which details are you missing? What danpost meant is there is no "setWorld" in your class to change to the shop or other levels.
Shade1177 Shade1177

2018/5/14

#
oh thats in a timer code... sorry here ill post it
Yehuda Yehuda

2018/5/14

#
When you want to change to the Level2 (in my example) you can do:
Greenfoot.setWorld(new Level2(500, 3, 112));
Shade1177 Shade1177

2018/5/14

#
public int time; public int timer = 80; /** * Act - do whatever the Timer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { time(); trans(); } public void time() { if(time < 60) { time = time+1; } if(time == 60) { timer--; time=0; } if(this.getWorld().getClass() != Start.class || this.getWorld().getClass() != TheShop.class || this.getWorld().getClass() != GameOver.class) { getWorld().showText("Time:"+ timer,940, 20); } } public void trans() { if(timer == 0) { Greenfoot.setWorld(new TheShop()); } } and yea I know its messy... I was making this on my own really without any help, for the most part, seeing if I could do it myself. just wanted to see if I could challenge myself and it worked...
Yehuda Yehuda

2018/5/14

#
What do you want me to do with that?
Yehuda was going to wrote...
Shade1177 wrote...
here ill post it
Use code tags.
Shade1177 Shade1177

2018/5/14

#
sorry im new here... ill start using them
Shade1177 Shade1177

2018/5/14

#
 public int time;
    public int timer = 80;
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        time();
        trans();
    }    
    public void time()
    {
        if(time < 60)
        {
            time = time+1;
        }
        if(time == 60)
        {
            timer--;
            time=0;
        }
        if(this.getWorld().getClass() != Start.class || this.getWorld().getClass() != TheShop.class || this.getWorld().getClass() != GameOver.class)
        {
            getWorld().showText("Time:"+ timer,940, 20);
        }
    }
    public void trans()
    {
        if(timer == 0)
        {
               Greenfoot.setWorld(new TheShop());
        }
    }
Yehuda Yehuda

2018/5/14

#
I still don't see what you want help with(, but I do see things I do not like about the bit of code you showed).
Shade1177 Shade1177

2018/5/14

#
i just want help with saving my variables into another world. so then I can transfer them between levels and the shop interface.
There are more replies on the next page.
1
2