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
danpost danpost

2020/5/7

#
Ahmed780 wrote...
Is this correct? << Code Omitted >>
Looks good.
Ahmed780 Ahmed780

2020/5/7

#
It still displays 0
danpost danpost

2020/5/7

#
Ahmed780 wrote...
It still displays 0
Show class of that which creates a new EndScreenWorld object.
Ahmed780 Ahmed780

2020/5/7

#
This is the class which creates EndScreenWorld. public class CatchWorld extends SimulationWorld { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ protected GreenfootSound music; protected double timeGameStart = System.currentTimeMillis(); public static double currentGameTime; /** * */ public CatchWorld() { super("", 800, 600, new Point2D(0.0, 0.0), 20); music = new GreenfootSound("gamePlay.wav"); setMusic(music); GreenfootImage background = getBackground(); background.setColor(Color.BLACK); background.fill(); prepare(); } /** * */ public void act() { super.act(); List<Bowl> bowls = getObjects(Bowl.class); if (bowls.size() > 0) { Bowl player = bowls.get(0); cameraCenter.setY(player.getPosition().getY() + 6.0); } currentGameTime = (System.currentTimeMillis() - timeGameStart) / 1000; if (System.currentTimeMillis() >= timeGameStart + 50000) { transitionToWorld( new EndScreenWorld(0)); } } /** * */ private void prepare() { Bowl bowl = new Bowl(); addObject(bowl, 400, 500); bowl.setLocation(400, 540); /* Generate apples for 20 screens*/ int i = 0; while (i < 12000) { int xPos = 40 + 60 * Greenfoot.getRandomNumber(10); /* get random number 1 or 2 for adding apples (appleCategory 1 for Green Apple, appleCategory 2 for Yellow Apple)*/ int appleCategory = Greenfoot.getRandomNumber(2) + 1; addObject( new Apple(appleCategory), xPos, - i); i = i + 60; } Score score = new Score(); addObject(score, 700, 56); } }
danpost danpost

2020/5/7

#
In this line:
transitionToWorld( new  EndScreenWorld(0));
you are passing a "0" score.
Ahmed780 Ahmed780

2020/5/7

#
What do I replace it with?
danpost danpost

2020/5/7

#
Ahmed780 wrote...
What do I replace it with?
You need to get the score from the Score object.
Ahmed780 Ahmed780

2020/5/7

#
Like this? transitionToWorld( new EndScreenWorld(Score)/(Score score);
danpost danpost

2020/5/7

#
Ahmed780 wrote...
Like this? transitionToWorld( new EndScreenWorld(Score)/(Score score);
No. You have to get the Score object from in the CatchWorld world and then get the score value from that Score object.
Ahmed780 Ahmed780

2020/5/7

#
I am actually new to java/greenfoot and I have no idea how to do this. Can you guide me please?
Ahmed780 Ahmed780

2020/5/7

#
I added getScore at the bottom to get the score. Is this correct? public class CatchWorld extends SimulationWorld{ /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ protected GreenfootSound music; protected double timeGameStart = System.currentTimeMillis(); public static double currentGameTime; private double score = 0; /** * */ public CatchWorld() { super("", 800, 600, new Point2D(0.0, 0.0), 20); music = new GreenfootSound("gamePlay.wav"); setMusic(music); GreenfootImage background = getBackground(); background.setColor(Color.BLACK); background.fill(); prepare(); } /** * */ public void act() { super.act(); List<Bowl> bowls = getObjects(Bowl.class); if (bowls.size() > 0) { Bowl player = bowls.get(0); cameraCenter.setY(player.getPosition().getY() + 6.0); } currentGameTime = (System.currentTimeMillis() - timeGameStart) / 1000; if (System.currentTimeMillis() >= timeGameStart + 15000) { transitionToWorld( new EndScreenWorld(0)); } } /** * */ private void prepare() { Bowl bowl = new Bowl(); addObject(bowl, 400, 500); bowl.setLocation(400, 540); /* Generate apples for 20 screens*/ int i = 0; while (i < 12000) { int xPos = 40 + 60 * Greenfoot.getRandomNumber(10); /* get random number 1 or 2 for adding apples (appleCategory 1 for Green Apple, appleCategory 2 for Yellow Apple)*/ int appleCategory = Greenfoot.getRandomNumber(2) + 1; addObject( new Apple(appleCategory), xPos, - i); i = i + 60; } Score score = new Score(); addObject(score, 700, 56); } /** * */ public double getScore() { return score; } }
danpost danpost

2020/5/7

#
Ahmed780 wrote...
I added getScore at the bottom to get the score. Is this correct? << Code Omitted >>
No. From the world, you get a list of all Score objects:
java.util.List scoreList = this.getObjects(Score.class);
I put an explicit "this" in there to show you the structure of how to perform an operation on an object. The format is: << subject >> . << verb >> Because the code is in your CatchWorld class, any operation performed on "this" world can be coded without the subject being explicitly given. That is, the above line of code will work simply as:
java.util.List scoreList = getObjects(Score.class);
Now, with a list of score object being a List object, you can perform a get operation:
Score score = (Score)scoreList.get(0);
The "Score" in parenthesis indicates that the object is indeed a Score object and can be assigned to a reference declared to be of type Score. Now, with the Score object, you can perform the getValue operation:
double scoreValue = score.getValue();
All in all, this can summerized as:
double scoreValue = ((Score)getObjects(Score.class).get(0)).getValue();
Then you can replace the "0" with "scoreValue".
Ahmed780 Ahmed780

2020/5/7

#
Where do I write those lines in Catchworld?
Ahmed780 Ahmed780

2020/5/7

#
Is there another method to display the score on endscreen? This method is a bit complicated and my teacher doesn’t expect this. I am given a scenario and I need to complete it without making too many changes
danpost danpost

2020/5/7

#
Ahmed780 wrote...
Is there another method to display the score on endscreen? This method is a bit complicated and my teacher doesn’t expect this. I am given a scenario and I need to complete it without making too many changes
You could move the line:
Score score = new Score();
to outside the prepare method. Then simply use:
double scoreValue = score.getValue();
There are more replies on the next page.
1
2
3
4