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

2014/12/9

Creating A New Level When Score Reaches A Certain Number

Retluoc Retluoc

2014/12/9

#
I am trying to get the world to go to a new level when the score reaches a certain number. I have the score counter working, but when it reaches the set number for the score limit, it continues to run the scenario. Any suggestions?
danpost danpost

2014/12/10

#
Inspect the world to see if the Counter reference in it has the same value shown in the Counter object within the world. Report back findings.
Retluoc Retluoc

2014/12/10

#
In the world under Actors, I do not have a Counter object. I have a ScoreHandler...I am sorry if I am not understanding what you are saying, I am just new to Greenfoot.
danpost danpost

2014/12/10

#
That is fine. Please post the code for the world that the ScoreHandler object is placed into. Use the 'code' tag below the reply box to insert code into your post.
Retluoc Retluoc

2014/12/10

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
public class ScoreHandler extends Actor
{
    private static final String scoreLabel = "Score: ";
    private static final String livesLabel = "Lives: ";
    private static int score = 0;
    private static int lives = 0;
     
    private GreenfootImage box;
     
    public ScoreHandler() {
        box = new GreenfootImage (120, 60);
         
        box.setColor(new Color (0f,0f,0f,0.5f));
        box.fillRect (0,0, 200, 60);
 
        updateStatus (0, 0);
    }
     
    public void act() {
        ShooterWorld w = (ShooterWorld)getWorld();
        score = w.getScore();
        lives = w.getLives();
        updateStatus (score, lives);
    }
     
    public void updateStatus (int score, int lives)
    {
        GreenfootImage img = new GreenfootImage (box);
         
        img.setColor (Color.RED);
        img.setFont(new Font("SANS_SERIF", 0, 16));
        img.drawString(scoreLabel + score, 5, 12);
        img.drawString(livesLabel + lives, 5, 30);
         
        setImage (img);
    }
}
This is the ScoreHandler code under Actor
danpost danpost

2014/12/10

#
I was asking for code to the ShooterWorld class, not the ScoreHandler class. However, it may help in determining what is going on. Please post the ShooterWorld class code.
Retluoc Retluoc

2014/12/10

#
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
84
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class ShooterWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class ShooterWorld extends World
{
 
    /**
     * Constructor for objects of class ShooterWorld.
     *
     */
    public int score = 0;
    public int lives = 5;
    private int level;
    public boolean running, spirited;
     
    public ShooterWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1, false);
        prepare();
        level = 0;
        reset();
    }
    public void reset()
    {
        if (score == 25)
        {
          newLevel(); 
        }
    }
 
    public void newLevel()
    {
        level++;
        Greenfoot.setSpeed(44+level/2);
        removeObjects(getObjects(null));
        running = false;
        ScoreHandler scorehandler = new ScoreHandler();
        addObject(scorehandler, 36, 579);
        scorehandler.setLocation(60, 574);
    }
    public void resetMovers()
    {
        running = false;
        removeObjects(getObjects(Plane.class));
        removeObjects(getObjects(Car.class));
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Car car = new Car();
        addObject(car, 411, 575);
        Plane plane = new Plane();
        addObject(plane, 64, 51);
        ScoreHandler scorehandler = new ScoreHandler();
        addObject(scorehandler, 36, 579);
        scorehandler.setLocation(60, 574);
    }
     
    public void addScore() {
        score++;
    }
     
    public int getScore() {
        return score;
    }
     
    public void loseLife() {
        lives--;
    }
     
    public int getLives() {
        return lives;
    }
}
Sorry for the initial misunderstanding.
danpost danpost

2014/12/10

#
Ok. It appears you are using the same World object for all levels (just clearing it of actors and replacing them with new ones for each level). This means that the 'score' and 'lives' values in the world should be accurate at all times (provided the setter methods are used for changing the values). Making those field 'private' would require other classes to use those methods. Now, you have a 'reset' method that appears to be what you are using to perform the check and up the level if needed. However, as far as I can tell, this method is only called once when the world is constructed; and the value of score would be zero at that time. So, as of right now, your code is as if lines 27 through 34 were non-existent. You should be calling 'reset' from an act method within the class.
Retluoc Retluoc

2014/12/10

#
Thank you very much for the help. That helped fix the issue with going to the new level.
You need to login to post a reply.