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

2020/5/4

How do I display the score?

1
2
3
4
Ahmed780 Ahmed780

2020/5/4

#
I am having trouble showing the score on the endscreen even though the score is defined in endscreenworld class. The endscreen displays 0 and novoice player instead of the final score. Endscreen class ************************************************************************************************************************* public class EndScreenWorld extends SimulationWorld { private GreenfootSound gameMusic; /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ /** * Constructor for objects of class EndScreenWorld. */ public EndScreenWorld(double vscore) { super("", 800, 600, new Point2D(0.0, 0.0), 10); gameMusic = new GreenfootSound("endMenu.wav"); Score result = new Score(); addObject(result, 400, 320); result.setImage( new GreenfootImage("You finished with a score of " + (int)vscore + ".", 40, Color.WHITE, Color.BLACK, Color.YELLOW)); Score feedback = new Score(); addObject(feedback, 400, 380); if (vscore >= 25 && vscore <= 75) { String ranking = new String("intermediate player"); feedback.setImage( new GreenfootImage("You are a " + ranking + ".", 40, Color.BLUE, Color.BLACK, Color.YELLOW)); } if (vscore < 25) { String ranking = new String("novice player"); feedback.setImage( new GreenfootImage("You are a " + ranking + ".", 40, Color.BLUE, Color.BLACK, Color.YELLOW)); } if (vscore > 75) { String ranking = new String(); feedback.setImage( new GreenfootImage("You are a " + ranking + ".", 40, Color.BLUE, Color.BLACK, Color.YELLOW)); } } /** * */ public void act() { if (Greenfoot.isKeyDown("space")) { transitionToWorld( new StartScreenWorld()); } } /** * */ public void started() { gameMusic.playLoop(); } /** * */ public void stopped() { gameMusic.stop(); } } Score class ************************************************************************************************************************* public class Score extends Actor { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ private double score = 0; /** * Act - do whatever the Score wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { /* Add your action code here.*/ SimulationWorld world = (SimulationWorld)getWorld(); List<Bowl> bowls = world.getObjects(Bowl.class); if (bowls.size() <= 0) { return; } Bowl bowl = world.getObjects(Bowl.class).get(0); if (bowl != null) { /* score is set to current real position of the bowl (in space)*/ int scoregreen = bowl.greenApplesCounter * 1; int scoreyellow = bowl.yellowApplesCounter * 3; score = scoregreen + scoreyellow; } setImage( new GreenfootImage("Time(sec.): " + CatchWorld.currentGameTime + "\n\n Score: " + (int)score + " ", 22, Color.WHITE, Color.DARK_GRAY, Color.YELLOW)); } /** * */ public double getScore() { return score; } }
Super_Hippo Super_Hippo

2020/5/4

#
Where do you create and set the EndScreenWorld?
Ahmed780 Ahmed780

2020/5/4

#
in simulation world
danpost danpost

2020/5/4

#
You are using a Score object as if it was just a simple actor. The Score class has an act method that will override any image you set to the score object elsewhere (like how you set an image to one in your EndScreenWorld class). I think you need a tutorial on display values. Please refer to my Value Display Tutorial scenario.
Ahmed780 Ahmed780

2020/5/4

#
which scenario should I watch?
Ahmed780 Ahmed780

2020/5/4

#
I watched the tutorial and I am still unable to fix the error. Can you help me fix it please? This is actually my final project and everything works well except for the score which doesn't appear on the gameoverscreen. I hope you you can help me or fix the error.
danpost danpost

2020/5/4

#
Ahmed780 wrote...
which scenario should I watch?
Same one. Read it a couple of times if you have to.
Ahmed780 Ahmed780

2020/5/5

#
I am still having trouble displaying the score on the endscreen. The endscreen keeps displaying 0. Please help me
danpost danpost

2020/5/6

#
Do not use a Score object to display the final score on the endscreen.
Ahmed780 Ahmed780

2020/5/6

#
Sir do you mean this line "addObject(result, 400, 320);"? If so what do I replace it with? public class EndScreenWorld extends SimulationWorld{ private GreenfootSound gameMusic; /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ /** * Constructor for objects of class EndScreenWorld. */ public EndScreenWorld(double vscore) { super("", 800, 600, new Point2D(0.0, 0.0), 10); gameMusic = new GreenfootSound("endMenu.wav"); Score result = new Score(); addObject(result, 400, 320); result.setImage( new GreenfootImage("You finished with a score of " + (int)vscore + ".", 40, Color.WHITE, Color.BLACK, Color.YELLOW)); Score feedback = new Score(); addObject(feedback, 400, 380); if (vscore >= 25 && vscore <= 75) { String ranking = new String("intermediate player"); feedback.setImage( new GreenfootImage("You are a " + ranking + ".", 40, Color.BLUE, Color.BLACK, Color.YELLOW)); } if (vscore < 25) { String ranking = new String("novice player"); feedback.setImage( new GreenfootImage("You are a " + ranking + ".", 40, Color.BLUE, Color.BLACK, Color.YELLOW)); } if (vscore > 75) { String ranking = new String(); feedback.setImage( new GreenfootImage("You are a " + ranking + ".", 40, Color.BLUE, Color.BLACK, Color.YELLOW)); } } /** * */ public void act() { if (Greenfoot.isKeyDown("space")) { transitionToWorld( new StartScreenWorld()); } } /** * */ public void started() { gameMusic.playLoop(); } /** * */ public void stopped() { gameMusic.stop(); } }
danpost danpost

2020/5/6

#
Ahmed780 wrote...
Sir do you mean this line "addObject(result, 400, 320);"? If so what do I replace it with?
No -- not that line; but, the line before it. You need to use a different type of object other than Score.
Ahmed780 Ahmed780

2020/5/6

#
What object should I use?
danpost danpost

2020/5/6

#
Ahmed780 wrote...
What object should I use?
Just a basic (sub-)Actor object will do.
Ahmed780 Ahmed780

2020/5/6

#
If I use another sub actor it doesn't display the score at all.
danpost danpost

2020/5/6

#
Ahmed780 wrote...
If I use another sub actor it doesn't display the score at all.
Why not? you give result its image of the score value two lines down.
There are more replies on the next page.
1
2
3
4