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

#
I put those lines in act method but "score.getValue()" says cannot find symbol-method getValue 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)); } Score score = new Score(); double scoreValue = score.getValue(); } /** * */ 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); //double scoreValue = ((Score)getObjects(Score.class).get(0)).getValue(); } }
danpost danpost

2020/5/7

#
Remove the last two lines you just put in act. Also, remove the last line you just put in prepare. You need to work in the block where you go to the end screen.
Ahmed780 Ahmed780

2020/5/7

#
I put the lines before transitionToWorld
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) {
         Score score =  new  Score();
         double scoreValue = score.getValue();
            transitionToWorld( new  EndScreenWorld(0));
        }
        
}
danpost danpost

2020/5/7

#
Ahmed780 wrote...
I put the lines before transitionToWorld << Code Omitted >>
Line 11 needs to be outside of any and all methods. Then you need to replace the "0" on line 12 with the proper value.
Ahmed780 Ahmed780

2020/5/7

#
Is this ok? If so what do replace 0 with?

public CatchWorld()
    {
        super("", 800, 600,  new  Point2D(0.0, 0.0), 20);
        music =  new  GreenfootSound("gamePlay.wav");
        setMusic(music);
        Score score =  new  Score();
        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) {
            double scoreValue = score.getValue();
            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);
        
        //double scoreValue = ((Score)getObjects(Score.class).get(0)).getValue();
    }
}
Super_Hippo Super_Hippo

2020/5/7

#
You replace the ‘0’ with ‘scoreValue’.
danpost danpost

2020/5/7

#
Super_Hippo wrote...
You replace the ‘0’ with ‘scoreValue’.
and remove line 51; and move line 6 up above where this code starts.
Ahmed780 Ahmed780

2020/5/7

#
I removed the line and replaced 0 with scoreValue.I now have two errors score.getValue and return score. Do I change anything else in catchworld or score class?
import lang.stride.*;
import greenfoot.*;
import java.util.*;



/**
 * 
 */
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);
        Score score =  new  Score();
        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) {
             double scoreValue = [i]score[/i].getValue();
            transitionToWorld( new  EndScreenWorld(scoreValue));
        }
               
    }

    /**
     * 
     */
    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;
        }
        
        //addObject(score, 700, 56);
        
        
    }
    [u]public double getValue()
    {
        return [i]score[/i];
}
}
Score class
import lang.stride.*;
import greenfoot.*;
import java.util.*;

/**
 * Write a description of class Score here. @author (your name) @version (a version number or a date)
 */
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;
    }
}
danpost danpost

2020/5/7

#
Move line 24 in CatchWorld class to line 17 and remove line 75 to 78.
Ahmed780 Ahmed780

2020/5/7

#
I still have an error "score.getValue()" cannot find symbol - method getValue(). How do I fix this error?
public class CatchWorld extends SimulationWorld
{
    /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/
    protected GreenfootSound music;
    
    protected double timeGameStart = System.currentTimeMillis();
    public static double currentGameTime;
    Score score =  new  Score();

    /**
     * 
     */
    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) {
             double scoreValue = score.getValue();
            transitionToWorld( new  EndScreenWorld(scoreValue));
        }
               
    }

    /**
     * 
     */
    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;
        }
        
        //addObject(score, 700, 56);
         
    }
}
Ahmed780 Ahmed780

2020/5/7

#
Thank you so much!!! It finally worked.
You need to login to post a reply.
1
2
3
4