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

2011/11/29

How to get score from the first level into the second level.

ardwennem2 ardwennem2

2011/11/29

#
Hi, how can I get my score from the first level (first world) to the second level (second world)? my score: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Counter here. * * @author (your name) * @version (a version number or a date) */ public class Counter extends Actor { /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int totalCount = 0; public Counter() { setImage(new GreenfootImage("Score = " + "0", 20, Color.BLACK, Color.WHITE)); } /** * Increase the total amount displayed on the counter, by a given amount. */ public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage("Score = " + totalCount, 20, Color.BLACK, Color.WHITE)); } }
davmac davmac

2011/11/29

#
Well, your question really gives a pretty big hint to the answer:
how can I get my score from the first level (first world) to the second level (second world)?
You need to do two things: 1. Remove the counter from the first world 2. Add it to the second world The code you've posted is just for the Counter class itself, so it's hard to give precise help. But basically, at some point, where you are actually setting the second level using Greenfoot.setWorld(), you also need to transition the counter to the new world. Your main challenge then is to get a reference to the counter. There are a few ways you might do this. 1. If there is only ever one counter in the world, you can use "getObjects(Counter.class).get(0)" to get a reference to the counter. 2. Or, you could have the world store a reference to the counter. See the tutorials (in particular #6) for help with this. 3. Or, you could store a static reference to the counter in one of your classes.
You need to login to post a reply.