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

2014/1/27

How do I keep my score from TheWorld to TheWorld2?

Bean4221 Bean4221

2014/1/27

#
I need help doing what the title says! Here's my code for TheWorld, and TheWorld2: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class TheWorld here. * * @author (your name) * @version (a version number or a date) */ public class TheWorld extends World { /** * Constructor for objects of class TheWorld. * */ public TheWorld() { super(600, 480, 1); prepare(); } /** * Prepare the world the start of the game */ private void prepare() { Counter counter = new Counter(); addObject(counter, 835, 19); Player player = new Player(counter); addObject (player, 452, 332); GoldCoin goldcoin = new GoldCoin(); addObject(goldcoin, 587, 327); goldcoin.setLocation(570, 326); GoldCoin goldcoin2 = new GoldCoin(); addObject(goldcoin2, 31, 31); GoldCoin goldcoin3 = new GoldCoin(); addObject(goldcoin3, 849, 60); GoldCoin goldcoin4 = new GoldCoin(); addObject(goldcoin4, 846, 644); GoldCoin goldcoin5 = new GoldCoin(); addObject(goldcoin5, 37, 653); goldcoin.setLocation(450, 406); GoldCoin goldcoin6 = new GoldCoin(); addObject(goldcoin6, 448, 250); GoldCoin goldcoin7 = new GoldCoin(); addObject(goldcoin7, 854, 317); GoldCoin goldcoin8 = new GoldCoin(); addObject(goldcoin8, 31, 353); GoldCoin goldcoin9 = new GoldCoin(); addObject(goldcoin9, 449, 36); GoldCoin goldcoin10 = new GoldCoin(); addObject(goldcoin10, 463, 677); goldcoin10.setLocation(453, 676); counter.setLocation(556, 16); goldcoin9.setLocation(325, 28); goldcoin6.setLocation(330, 222); player.setLocation(333, 329); goldcoin.setLocation(332, 416); goldcoin10.setLocation(29, 134); goldcoin8.setLocation(29, 253); goldcoin5.setLocation(27, 442); goldcoin8.setLocation(26, 304); goldcoin10.setLocation(27, 183); goldcoin9.setLocation(307, 29); goldcoin3.setLocation(568, 60); goldcoin7.setLocation(561, 331); goldcoin4.setLocation(564, 241); goldcoin7.setLocation(568, 448); goldcoin6.setLocation(307, 166); player.setLocation(313, 262); goldcoin.setLocation(310, 374); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(310, 453); goldcoin.setLocation(311, 352); goldcoin2.setLocation(29, 59); goldcoin8.setLocation(28, 253); goldcoin5.setLocation(26, 452); Snake snake = new Snake(); addObject(snake, 440, 32); Snake snake2 = new Snake(); addObject(snake2, 182, 36); Snake snake3 = new Snake(); addObject(snake3, 464, 457); Snake snake4 = new Snake(); addObject(snake4, 177, 456); Snake snake5 = new Snake(); addObject(snake5, 447, 263); Snake snake6 = new Snake(); addObject(snake6, 188, 257); removeObject(snake4); removeObject(snake3); snake6.setLocation(167, 427); snake5.setLocation(442, 428); snake2.setLocation(167, 39); snake.setLocation(451, 41); SuperGoldCoin supergoldcoin = new SuperGoldCoin(); addObject(supergoldcoin, 315, 97); } } TheWorld2: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class TheWorld2 here. * * @author (your name) * @version (a version number or a date) */ public class TheWorld2 extends World { /** * Constructor for objects of class TheWorld2. * */ public TheWorld2() { super(1000, 800, 1); prepare(); } /** * Prepare the world the start of the game */ private void prepare() { Counter counter = new Counter(); addObject(counter, 835, 19); Player player = new Player(counter); addObject (player, 452, 332); GoldCoin goldcoin = new GoldCoin(); addObject(goldcoin, 587, 327); goldcoin.setLocation(570, 326); GoldCoin goldcoin2 = new GoldCoin(); addObject(goldcoin2, 31, 31); GoldCoin goldcoin3 = new GoldCoin(); addObject(goldcoin3, 849, 60); GoldCoin goldcoin4 = new GoldCoin(); addObject(goldcoin4, 846, 644); GoldCoin goldcoin5 = new GoldCoin(); addObject(goldcoin5, 37, 653); goldcoin.setLocation(450, 406); GoldCoin goldcoin6 = new GoldCoin(); addObject(goldcoin6, 448, 250); GoldCoin goldcoin7 = new GoldCoin(); addObject(goldcoin7, 854, 317); GoldCoin goldcoin8 = new GoldCoin(); addObject(goldcoin8, 31, 353); GoldCoin goldcoin9 = new GoldCoin(); addObject(goldcoin9, 449, 36); GoldCoin goldcoin10 = new GoldCoin(); addObject(goldcoin10, 463, 677); goldcoin10.setLocation(453, 676); counter.setLocation(556, 16); goldcoin9.setLocation(325, 28); goldcoin6.setLocation(330, 222); player.setLocation(333, 329); goldcoin.setLocation(332, 416); goldcoin10.setLocation(29, 134); goldcoin8.setLocation(29, 253); goldcoin5.setLocation(27, 442); goldcoin8.setLocation(26, 304); goldcoin10.setLocation(27, 183); goldcoin9.setLocation(307, 29); goldcoin3.setLocation(568, 60); goldcoin7.setLocation(561, 331); goldcoin4.setLocation(564, 241); goldcoin7.setLocation(568, 448); goldcoin6.setLocation(307, 166); player.setLocation(313, 262); goldcoin.setLocation(310, 374); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(27, 183); goldcoin10.setLocation(310, 453); goldcoin.setLocation(311, 352); goldcoin2.setLocation(29, 59); goldcoin8.setLocation(28, 253); goldcoin5.setLocation(26, 452); Snake snake = new Snake(); addObject(snake, 440, 32); Snake snake2 = new Snake(); addObject(snake2, 182, 36); Snake snake3 = new Snake(); addObject(snake3, 464, 457); Snake snake4 = new Snake(); addObject(snake4, 177, 456); Snake snake5 = new Snake(); addObject(snake5, 447, 263); Snake snake6 = new Snake(); addObject(snake6, 188, 257); removeObject(snake4); removeObject(snake3); snake6.setLocation(167, 427); snake5.setLocation(442, 428); snake2.setLocation(167, 39); snake.setLocation(451, 41); SuperGoldCoin supergoldcoin = new SuperGoldCoin(); addObject(supergoldcoin, 315, 97); } } Also when I switch worlds by touching the edge (code below) when I get in the new world, and hit the side again, it resets the world. if (atWorldEdge()) { Greenfoot.setWorld(new TheWorld2()); }
danpost danpost

2014/1/27

#
You need to set a local field to the new TheWorld2 object, set the value of the counter in the new world to that of the old world and then set the new world active:
if (atWorldEdge())
{
    // create a reference to a newly created TheWorld2 world
    TheWorld2 world2 = new TheWorld2();
    // get references to the counters in both worlds
    Counter w1counter = (Counter)getWorld().getObjects(Counter.class).get(0);
    Counter w2counter = (Counter)world2.getObjects(Counter.class).get(0);
    // set value of counter in new world to the counter in current world
    w2counter.setValue(w1counter.getValue());
    // set the new world active
    Greenfoot.setWorld(world2);
}
Bean4221 Bean4221

2014/1/28

#
thanks so much
Bean4221 Bean4221

2014/1/28

#
just one problem, when I try to compile it, it says for this line of code... Space.setValue(TheWorld.getValue()); cannot find method getValue() Do I have to make something so the .getValue method from my counter to the other classes?
danpost danpost

2014/1/28

#
Why did you even change anything in the code I provided? It should work just as it is. I did presume your Counter class had 'getValue' and 'setValue' methods in it, however. Also, change the 'if' line to this:
Bean4221 Bean4221

2014/1/28

#
I only changed it because the world names where different :)
Bean4221 Bean4221

2014/1/28

#
and yes my counter class is... 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 maxPoints; /** * 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("" + value, 22, Color.BLACK, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); } }
danpost danpost

2014/1/28

#
danpost wrote...
Also, change the 'if' line to this:
if (atWorldEdge() && getWorld() instanceof TheWorld)
Sorry, this was cut off my previous post.
Bean4221 wrote...
I only changed it because the world names where different :)
You only had shown the two worlds above. Anyway, it is not the world that has the 'getValue' and 'setValue' methods; it is the Counter objects within those worlds. That is why I got a reference to Counter objects in both worlds and then set the value of the one in the new world to the value of the one in the current world.
Bean4221 Bean4221

2014/1/28

#
it still cannot find the method getValue.... Also just to let you know, the getValue and other methods are in a class called Counter. Sorry this is taking long, I'm pretty new to coding :)
danpost danpost

2014/1/28

#
danpost wrote...
You need to set a local field to the new TheWorld2 object, set the value of the counter in the new world to that of the old world and then set the new world active:
if (atWorldEdge())
{
    // create a reference to a newly created TheWorld2 world
    TheWorld2 world2 = new TheWorld2();
    // get references to the counters in both worlds
    Counter w1counter = (Counter)getWorld().getObjects(Counter.class).get(0);
    Counter w2counter = (Counter)world2.getObjects(Counter.class).get(0);
    // set value of counter in new world to the counter in current world
    w2counter.setValue(w1counter.getValue());
    // set the new world active
    Greenfoot.setWorld(world2);
}
The fields 'w1counter' and 'w2counter' are the counters in each world. Line 9 passes the value from one counter to the other.
You need to login to post a reply.