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

2012/3/11

Please Help (With Scoreboard)

1
2
JayWood860 JayWood860

2012/3/11

#
this is my code for the world and scoreboard (class name = score), i have no syntax errors but the text is not appearing. what am i doing wrong? SPACE:
public class Space extends World
{
    private Player1 P1;
    Score scoreCounter = new Score("Score: ");
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 500, 1); 
        
        Player1 P1 = new Player1();        
        addObject(P1, 350, 350);
        
        addObject(scoreCounter, 950, 490);
        
        addRocks();
    }
SCORE:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
/**
 * Write a description of class Score here.
 * 
 * @author Juwan 
 * @version P.Bravo(2)
 */
public class Score extends Actor
{
    private static final Color textColor = new Color(225, 180, 150);
    
    public int vos = 0;
    private String text;
    private int stringLength;
    
    GreenfootImage image = getImage();
    Space space = (Space) getWorld();
    Font font = image.getFont();
    public Score()
    {
        this("");
    }
    public Score(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;
        
        setImage(new GreenfootImage(stringLength, 24));
        image.setFont(font.deriveFont(24.0F));
        image.setColor(textColor);
        
        updateImage();
    }
    /**
     * 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()
    {
        vos++;
        updateImage();
    }
    public int getVOS()
    {
        return vos;
    }
    public void updateImage()
    {
        image.clear();
        image.drawString(text + vos, 1, 12);
    }
}
danpost danpost

2012/3/11

#
Line 31 sets the image to a trasparent image, but where in your code does it set it to anything else? Add, as the last statement in updateImage(), 'setImage(image);' In fact, I think you want 'image = new GreenfootImage(stringLength, 24);' in line 31.
danpost danpost

2012/3/11

#
Also, change line 19 to 'GreenfootImage image;'. Remove line 20 (not needed). And, move line 21 to between lines 31 and 32.
JayWood860 JayWood860

2012/3/11

#
thank you so much. it's working now, but the image is cut off at the top and on the right side. you can only see the bottom half of the words/number and it only shows the first digit of "int vos". Which variables do i have to change?
danpost danpost

2012/3/11

#
One more thing, your act method has the score incrementing by one each act cycle. Is this what you want (it would run amuck in no time flat)? Maybe you wanted a method for changing the score:
public void add(int changeAmount)
{
    vos += changeAmount;
    updateImage();
}
instead of the current act() method.
danpost danpost

2012/3/11

#
You are trying to put text with a fontSize of 24 in an object that is only 12 in height. Change the 12 in line 53 to 24.
danpost danpost

2012/3/11

#
If the text is being cut off at the end, you may need to change the 10 in line 29 to 12 or 15.
JayWood860 JayWood860

2012/3/11

#
after i've done everything, and i compile, the greenfoot terminal window comes up saying: java.lang.NullPointerException at Score.<init>(Score.java:20) at Space.<init>(Space.java:12) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:520) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411) at greenfoot.core.Simulation.maybePause(Simulation.java:269) at greenfoot.core.Simulation.runContent(Simulation.java:201) at greenfoot.core.Simulation.run(Simulation.java:194) i've still yet to understand the terminal window (as im still learning about greenfoot); what does this mean and what do i do in order to change this?
danpost danpost

2012/3/11

#
Comment out lines 22 through 25 and try again (the problem may be there). Post back with results.
danpost danpost

2012/3/11

#
It appears you did not remove line 20 from the score class
danpost wrote...
Also, change line 19 to 'GreenfootImage image;'. Remove line 20 (not needed). And, move line 21 to between lines 31 and 32.
JayWood860 JayWood860

2012/3/11

#
this is my code for the score class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
/**
 * Write a description of class Score here.
 * 
 * @author Juwan 
 * @version P.Bravo(2)
 */
public class Score extends Actor
{
    private static final Color textColor = new Color(225, 180, 150);
    
    public int vos = 0;
    private String text;
    private int stringLength;
    
    GreenfootImage image;
    Font font = image.getFont();
    public Score()
    {
        this("");
    }
    public Score(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 15;
        
        image = new GreenfootImage(stringLength, 24);
        image.setFont(font.deriveFont(24.0F));
        image.setColor(textColor);
        
        updateImage();
    }
    /**
     * 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(1);
    }
    public int getVOS()
    {
        return vos;
    }
    public void add(int changeAmount)
    {
        vos += changeAmount;
        updateImage();
    }
    public void updateImage()
    {
        image.clear();
        image.drawString(text + vos, 1, 24);
        setImage(image);
    }   
}
i did remove the world reference; and what is wrong with lines 22-25(or 21- 24)? isn't this just the constructor method?
JayWood860 JayWood860

2012/3/11

#
still terminal error message is appearing
danpost danpost

2012/3/11

#
What does the terminal error say now? Still, I see the Font line around line 20; it should be immediately after 'image = new GreenfootImage...'
danpost danpost

2012/3/11

#
How exactly are you going to be using this Score class? Will it be constantly increasing with time, or will it be bumped up when certain actions occur?
JayWood860 JayWood860

2012/3/11

#
i've moved the font reference down. the error message no longer comes up and the score is working perfectly. THANK YOU SO MUCH! and for the score class, i would like it to increase with time and eventually (when i get to it) for the score to increase with time as well as when certain actions occur.
There are more replies on the next page.
1
2