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

2016/2/24

Change levels when reached a certain score

1
2
Joshua Joshua

2016/2/24

#
Hey guys I was wondering if I could change worlds when reached a certain score but I don't know how. Any tips? I'll paste the code if you want to, just don't know what code is needed to solve this haha
Joshua Joshua

2016/2/24

#
These codes have the score counter in them: The world class which needs to change to another world :
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
34
35
36
37
38
39
40
41
42
43
44
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class world here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class ZombieWorld extends World
{
    /**
     * Constructor for objects of class world.
     *
     */
    Counter counter = new Counter();
    HealthBar healthbar = new HealthBar();
    public ZombieWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 800, 1, false);
        prepare();
    }
 
    public Counter getCounter()
    {
        return counter;
 
    }
 
    public HealthBar getHealthBar()
    {
        return healthbar;
 
    }
 
    private void prepare() {
        ZombieSpawner spawner = new ZombieSpawner();
        addObject(spawner, 0, 0);
        Survivor survivor = new Survivor();
        addObject(survivor, 600, 600);
        addObject(counter, 61, 26);
        addObject(healthbar, 174, 26);
    }
}
The counter code:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }   
    public void addScore()
    {
        score ++;
         
         
    }
}
And the code from the zombie which it counts 1 when it hits the bullet:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class zombie here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Zombie extends WorldActors
{
    GreenfootImage zombie1 = new GreenfootImage("zombie1.png");
    GreenfootImage zombie2 = new GreenfootImage("zombie2.png");
    GreenfootImage zombie3 = new GreenfootImage("zombie3.png");
    public int frame = 1;
    public int animationCounter = 0;
    public int speed = 1;
 
    /**
     * Act - do whatever the zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        animationCounter ++;
        if(animationCounter % 6 == 0)
            animate();
 
        chaseSurvivor();
        die();
    }   
 
    public void chaseSurvivor()
    {
        turnTowards(Survivor.survivorX, Survivor.survivorY);
        move(speed);
 
    }
 
    public void animate()
    {
        if(frame == 1)
        {
            setImage(zombie1);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage(zombie2);
            frame = 3;
        }
        else if(frame == 3)
        {
            setImage(zombie3);
            frame =1;
        }
    }
 
    public void die() {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        Actor explosion = getOneIntersectingObject(Explosion.class);
        if(bullet!=null)
        {
            World myWorld = getWorld();
            getWorld().removeObject(bullet);
            ZombieWorld zombieworld = (ZombieWorld)myWorld;
            Counter counter = zombieworld.getCounter();
            counter.addScore();
            getWorld().addObject(new Splat(), getX(), getY());
            getWorld().removeObject(this);     
        }
 
        else if(explosion != null)
        {
            World myWorld = getWorld();
            ZombieWorld zombieworld = (ZombieWorld)myWorld;
            Counter counter = zombieworld.getCounter();
            counter.addScore();
            getWorld().addObject(new Splat(), getX(), getY());
            getWorld().removeObject(this);    
 
        }
    }
}
I really hope someone can help me with this :)
danpost danpost

2016/2/24

#
Basically, you would code something like (but not specifically) this:
1
if (score == someValue) Greenfoot.setWorld(new NextWorld());
Joshua Joshua

2016/2/24

#
Do I need to put that in the world method? Because when I tried that it didn't recognize the score variable.
danpost danpost

2016/2/24

#
Joshua wrote...
Do I need to put that in the world method? Because when I tried that it didn't recognize the score variable.
You can place this at the end of the act method in the class where the score is located -- or, even better, immediately after the incrementing of the score..
Joshua Joshua

2016/2/24

#
It works now, thanks for your help!
Joshua Joshua

2016/2/24

#
Now something else does not work in my second level. When I kill a zombie there, it denies to kill it with the 'die' method. I see that the problem is that it only says ZombieWorld, but I want to make it work for ZombieWorld2 as well, I just don't know how. Does anyone knows? Code:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class zombie here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Zombie extends WorldActors
{
    GreenfootImage zombie1 = new GreenfootImage("zombie1.png");
    GreenfootImage zombie2 = new GreenfootImage("zombie2.png");
    GreenfootImage zombie3 = new GreenfootImage("zombie3.png");
    public int frame = 1;
    public int animationCounter = 0;
    public int speed = 1;
 
    /**
     * Act - do whatever the zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        animationCounter ++;
        if(animationCounter % 6 == 0)
            animate();
 
        chaseSurvivor();
        die();
    }   
 
    public void chaseSurvivor()
    {
        turnTowards(Survivor.survivorX, Survivor.survivorY);
        move(speed);
 
    }
 
    public void animate()
    {
        if(frame == 1)
        {
            setImage(zombie1);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage(zombie2);
            frame = 3;
        }
        else if(frame == 3)
        {
            setImage(zombie3);
            frame =1;
        }
    }
 
    public void die() {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        Actor explosion = getOneIntersectingObject(Explosion.class);
        if(bullet!=null)
        {
            World myWorld = getWorld();
            getWorld().removeObject(bullet);
            ZombieWorld zombieworld = (ZombieWorld)myWorld;
            Counter counter = zombieworld.getCounter();
            counter.addScore();
            getWorld().addObject(new Splat(), getX(), getY());
            getWorld().removeObject(this);     
        }
 
        else if(explosion != null)
        {
            World myWorld = getWorld();
            ZombieWorld zombieworld = (ZombieWorld)myWorld;
            Counter counter = zombieworld.getCounter();
            counter.addScore();
            getWorld().addObject(new Splat(), getX(), getY());
            getWorld().removeObject(this);    
 
        }
    }
}
Joshua Joshua

2016/2/24

#
Does anyone know?*
danpost danpost

2016/2/24

#
You will need to determine what type world the counter is in so you can access the proper 'getCounter' method
1
2
3
Counter counter = null;
if (myWorld instanceof ZombieWorld) counter = ((ZombieWorld)myWorld).getCounter();
else if (myWorld instanceof ZombieWorld2) counter = ((ZombieWorld2)myWorld).getCounter();
Joshua Joshua

2016/2/24

#
Thanks for that as well :)
Joshua Joshua

2016/2/24

#
One more question: If I complete level 1, it goes to a 'ScreenInbetween" world which basically just says "Congratulations with completing level one! Press enter to start level 2" and it goes to level 2. But when I reach a score of 30 in level 2 it goes back to the same "ScreenInbetween", but I want it to go to a world which says: "Congratulations. Press enter to go to level 3!". Does this involve the instanceof as well? I tried some coding but it didn't really work. Code :
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
        if(score == 30) Greenfoot.setWorld(new ScreenInbetween());
    }   
    public void addScore()
    {
        score ++;
         
         
    }
}
danpost danpost

2016/2/24

#
Being the ScreenInbetwen object is a separate world, you will need to give it some indication as to what type world to proceed to. Maybe creating and passing the next world to the in-between world might be easiest. And yes, to determine the next world, you would have to determine the type of the current world. Another way to go about it, which might be easier, is to track the level number in a class ('static') field (possibly in your initial ZombieWorld class. But, even better, as far as structure and ease of use, might be to subclass all your levels with a superclass between them and the World class which can contain the shared methods and fields required for controlling the specific worlds. Refer to my Super Level Support Class scenario to see how this is done.
Joshua Joshua

2016/2/24

#
Here's how I fixed it:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
        World myWorld = getWorld();
        if(score == 30 && myWorld instanceof ZombieWorld) Greenfoot.setWorld(new ScreenInbetween());
        else if(score == 30 && myWorld instanceof ZombieWorld2) Greenfoot.setWorld(new ScreenInbetween2());
        else if(score == 30 && myWorld instanceof ZombieWorld3) Greenfoot.setWorld(new EndScreen());
    }   
    public void addScore()
    {
        score ++;
         
         
    }
}
It works well for me
Joshua Joshua

2016/2/24

#
Thanks for all your help thought danpost!
Joshua Joshua

2016/2/24

#
Now everything works but my background music restarts every time a new world opens but the music that was playing already keeps playing, so you hear 2 of the same songs at the same time, but not in sync. Now I don't know how to stop the music from playing when a new world is opened. Here's the code for the starting world:
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
34
35
36
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class StartScreen here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class StartScreen extends World
{
    /**
     * Constructor for objects of class StartScreen.
     *
     */
    public StartScreen()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 800, 1);
        GreenfootSound backgroundMusic = new GreenfootSound("GameMusic.mp3");
        backgroundMusic.playLoop();
        prepare();
    }
    private void prepare()
    {
        TitleLetters titleletters = new TitleLetters();
        addObject(titleletters, 600, 400);
    }
     
    public void act()
    {
        if(Greenfoot.isKeyDown("enter"))
        Greenfoot.setWorld(new ZombieWorld());
        else if(Greenfoot.isKeyDown("c"))
        Greenfoot.setWorld(new Controls());
    }
}
And here's the world that it's going to when pressed enter:
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
34
35
36
37
38
39
40
41
42
43
44
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class world here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class ZombieWorld extends World
{
    /**
     * Constructor for objects of class world.
     *
     */
    Counter counter = new Counter();
    HealthBar healthbar = new HealthBar();
    public ZombieWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 800, 1, false);
        prepare();
    }
 
    public Counter getCounter()
    {
        return counter;
 
    }
 
    public HealthBar getHealthBar()
    {
        return healthbar;
 
    }
 
    private void prepare() {
        ZombieSpawner spawner = new ZombieSpawner();
        addObject(spawner, 0, 0);
        Survivor survivor = new Survivor();
        addObject(survivor, 600, 600);
        addObject(counter, 61, 26);
        addObject(healthbar, 174, 26);
    }
}
Any tips on how to fix that as well?
There are more replies on the next page.
1
2