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

2023/6/18

Issues with transfering values of counters

1
2
Kolala Kolala

2023/6/18

#
Im programming a game for a school project where you collect coins. In the "Game world" you collect the coins and the counter goes up. When you die, I want the score to be transfered to the counter in my main menu but I have no idea how im supposed to do that. Also I want it to add the count so for example when I already have 10 collected coins and it displays that in the menu, then I collect 10 more coins in the game, I want the counter in the main menu to say 20 if you know what I mean, thanks in advance!
danpost danpost

2023/6/18

#
Kolala wrote...
In the "Game world" you collect the coins and the counter goes up. When you die, I want the score to be transfered to the counter in my main menu but I have no idea how im supposed to do that. Also I want it to add the count so for example when I already have 10 collected coins and it displays that in the menu, then I collect 10 more coins in the game, I want the counter in the main menu to say 20
With the following, you will only create one counter for the main menu world (by making it static). The second constructor (lines 10 to 16) is added to allow the score to be passed to any new main menu world and added to the counter: The first constructor is reduced to calling the second one without changing the score (passing a zero value):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static Counter counter;
 
// other fields
 
public MainMenuWorld()
{
    this(0};
}
 
public MainMenuWorld(int score)
{
    super(600, 400, 1);
    if (counter == null) counter = new Counter();
    counter.add(score);
    prepare();
}
 
// etc.
When going to a main menu world from the game world, use something like the following (game world code):
1
2
3
int score = counter.getScore();
MainMenuWorld mmw = new MainMenuWorld(score);
Greenfoot.setWorld(mmw);
There might have been a way that would be slightly easier, but because you have not provided any codes, this is general enough to work whatever your code is. You may also need to add the following line in your starting world:
1
MainMenuWorld.counter = null;
-- that is, if it is not a main menu world. If your initial world is a main menu world, then more will need done. Let me know if that is the case.
Kolala Kolala

2023/6/18

#
Yea, the main menu world is the world you start in. This is the code from the Main menu counter, I tried to do it with the method "scoreTransfer()" but it didnt work.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value = 0;
    private int target = 0;
    private String prefix;
     
    public Counter()
    {
        this(new String());
    }
 
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
     
    public void act()
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
         
        scoreTransfer();
    }
     
    public void increment() {
        value++;
    }
     
    public void add(int score)
    {
        target += score;
    }
     
    public int getValue()
    {
        return target;
    }
     
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
     
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }
 
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
         
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
         
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
     
    public void scoreTransfer() {
          MuenzCounter object = (MuenzCounter)getOneIntersectingObject(MuenzCounter.class);
          value = value + object.getValue();
    }
}
The second counters 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
92
93
94
95
96
97
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class MuenzCounter extends Actor
{
    private static final Color transparent = new Color(3,0,0,0);
    private GreenfootImage background;
    private int value = 0;
    private int target = 0;
    private String prefix;
     
    public MuenzCounter()
    {
        this(new String());
    }
 
    /**
     * Create a new counter, initialised to 0.
     */
    public MuenzCounter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        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.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }
 
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }
 
    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
     
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }
 
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
         
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
         
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Main menu world:
1
2
3
4
5
6
7
8
9
10
11
12
13
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Menu extends World
{
    public Menu()
    {   
        super(1000, 500, 1);
        addObject(new Play(), 500, 250);
        addObject(new shop(), 800, 250);
        addObject(new levels(), 200, 250);
        addObject(new Counter(), 500, 50);
    }
}
And the Game world: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class JumpWorld extends World { public int i; public int k; public int x; GreenfootImage background; private int backgroundX; public void act() { CoinSpawn(); PlatformSpawn(); drawBackground(); } public JumpWorld() { super(1000, 500, 1, false); background = new GreenfootImage("scrolling_city.png"); this.getBackground().drawImage(background, 0,0); addObject(new character(), 100, 300); addObject(new Platform(), 50, 450); addObject(new Platform(), 150, 450); addObject(new Platform(), 250, 450); addObject(new Platform(), 370, 450); addObject(new Platform(), 550, 450); addObject(new Platform(), 700, 450); addObject(new Platform(), 850, 450); addObject(new Platform(), 1000, 450); prepare(); } public void randomMuenze(int howMany) { for(int i=0; i<howMany; i++) { Muenze clover = new Muenze(); int x = Greenfoot.getRandomNumber(getWidth()); int y = 100; addObject(clover, x, y); } } private void prepare() { MuenzCounter counter = new MuenzCounter(); addObject(counter,53,25); } private void CoinSpawn() { if(i == 60) { addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30); addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30); addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30); addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30); addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30); i = 0; } else { i++; } } private void PlatformSpawn() { if(k == Greenfoot.getRandomNumber(20)) { addObject(new Platform(), 1100, 450); k = 0; } else { k++; } if(k == 25) { k = 0; } } public void changeBackgroundX(int ChangeX) { backgroundX = backgroundX - ChangeX; } public void drawBackground() { this.getBackground().drawImage(background,backgroundX, 0); } }
Kolala Kolala

2023/6/18

#
Oops game world not in code heres it again:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class JumpWorld extends World
{
    public int i;
    public int k;
    public int x;
    GreenfootImage background;
    private int backgroundX;
    public void act() {
        CoinSpawn();
        PlatformSpawn();
        drawBackground();
    }
     
    public JumpWorld()
    {   
        super(1000, 500, 1, false);
         
        background = new GreenfootImage("scrolling_city.png");
        this.getBackground().drawImage(background, 0,0);
         
        addObject(new character(), 100, 300);
        addObject(new Platform(), 50, 450);
        addObject(new Platform(), 150, 450);
        addObject(new Platform(), 250, 450);
        addObject(new Platform(), 370, 450);
        addObject(new Platform(), 550, 450);
        addObject(new Platform(), 700, 450);
        addObject(new Platform(), 850, 450);
        addObject(new Platform(), 1000, 450);
        prepare();
    }
     
    public void randomMuenze(int howMany)
    {
        for(int i=0; i<howMany; i++) {
            Muenze clover = new Muenze();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = 100;
            addObject(clover, x, y);
        }
    }
     
    private void prepare()
    {
        MuenzCounter counter = new MuenzCounter();
        addObject(counter,53,25);
    }
     
    private void CoinSpawn() {
        if(i == 60) {
            addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30);
            addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30);
            addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30);
            addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30);
            addObject(new Muenze(), Greenfoot.getRandomNumber(990), -30);
            i = 0;
        }
        else {
            i++;
        }
    }
     
    private void PlatformSpawn() {
        if(k == Greenfoot.getRandomNumber(20)) {
            addObject(new Platform(), 1100, 450);
            k = 0;
        }
        else {
            k++;
        }
         
        if(k == 25) {
            k = 0;
        }
             
    }
     
    public void changeBackgroundX(int ChangeX) {
        backgroundX = backgroundX - ChangeX;
    }
     
    public void drawBackground() {
        this.getBackground().drawImage(background,backgroundX, 0);
    }
     
     
}
danpost danpost

2023/6/19

#
Kolala wrote...
Yea, the main menu world is the world you start in.
And the main menu world code is the one you don't give.
Kolala Kolala

2023/6/19

#
I did send it
Kolala Kolala

2023/6/19

#
Just scroll up a bit its above the JumpWorld code I messed up
danpost danpost

2023/6/19

#
Kolala wrote...
Just scroll up a bit its above the JumpWorld code I messed up
Oops -- sorry I missed that. Try this for your Menu 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
import greenfoot.*;
 
public class Menu extends World
{
    private static Counter counter;
     
    public Menu()
    {
        this(counter = new Counter());
    }
     
    private Menu(Counter counter)
    {
        this(0);
    }
     
    public Menu(int score)
    {
        super(1000, 500, 1);
        addObject(new Play(), 500, 250);
        addObject(new shop(), 800, 250);
        addObject(new levels(), 200, 250);
        addObject(counter, 500, 50);
        counter.add(score);
    }
}
This seems to be a good way to accomplish what you need even though it is like going through hoops to get there. Remove the scoreTransfer method from your Counter class; then, delete the entire MuenzCounter class and replace all "MuenzCounter" with "Counter" in all classes. Always pass an int value when creating a Menu world. The constructor with no parameters is reserved for when you reset the program and your initial world is created. The constructor starting on line 12 is the first case (for me) where a parameter is given for no "apparent" reason; but, the reason was to facilitate the creating and assigning of the Counter object to the static field and still call the constructor with the int parameter.
Kolala Kolala

2023/6/21

#
I did everything as you said, the counter does work when Im in game but doesnt add that score to the menu world. When I create a Menu world manually and give it an int value, the counter does go up by that amount when I run it, but after I get coins in the game world, the counter resets to 0.
danpost danpost

2023/6/21

#
Kolala wrote...
I did everything as you said, the counter does work when Im in game but doesnt add that score to the menu world. When I create a Menu world manually and give it an int value, the counter does go up by that amount when I run it, but after I get coins in the game world, the counter resets to 0.
What does your character class look like? (provide codes)
Kolala Kolala

2023/6/21

#
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import greenfoot.*;    // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List; // (List)
 
/**
 * Write a description of class Kara here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class character extends Actor
{
    int cloversEaten = 0;
    private int vSpeed = 0;
    public int frame = 1;
    public int animationSpeed;
    GreenfootImage bmls = new GreenfootImage("basic_man_left_stand.png");
    GreenfootImage bmlr = new GreenfootImage("basic_man_left_run.png");
    GreenfootImage bmrs = new GreenfootImage("basic_man_right_stand.png");
    GreenfootImage bmrr = new GreenfootImage("basic_man_right_run.png");
     
    /**
     * Act - do whatever the Kara wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public void act()
    {
        JumpWorld meineWelt;
        meineWelt = (JumpWorld)this.getWorld();
        if(Greenfoot.isKeyDown("d")){
            this.setLocation(this.getX() + 5, this.getY() + 0);
            if(animationSpeed % 3 == 0) {
            animateRight();
            }
        }
         
        if(Greenfoot.isKeyDown("a")){
            this.setLocation(this.getX() - 5, this.getY() + 0);
            if(animationSpeed % 3 == 0 && onGround() == true) {
            animateLeft();
            }
        }
         
        if(Greenfoot.isKeyDown("space")) {
            if(this.onGround() == true || this.getY() > 450) {
                this.jump();
            }
        }
         
        if(Greenfoot.isKeyDown("a")) {
            if(this.onGround() == false) {
                setImage("basic_man_left.png");
            }
        }
         
        if(Greenfoot.isKeyDown("d")) {
            if(this.onGround() == false) {
                setImage("basic_man_right.png");
            }
        }
         
        if(isTouching(Muenze.class)){
            removeTouching(Muenze.class);
            Greenfoot.playSound("collect_coin.mp3");
            Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0);
            counter.add(1);
        }
        this.checkDeath();
        meineWelt.changeBackgroundX(1);
        animationSpeed = animationSpeed +1;
        this.CheckSterben();
        this.checkFalling();
    }
     
    public void turnLeft()
    {
        turn(-90);
    }
     
    public void turnRight()
    {
        turn(90);
    }
     
    public void schrittVor()
    {
        if (Greenfoot.isKeyDown("d")){
            this.move(1);
        }
         
    }
     
    public void falling() {
        this.setLocation(this.getX(), this.getY() + vSpeed);
        vSpeed = vSpeed + 1;
    }
     
    public boolean onGround(){
        Actor groundBelow;
        groundBelow = this.getOneObjectAtOffset(0, 45, Platform.class);
         
        if(groundBelow != null || this.getY() >= 460){
            return true;
        }
        else {
            return false;
        }
    }
     
    public void checkFalling() {
        if(onGround() == true || this.getY() > 450) {
            vSpeed = 0;
             
            Actor myPlatform;
            myPlatform = this.getOneIntersectingObject(Platform.class);
             
            if(myPlatform != null) {
                this.setLocation(this.getX(), myPlatform.getY() - 80);
            }
        }
        else {
            this.falling();
        }
    }
     
    public void jump() {
        vSpeed = -15;
        this.falling();
    }
     
    public void animateLeft() {
        if(frame == 1){
            setImage(bmls);
            frame = 2;
        }
        else if(frame == 2){
            setImage(bmlr);
            frame = 1;
        }
    }
     
    public void animateRight() {
        if(frame == 1){
            setImage(bmrs);
            frame = 2;
        }
        else if(frame == 2){
            setImage(bmrr);
            frame = 1;
        }
    }
     
    public void CheckSterben() {
        if(this.getY() >= 420) {
            Greenfoot.setWorld(new Menu());
        }
    }
     
    public boolean checkDeath() {
        if(getY() >= 420) {
            return true;
        }
        else {
            return false;
        }
    }
}
danpost danpost

2023/6/21

#
Kolala wrote...
<< Code Omitted >>
Line 155 creates a Menu object without passing an int value, hence initializing the score to zero. The value of the Counter object from the world needs to be passed there.
Kolala Kolala

2023/6/22

#
How do I get the value in there?
danpost danpost

2023/6/22

#
Kolala wrote...
How do I get the value in there?
1
2
Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
Greenfoot.setWorld(new Menu(counter.getValue()));
Kolala Kolala

2023/6/22

#
Thank you so much man youre awesome! <3
There are more replies on the next page.
1
2