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

2013/11/21

Score Counter problems

Acummings Acummings

2013/11/21

#
I am new to greenfoot and we have a project in class to do, i have my score counter working and running in my first world but once i shift to my second world my score counter resets back to zero. How would i go about fixing this. If i need to attach code just let me know which parts you need and i will add them. any help would be great. its stressing me out.
thefutureisnow thefutureisnow

2013/11/21

#
I'm not sure how possible this is in Greenfoot (I haven't used Greenfoot in a while), but I guess you could create an independant class (not an entity or a world, just a plain class) that is in charge of variables like this.
Acummings Acummings

2013/11/21

#
any chance i can possibly get the code for said idea?
danpost danpost

2013/11/21

#
You can transfer the score counter itself into the new world or just set the new counter in the new world to the value of the counter in the old world. Example from an actor class:
World2 world2 = new World2();
world2.getCounter().setValue(getWorld().getCounter().getValue());
Acummings Acummings

2013/11/22

#
Now would that set of code go into an actor or the new world?
danpost danpost

2013/11/22

#
Acummings wrote...
Now would that set of code go into an actor or the new world?
danpost wrote...
Example from an actor class
Anyway, the use of 'getWorld' should be a good hint.
thefutureisnow thefutureisnow

2013/11/22

#
Well, (and I'm not sure exactly how well this would work). You would create a class (not a world or an actor class), and you would set score variables for it (make sure they're public, and if you set them private, make sure you have public methods in that class that can edit and view that variable). And then you would just do the following:
this.score = scoreclass.score
Of course, make sure that you modify this for the correct variables.
danpost danpost

2013/11/22

#
thefutureisnow wrote...
Well, (and I'm not sure exactly how well this would work). You would create a class (not a world or an actor class), and you would set score variables for it (make sure they're public, and if you set them private, make sure you have public methods in that class that can edit and view that variable). And then you would just do the following:
this.score = scoreclass.score
Of course, make sure that you modify this for the correct variables.
All this does is create another object (just not a world or actor object) that is referenced in the first world. It would still have to be passed to the new world either as an argument in the world constructor call or by calling a method that passes the object to the new world or by assigning a field in the new world to the object. The code 'this.score = scoreclass.score' would only work if the new world was already passed tht 'scoreclass' object. Now, if everything saved in the object were made 'static', it would be possible to make them accessible to any world without any further actions. However, I do not think that doing this would be considered a 'good' programming technique.
You need to login to post a reply.