This site requires JavaScript, please enable it in your browser!
Greenfoot_back
Back to discussions
No_avatar_thumb
OrangeFlash81 wrote ...

2012/8/24

How do I print my score with this script?

No_avatar OrangeFlash81

2012/8/24

Heya, I'm having a little bit of a problem getting my score from the last game to print. Basically, I have an actor called 'Scoreboard' and when I load a world called MyWorld it prints the score up at the top (successfully). I have another world called GameOver which is switched to when Rocket.class hits Asteroid.class. I want to print the same score I got from the last game on GameOver, but when I try it just says the score is 0. Here's the code for the scoreboard that you may need:
Thanks, OF
Alucard_profile erdelf

2012/8/24

sure, it prints 0 cause you set it to zero on the beginning. You need access to the integer in the other world, my game arena should help you, but in short, you have many way to do this, save the score in a parent world or ask in the constructor or make the int in the game world static
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/8/24

The easiest way is to pass the score to the GameOver world in a parameter. The constructor of the world should be:
The 'score' is then added to the Scoreboard object after it is created, if you are using the Scoreboard class to create the score display in this class. Either way, 'score' will hold the appropriate value. Set the new GameOver world with:
This is assuming you have a reference to the scoreboard and you named it 'scoreboard'. If you named it something else, just replace with that name.
No_avatar OrangeFlash81

2012/8/25

danpost wrote...
The easiest way is to pass the score to the GameOver world in a parameter. The constructor of the world should be:
The 'score' is then added to the Scoreboard object after it is created, if you are using the Scoreboard class to create the score display in this class. Either way, 'score' will hold the appropriate value. Set the new GameOver world with:
This is assuming you have a reference to the scoreboard and you named it 'scoreboard'. If you named it something else, just replace with that name.
OK, I'm now having quite a lot of problems. I have done the
thing in the GameOver world's code and it throws 'missing method body, or declare abstract'. Here's the code for the GameOver world:
And I also made the setWorld command different as you said but now the Rocket throws 'constructor GameOver in class GameOver cannot be applied to the given types; required: no arguments; found: int; reason: actual and formal argument lists differ in length' Here's all of the rocket's code:
Thanks, OF
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/8/25

The main problem (why your score keeps coming up zero) is because of the 'GameOver' world code, specificly lines 13 and 25. You are creating a NEW scoreboard (one that nothing has been added to) and displaying that score. An easy fix at this point is to insert a line at 25 (just before adding the scoreboard object to the world)
The reason you are getting an error on 'public GameOver(int score);' is because you have a semi-colon ( ; ) at the end of that line which should NOT be there. Remove it. Line 14 in the Rocket actor code should be removed (your scoreboard is in the MyWorld world code). Finally, line 61 should be replaced with the following:
You should not longer get any errors and your scoreboard should work. Again, if any problems, post back.
You need to login to post a reply