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

2017/5/15

Keeping counter value over worlds.

OKNEM OKNEM

2017/5/15

#
Hello, My game is a platformer in which a Robot must collect Fuel to power his rocket. The first level is where the Robot travels up a building on platforms to reach the top, where the Rocketship pad is. Along the way, he collects Fuel. My counter successfully records this. However, it doesn't record the counter value over different worlds, as my world changes when the Robot hits the top edge . Is there any way to pass over the counter value? Here's a link to the actual scenario. There's too much code to paste down here. Also, I think there's some stuff I could probably do quicker and faster, without the bulky lengths of code, and if you could suggest some small solutions to these too, that would be great. Thanks!
danpost danpost

2017/5/15

#
When you create a new world, set the counter in the new world to the value of the one in the old world. E.I. (for in robot class)
1
2
3
Myworld2 myworld2 = new Myworld2();
((Counter)myworld2.getObjects(Counter.class).get(0)).setValue(counter.getValue());
Greenfoot.setWorld(myworld2);
OKNEM OKNEM

2017/5/17

#
Awesome, thanks. Good to know I only need one line of code to implement this. :) Also, in the same scenario, is there an easier way to get the fade-out effect to work? Right now I'm using 8 different worlds and having them change on a timer. Is there anything else I could use?
danpost danpost

2017/5/17

#
I am sure you can do it using just one, instead of eight, worlds. You will just need to list, or make an array, of the eight images used (or at least reference each one (except for possibly the first one) somewhere in the code of that one world.
OKNEM OKNEM

2017/5/17

#
danpost wrote...
or make an array
Could you demonstrate? I'm new to this kind of stuff, only just coming out of the basics of it.
danpost danpost

2017/5/17

#
If your eight images were named from "image_1.jpg" to "image_8.jpg", and the number of act frames between changes being 'delay', you could implement the timer as follows:
1
2
3
4
5
6
7
timer++;
if (timer == 8*delay)
{
    proceedToNextWorld()
    return;
}
if (timer%delay == 0) setBackground("image_"+(1+timer/delay)+".jpg");
You can add the 'proceedToNextWorld' method and place the appropriate code in it to initialize and set active the next world.
OKNEM OKNEM

2017/5/17

#
danpost wrote...
to initialize and set active the next world.
So would that be as simple as setImage for Myworld3?
danpost danpost

2017/5/17

#
OKNEM wrote...
danpost wrote...
to initialize and set active the next world.
So would that be as simple as setImage for Myworld3?
Initializing and setting a world active, at minimum requires:
1
Greenfoot.setWorld(new Myworld3());
OKNEM OKNEM

2017/5/17

#
danpost wrote...
Initializing and setting a world active, at minimum requires:
1
Greenfoot.setWorld(new Myworld3());
So I'd still need 8 different worlds for each fade out screen? Also, what would "timer" be in your array demonstration? As in a private int timer = ?
danpost danpost

2017/5/17

#
OKNEM wrote...
So I'd still need 8 different worlds for each fade out screen?
No -- only one fade out world.
what would "timer" be in your array demonstration? As in a private int timer = ?
"timer" would be a field declared in your fade out world class:
1
2
private int timer;
private int delay = 20; // adjust value as needed
OKNEM OKNEM

2017/5/17

#
So, so far I've got this in fadeout world class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class fadeout extends World
{
    private int timer;
    private int delay = 20; // adjust value as needed
    /**
     * Constructor for objects of class fadeout.
     *
     */
    public fadeout()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 426, 1);
    }
 
    public void act()
    {
        if(getObjects(Rocketship.class).size() == 0)
        {
            timer++;
            if (timer == 8*delay)
            {
                proceedToNextWorld();
                return;
            }
            if (timer%delay == 0) setBackground("image_"+(1+timer/delay)+".png");
        }
    }
 
    public void proceedToNextWorld()
    {
        Greenfoot.setWorld(new Myworld3());
    }
}
However, the timer doesn't trigger when the Rocketship exits the screen. I'm guessing it's got something to do with the fact that the fadeout world isn't on-screen or available when this happens? Or is this code supposed to go into the Myworld3 class? I didn't think so --
danpost wrote...
"timer" would be a field declared in your fade out world class
danpost danpost

2017/5/18

#
It is line 17 that needs to be in the old world and is the condition that should set this world active.
OKNEM OKNEM

2017/5/21

#
So I've found that this works, but the worlds are on a loop. I've tried to take what you said and input this into the act method of Myworld3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void act()
    {
        int value = 0;
        int delay = 10;
        if(getObjects(Rocketship.class).size() == 0)
        {
            delay--;
            if(delay == 0)
            {
                value++;
                setBackground("image_"+value+".png");
                delay = 10;
            }
            if(value == 6)
            {
                level2 level2 = new level2();
                Greenfoot.setWorld(level2);
            }
        }       
    }
I've used "value" as the number as I have 6 images to fade out. However, this code doesn't appear to do anything when the rocket disappears. Any idea why?
You need to login to post a reply.