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

2016/4/12

How to continue Counter score to different worlds and make a leaderboard score when win or lose?

1
2
Sir_brandon_ Sir_brandon_

2016/4/12

#
I have no clue on how to contine the counter score into different worlds. here is my world 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class CrabWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Playerx1 extends World
{
    public Counter counter; //save the counter in a field to get access from outside
    /**
     * Constructor for objects of class CrabWorld.
     *
     */
    public Playerx1()
    {
        super(1000, 660, 1);
        prepare();
 
    }
 
    public void act()
    {
         
        if (getObjects(Player_p1.class).isEmpty()){
            Greenfoot.setWorld(new GameOverScreen());
        }
        if (getObjects(Peasant.class).isEmpty()){
            Greenfoot.setWorld(new level_2());
        }
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
 
 
        //when creating
        counter = new Counter();
        addObject(counter, 54, 624);
 
        Player_p1 player_p1 = new Player_p1(counter);
        addObject(player_p1, 240, 304);
here is my 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
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
85
86
87
88
89
90
91
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * A simple counter with graphical representation as an actor on screen.
 *
 * @author mik
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
 
    private int score = 0; //declare 'score' as private
 
    public int getScore() //to be able to access this field from outside this class
    {
        return score;
    }
 
    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
 
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act()
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
         
    }
 
    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }
 
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
 
    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
 
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("Score : " + value, 22, Color.GREEN, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
            (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
 
 
 
}
and here is my player 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Player_p1 extends Actor
{
    private Counter counter;
 
    public Player_p1(Counter pointCounter)
    {
        counter = pointCounter;
    }
 
    public void act()
    {
        moveAndTurn();
        eat();
 
    }
 
    public void moveAndTurn()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            turn(-7);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            turn(7);
        }
        if (Greenfoot.isKeyDown("w"))
        {
            move(7);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-7);
        }
    }
 
    public void eat()
    {
        Actor peasant;
        peasant = getOneObjectAtOffset(0, 0, Peasant.class);
        if (peasant != null)
        {
            World world;
            world = getWorld();
            world.removeObject(peasant);
            counter.add(1);
            Greenfoot.playSound("eating.wav");
        }
         
         
    }
}
also here is my GameOverScreen
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
import greenfoot.*;
 
/**
 * Write a description of class GameOverScreen here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GameOverScreen extends World
{
 
    /**
     * Constructor for objects of class GameOverScreen.
     *
     */
    public GameOverScreen()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 624, 1);
         
        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()
    {
 
        GameOverText gameovertext = new GameOverText();
        addObject(gameovertext, 309, 175);
        gameovertext.setLocation(300, 176);
        gameovertext.setLocation(490, 197);
 
        Back_button back_button = new Back_button();
        addObject(back_button, 494, 456);
        back_button.setLocation(479, 451);
    }
}
danpost danpost

2016/4/12

#
You can pass the counter to the Level_2 world just like you passed it to the player character. For leaderboard, there is a ScoreBoard class that you can import if you are going to be using UserInfo storage for saving player scores.
Sir_brandon_ Sir_brandon_

2016/4/12

#
Can you show me an example plz? Edit: for the passing through to level 2
danpost danpost

2016/4/12

#
Sir_brandon_ wrote...
Can you show me an example plz?
The example (not passing to Level_2 world however) is contained on lines 5 through 10 of the Player_p1 class used by your Playerx1 world at line 46. The Counter object created in your world is passed to the player, who saves it in its own field. You would do pretty much the same thing. Pass the counter (again), but this time from the world (at line 30) to the Level_2 world.
Sir_brandon_ Sir_brandon_

2016/4/12

#
so with this
1
2
3
4
5
6
7
8
public class Player_p1 extends Actor
{
    private Counter counter;
  
    public Player_p1(Counter pointCounter)
    {
        counter = pointCounter;
    }
So on lines 1, 3, 5, 7 what replaces counter and does anything change to do with the Player_p1 also with this what do i replace in it
1
2
Player_p1 player_p1 = new Player_p1(counter);
        addObject(player_p1, 240, 304);
. and with this
DanPost wrote...
You would do pretty much the same thing. Pass the counter (again), but this time from the world (at line 30) to the Level_2 world
what do you mean?
danpost danpost

2016/4/12

#
The code I mentioned (that which you have just posted) was just an example of passing the object from an object of one class to another. You should not change anything dealing with the Player_p1 class or any object it creates. It is line 30 of the Playerx1 class and the code in your Level_2 world class that need worked on.
Sir_brandon_ Sir_brandon_

2016/4/12

#
what do i need to do with it
danpost danpost

2016/4/12

#
The code you posted is an example of what you need to do. Make line 30 pass the counter to the Level_2 world being created just like line 46 passes the counter to the created Player_p1 object. Then change the code in your Level_2 class to accept the counter object and save it to a field of its own (just like the Player_p1 class does between line 5 and 10).
Sir_brandon_ Sir_brandon_

2016/4/12

#
1
2
counter = new Counter(level_2);
        addObject(counter, 54, 624);
So like that
danpost danpost

2016/4/12

#
Sir_brandon_ wrote...
1
2
counter = new Counter(level_2);
        addObject(counter, 54, 624);
So like that
No. You are not wanting to create a new counter. You want the one you already have (the one that has the current score).
Sir_brandon_ Sir_brandon_

2016/4/13

#
so like this
1
2
counter = new Counter(level_2);
        getObject(counter, 54, 624);
Edit: i dont really know how or what code to place
danpost danpost

2016/4/13

#
Sir_brandon_ wrote...
so like this < Code Omitted > Edit: i dont really know how or what code to place
Let me put it this way: you need to pass the Counter object to the Level_2 world -- not the Level_2 world to the Counter object. You need to use the following line of code for line 30 above in the Playerx1 class:
1
Greenfoot.setWorld(new Level_2(counter));
Then your Level_2 class code needs to be adjusted to receive the counter and save it in a field (just like the player does when it receives it). If you are creating a new player actor in the Level_2 world, you will need to pass the counter again to it.
Sir_brandon_ Sir_brandon_

2016/4/13

#
ok ive done that code into my world like this
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class CrabWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Playerx1 extends World
{
    public Counter getCounter()
    {
        return counter;
    }
    public Counter counter; //save the counter in a field to get access from outside
    /**
     * Constructor for objects of class CrabWorld.
     *
     */
    public Playerx1()
    {
        super(1000, 660, 1);
        prepare();
 
    }
 
    public void act()
    {
        if (getObjects(Player_p1.class).isEmpty()){
            Greenfoot.setWorld(new GameOverScreen());
        }
        if (getObjects(Peasant.class).isEmpty()){
            Greenfoot.setWorld(new level_2(counter));
        }
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
 
        //when creating
        counter = new Counter();
        addObject(counter, 54, 624);
 
        Player_p1 player_p1 = new Player_p1(counter);
        addObject(player_p1, 240, 304);
but now its getting an error saying constructor level_2 in class level_2 cannot be applied to given types; required: no arguments; found: Counter; reason: actual and formal argument lists differ in length whats happening
danpost danpost

2016/4/13

#
Sir_brandon_ wrote...
ok ive done that code into my world like this < Code Omitted > but now its getting an error saying constructor level_2 in class level_2 cannot be applied to given types; required: no arguments; found: Counter; reason: actual and formal argument lists differ in length whats happening
danpost wrote...
Then your Level_2 class code needs to be adjusted to receive the counter and save it in a field (just like the player does when it receives it).
Sir_brandon_ Sir_brandon_

2016/4/13

#
k fixed it but now when i compile the worlds it wont let me open them and the boxes in both actor and world have lines like this ///////// through them
There are more replies on the next page.
1
2