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

2015/1/4

Need help with Score counter resetting after the world changes

ddnezus ddnezus

2015/1/4

#
I have basically made my game change levels when the score counter gets to 12, however when the world changes, the score counter resets and i need a way to keep the Score the same when the world changes.import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Counter here. * * @author (your name) * @version (a version number or a date) */ public class Counter extends Actor { private 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() { // Add your action code here. } public Counter() { setImage( new GreenfootImage("Score:" + score, 25, java.awt.Color.WHITE, java.awt.Color.BLACK)); } public void changeScore(int number) { score = score + number; setImage(new GreenfootImage("Score: "+ score, 25, java.awt.Color.WHITE, java.awt.Color.BLACK)); if(score == 12) { Greenfoot.setWorld(new level2()); } } }
danpost danpost

2015/1/4

#
If you know the score will be 12 when the second level is started, then you can just 'changeScore(12)' to the new counter in the second world constructor. You will, however, need to specify which world should set the next level with 'if (score == 12 && getWorld() instanceof Level1)'.
ddnezus ddnezus

2015/1/4

#
do i have to declare what int the score is in the second world constructor?
ddnezus ddnezus

2015/1/4

#
atm this is the constructor for the second level since i want the platforms to stay the same as the first level
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class level2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class level2 extends CastleWorld
{
   
    /**
     * Constructor for objects of class level2.
     *
     */
    public level2()
    {
 
        prepare();
        
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
      
         
       
    }
    }
danpost danpost

2015/1/4

#
ddnezus wrote...
do i have to declare what int the score is in the second world constructor?
The score is kept in the counter; there is no reason for the second world to retain the value as the counter will do that. Unless you were not clear on what you were asking, that should answer your question.
ddnezus ddnezus

2015/1/4

#
so what should i write in the constructor for the second world?
danpost danpost

2015/1/4

#
It sounds like you basically want the same world for the second level. You may not even have to change worlds. Just reset (by removing and adding whatever objects need doing -- keeping the score counter -- and moving the main actor back to square one) the world. Use the score as an indication of what level you may be on for things that are different in each 'level'.
ddnezus ddnezus

2015/1/4

#
ok ty :)
ddnezus ddnezus

2015/1/4

#
and also another thing: what i do want to change is what all of the actors, such as the coins and the enemyies look like. Is there a possible way for when the background or world changes, the actors also change images?
danpost danpost

2015/1/4

#
You can use the 'addedToWorld(World)' method in those actor classes to get access to the score to determine the level and decide which image to take on. For example:
1
2
3
4
5
6
protected void addedToWorld(World world)
{
    CastleWorld cw = (CastleWorld)world;
    if (cw.score < 12) setImage("level1image.png");
    else if (cw.score < 24) setImage("level2image.png"); // etc.
}
The score field will need to be made 'public'; or you can add a getter method to access its value and use 'if (cw.getScore() < 12)', etc.
ddnezus ddnezus

2015/1/4

#
ok ty
muhammadrafi muhammadrafi

2015/5/5

#
thanks danpost, its very help my project.
You need to login to post a reply.