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

2020/4/30

Asteroids ScoreBoard Reading 100

1
2
Sharp1122 Sharp1122

2020/4/30

#
Hello Everyone, I am currently trying to figure out the reason why my scoreboard is always reading 100 points. No matter what I do. Maybe someone can help? Here is my Scoreboard code: public class ScoreBoard extends Actor { public static final float FONT_SIZE = 20f; public static final int WIDTH = 400; public static final int HEIGHT = 300; /** * Create a score board with dummy result for testing. */ public ScoreBoard() { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over! Thank you for playing!", "Score: ", score); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.BLACK); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); } }
danpost danpost

2020/4/30

#
Please show class code where "new ScoreBoard(" is located in.
Sharp1122 Sharp1122

2020/4/30

#
I don't think I have a class that has "new ScoreBoard"(
Sharp1122 Sharp1122

2020/4/30

#
Nevermind, I didn't look hard enough. Here it is... public void gameOver() { addObject(new ScoreBoard(100), getWidth()/2, getHeight()/2); }
Sharp1122 Sharp1122

2020/4/30

#
With me changing the addObject(new ScoreBoard(100), getWidth()/2, getHeight()/2); to addObject(new ScoreBoard(0), getWidth()/2, getHeight()/2); it does change the scoreboard when the game is over to 0 but I want it to be linked with the Score Counter. Does that mean I have to make another method?
danpost danpost

2020/4/30

#
Sharp1122 wrote...
Nevermind, I didn't look hard enough. Here it is... public void gameOver() { addObject(new ScoreBoard(100), getWidth()/2, getHeight()/2); }
You need to replace the 100 with a reference to the field that contains the score.
Sharp1122 Sharp1122

2020/5/1

#
What is a reference, I'm sorry about this. Should I adjust the code so it links with another class?
Sharp1122 Sharp1122

2020/5/1

#
Also I'm assuming I can get rid of the dummy test?
Sharp1122 Sharp1122

2020/5/1

#
import greenfoot.*; /** * Space. Something for rockets to fly in. * * @author Michael Kölling * @version 1.1 */ public class Space extends World { private Counter scoreCounter; private Counter levelCounter; private int startAsteroids = 3; /** * Create the space and all objects within it. */ public Space() { super(600, 500, 1); GreenfootImage background = getBackground(); background.setColor(Color.ORANGE); background.fill(); createStars(50); Rocket rocket = new Rocket(); addObject(rocket, getWidth()/2 + 100, getHeight()/2); //addAsteroids(startAsteroids); scoreCounter = new Counter("Score: "); addObject(scoreCounter, 60, 480); levelCounter= new Counter(" Level: "); addObject(levelCounter, 120, 480); Explosion.initializeImages(); ProtonWave.initializeImages(); runLevel(1); } public void runLevel(int level) { countLevel(); //setInitialBulletLevel(); startAsteroids = levelCounter.getValue() + 2; addAsteroids(startAsteroids); } public void countScore(int addValue) { scoreCounter.increment(); } public void countLevel() { levelCounter.increment(); levelCounter.act(); } /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } /** * Crete a given number of stars in space. */ private void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = 120 - Greenfoot.getRandomNumber(100); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } /** * This method is called when the game is over to display the final score. */ public void gameOver() { addObject(new ScoreBoard(100), getWidth()/2, getHeight()/2); } }
Sharp1122 Sharp1122

2020/5/1

#
such as int ScoreBoard = 1?
Yehuda Yehuda

2020/5/1

#
Create a getter method in class Score (if there is not one already). Then, if the name of the getter method is "getScore", when you add the score board you can do:
addObject(new ScoreBoard(scoreCounter.getScore()), ..., ...);
Sharp1122 Sharp1122

2020/5/1

#
so like getValue() and getCounter()?
Yehuda Yehuda

2020/5/1

#
Sharp1122 wrote...
so like getValue() and getCounter()?
I'm not sure what you're referring to. In the counter class you need a way to retrieve its current score.
Sharp1122 Sharp1122

2020/5/1

#
I'm sorry, I'm just confused on what I need to do. I think I am just overthinking it. You are saying I need to make a get method in the Counter class to get the counter score onto the scoreboard?
Yehuda Yehuda

2020/5/1

#
Yes. After you've done that, you can do what I've shown above in your Space class.
There are more replies on the next page.
1
2