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

2013/3/22

Keeping a counters amount??

1
2
3
RedManRocket RedManRocket

2013/3/22

#
I have a space invaders game and in this game iI have a counter that hold "coins" and I have a retry button after you die that will set the world back and I want to know how to keep the amount of coins in the counter even if I press the retry button
danpost danpost

2013/3/22

#
Do not set the world at the time you create the world. Not knowing your world class name, I will call it 'MyWorld'.
// instead of
Greenfoot.setWorld(new MyWorld());
// use
MyWorld myWorld = new MyWorld();
Greenfoot.setWorld(myWorld);
// then follow with this
myWorld.counter.setValue(counter.getValue());
I do not know if you called it 'counter' in your world, but the idea is evident -- just move the counter value to the counter in the new world.
RedManRocket RedManRocket

2013/3/22

#
The variable counter "the one after the = symbol what counter variable is that The first variable I put as cannonCoins, as that is the name of the counter, variable wise
danpost danpost

2013/3/22

#
I edited the code above. If 'cannonCoins' is just a variable and not an object, then change line 7 to
myWorld.cannonCoins = cannonCoins;
If 'cannonCoins' is a counter object then change in line 7 to
myWorld.cannonCoins.setValue(cannonCoins.getValue());
RedManRocket RedManRocket

2013/3/22

#
No no my bad here ill try to explain better, in Space "myWorld" cannonCoins is the CoinCounter and in Space I have a returnCannonCoins method, I'm not really sure if that helps the situation. And in my cannon I have a variable called money that tells the CoinCounter to up its value.
danpost danpost

2013/3/22

#
I would be better if you explained it using key words, like 'object', 'class', 'field', and 'method'. If a method returns something, say what it returns, whether it be an object or a value, and what kind of object or value. So far, I got this: you have a world class called 'Space' that has a method called 'returnCannonCoins' that returns something (I am not sure what). Then, I am guessing you have a 'cannon' class that has a variable (or field) called 'money'; but a variable cannot tell anything to do anything (I am confused here). I cannot even tell if CoinCounter is a class or an object.
RedManRocket RedManRocket

2013/3/22

#
//This is the Cannon Class, not everything but what matters public int coin; public int money; public Cannon() { coin = 10; money = 0; } public void addCoins(int coin) { CoinCounter coincounter = new CoinCounter(); money = money + coin; updateCannonCoins(); } public void updateCannonCoins() { Space space = (Space)getWorld(); CoinCounter cannonCoins = space.returnCannonCoins(); cannonCoins.setValue(money); } //This is the World "Space" again not everything but what matters public CoinCounter cannonCoins = new CoinCounter("Coins "); /** * Return the amount of Coins */ public CoinCounter returnCannonCoins() { return cannonCoins; }
danpost danpost

2013/3/22

#
Looks like you should use the following to pass the counter value to the new world:
Space space = new Space();
Greenfoot.setWorld(space);
space.returnCannonCoins().setValue(returnCannonCoins().getValue());
RedManRocket RedManRocket

2013/3/22

#
This did not work, where was I supposed to put this
danpost danpost

2013/3/22

#
In the retry button class 'act' method when you detect a mouse click?
RedManRocket RedManRocket

2013/3/22

#
It still sets the coins back to 0
danpost danpost

2013/3/22

#
Did you stop the scenario, right click on the counter object and inspect the value? It may have a different value internally than what is being displayed. Report back as to results.
danpost danpost

2013/3/22

#
I guess if this code is to be in a different actor class, then it should be more like the following:
Space space = new Space();
Greenfoot.setWorld(space);
Space oldSpace = (Space) getWorld();
space.returnCannonCoins().setValue(oldSpace.returnCannonCoins().getValue());
RedManRocket RedManRocket

2013/3/22

#
Still no, and a FYI, I have a CoinCounter class that has methods in it but it is just the default import counter class if that helps
RedManRocket RedManRocket

2013/3/22

#
The last post you posted have given the correct results it is internally correct and is displaying correctly thank for your time. If there is anything else you would lie to add feel free
There are more replies on the next page.
1
2
3