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

2012/10/1

cannot find symbol - variable

Penguins_and_Orbs Penguins_and_Orbs

2012/10/1

#
I am attempting to create a score board, which counts how many orbs my Penguin has eaten. I currently have an error: cannot find symbol - variable Score My current script goes as follows: import greenfoot.*; import java.awt.*; import java.awt.Color; /** * Write a description of class Score here. * * @author Connor Feltham * @version (a version number or a date) */ public class Score extends Actor { private int Score; /** * Act - count the number of worms eaten. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { score(); } public void score() { GreenfootImage img = new GreenfootImage(200, 200); img.setColor (new Color(0,0,0)); float fontSize = 35.0f; //change this to the font size that you want Font font = img.getFont(); font = font.deriveFont(fontSize); img.setFont(font); PenguinWorld w = (PenguinWorld) getWorld(); Penguin p = (Penguin) w.getObjects(Penguin.class).get(0); img.drawString("Score" + p.Score, 50, 50); //this x and y position is the bottom-left of the first letter. setImage(img); } } Thanks.
danpost danpost

2012/10/1

#
It appears you are trying to get the 'Score' value from the penguin object. So, I wonder why you have a 'Score' variable in this class!
Gevater_Tod4711 Gevater_Tod4711

2012/10/1

#
you want to find a variable named Score in your Penguin but if there is no such variable in this class you can't find it. Also it would be possible that the variable Score in your Penguin has private excess but I think that would throw another error. Another error prone part of your code is the line Penguin p = (Penguin) w.getObjects(Penguin.class).get(0), because if there is no Penguin on your World there will be an Exception. Better use this:
public void score() {
    GreenfootImage img = new GreenfootImage(200, 200); 
    img.setColor (new Color(0,0,0));
    float fontSize = 35.0f; //change this to the font size that you want 
    Font font = img.getFont(); font = font.deriveFont(fontSize);
    img.setFont(font); PenguinWorld w = (PenguinWorld) getWorld();
    if (w.getObjects(Penguin.class).size() != 0) {
        Penguin p = (Penguin) w.getObjects(Penguin.class).get(0);
        img.drawString("Score" + p.Score, 50, 50); //this x and y position is the bottom-left of the first letter. 
        setImage(img);
    }
}
With this code there will be no exceptions if there is no penguin on your world.
Penguins_and_Orbs Penguins_and_Orbs

2012/10/15

#
It's still saying it cannot find the variable Score. Would I put this into the Penguin class, or into a new Score class?
danpost danpost

2012/10/15

#
Is the class 'Score' supposed to create a final scoreboard? It is a bit confusing, as you have the code being run every act method, but the size of the scoreboard is extremely large to be a running score counter. It looks like you gathered code from possibly more than one source (with different ideas of what you wanted) and tried to combine them together. What exactly is the 'Score' class supposed to be doing (be precise)?
Penguins_and_Orbs Penguins_and_Orbs

2012/10/15

#
The Score class is supposed to be a piece of text at the top of the screen, which goes something like Score: 0 and then adds up one each time an orb is eaten.
Upupzealot Upupzealot

2012/10/15

#
The name of the class is Score, it has a param also named Score, and what's more a method has a name "score"...would please make them diffrent, it may cause some unexpected problem sometimes.
danpost danpost

2012/10/15

#
What you need then is for the Penguin to inform the Score class each time it eats an orb. The Score class does not need an act method, but does need a method the penguin can call to inform it (see 'bump'); plus a method to change the image when informed (see 'updateImage'). The Score class can hold the score variable which keep track of how many has been eaten. This means you also would need a method to retrieve the score from the class (see 'getScore').
import greenfoot.*;
import java.awt.*;
import java.awt.Color;

public class Score extends Actor
{
    private int score = 0;
 
    public Score()
    {
        updateImage();
    }
  
    private void updateImage()   
    {   
       GreenfootImage img = new GreenfootImage(200, 45); // adjust size
       img.setColor (Color.black);
       float fontSize = 35.0f; //change this to the font size that you want   
       Font font = img.getFont();   
       font = font.deriveFont(fontSize);   
       img.setFont(font);   
       img.drawString("Score: " + score, 20, 40); //this x and y position is the bottom-left of the first letter.   
       setImage(img);   
    }

    public void bump()
    {
        score++;
        updateImage();
    }

    public int getScore()
    {
        return score;
    }
}
In the Penguin class, use this as the 'eat' method:
private void eat()
{
    Orb orb = (Orb) getOneIntersectingObject(Orb class);
    if (orb == null) return; // remove this line if using 'canSee' method
    getWorld().removeObject(orb);
    if (getWorld().getObjects(Score.class).isEmpty()) return;
    Score scr = (Score) getWorld().getObjects(Score.class).get(0);
    scr.bump();
}
Hopefully, that will get you going. Any question or concerns, do not hesitate to post back. BTW, you need to remove the variable 'Score' from the Penguin class
danpost danpost

2012/10/15

#
The reason for the 'getScore' method in the Score class is so you can check it and perform actions depending on the value. Let us say you wanted the game to stop after 20 orbs have been eaten. Then, in the 'eat' method of the Penguin class, after 'scr.bump();', you could put 'if (scr.getScore() == 20) { gameOver(); }' (of course, you would have to create the 'gameOver' method).
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
Yeah thanks everyone. danpost your script has worked to the point of the score being on there and counting up, but it goes up whether I collect an orb or not. I'll have a look to see if I can work out why, but thanks for the whole script! :)
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
Got it.
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
Okay, it's working now, but the score actually starts at one, I was wondering why?
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
Ignore me... it's fixed.
You need to login to post a reply.