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

2019/5/27

I'm having trouble keeping the same score across different levels

1
2
3
Dan143 Dan143

2019/5/27

#
I have a Maze game where there are coins that you can collect, I would like the score to continue across 3 Levels.
CreatorMoon CreatorMoon

2019/5/27

#
I think you have to put the code in all three worlds. Not so sure. I need help with the same thing lol.
Super_Hippo Super_Hippo

2019/5/27

#
Either pass the counter object to the new world or the score variable. Alternatively, save the score in one place and use it from every world.
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
Literally just about to ask this xD
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
Super_Hippo wrote...
Either pass the counter object to the new world or the score variable. Alternatively, save the score in one place and use it from every world.
what do you mean by this?
Super_Hippo Super_Hippo

2019/5/27

#
When creating an object, you can pass something to it and use this passed variable or object in the constructor of this newly created object. So for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
//in class A
 
//constructor without parameter
public A()
{
    //…
}
 
//constructor with an int as parameter
public A(int a)
{
    //…
}
A world is also an object, so this way, you can pass the score variable or the counter to the new world and use it there. The alternative is that you have a static field somewhere and you use this everywhere.
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
so I just make the variables public
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
I honestly don't get it. I try making them public and it doesn't work. I don't have any counter code in my words if that's what you mean, just the code to import the counter or whatever. I'm probably annoying you, it's just I don't get what I'm meant to change.
Super_Hippo Super_Hippo

2019/5/27

#
What? No, making them public won't change anything. Do you know what a constructor is?
danpost danpost

2019/5/27

#
You must first have a Counter object in each world. Then, do the step I had outlined previously (in another thread).
CreatorMoon CreatorMoon

2019/5/27

#
How do you save it in one place?
CreatorMoon CreatorMoon

2019/5/27

#
Yeah, I honestly don't get it. I am using my score in my Player, is there any way to transfer it from my player to all of my worlds? Below is my code, I don't know what to transfer and what to keep.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Player extends Actor
{
    public int coinsCollected;
}
public void act()
    {
getWorld().showText("Score: "+coinsCollected, 50, 50);//shows the score
        if (isTouching(Coin.class))
        {
            coinsCollected = coinsCollected +1;
            removeTouching(Coin.class);//removes a coin when you touch it
        }
if (coinsCollected == 5)
        {
            getWorld().removeObjects(getWorld().getObjects(Lock.class));//removes a lock when you meet the criteria (5 coins)
        }
}
CreatorMoon CreatorMoon

2019/5/27

#
Right, now I kinda get it, but how do you pass something? I'm sorry for being annoying, I just want a good mark.
danpost danpost

2019/5/27

#
CreatorMoon wrote...
How do you save it in one place?
That would be a different way of going about it. If you do not have your "level" worlds extending a single class that extends World (the single class being one place to put it), then you would probably use a separate class, like a "Stats" class for it. Either way, you would need to use a static field and your initial world class would need to explicitly set it to zero (as static fields to do re-initialize when you reset your scenario).
CreatorMoon CreatorMoon

2019/5/27

#
I'm still confused. Let me simplify it. I'm basically wanting to carry line 3 in my code over to one world. I'm sorry if I'm acting like a complete idiot, but I can't understand what you're saying.
There are more replies on the next page.
1
2
3