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

2017/5/5

non-static method cannot be called in static context. Can anyone help me?

CodeBoy CodeBoy

2017/5/5

#
I'm making a typing game and am trying to call the method to add 1 to my score from the world. The code errors whenever I try to call the method though. Can anyone help me fix this. World class
if (Greenfoot.isKeyDown("enter"))
        {
            if (Typed == WordBank1.word)
            {
                Score.addScore();
            }
            else
            {
                Lives.minusLives();
            }
Score class
public class Score extends Actor
{
    
    public static int score = 0;
    public static GreenfootImage image;
    
    public Score()
    {
        image = new GreenfootImage(160, 50);
        image.setColor(Color.RED);
        image.setFont(new Font("Calibri", Font.BOLD, 18));
        printScore();
    }
    public void addScore()
    {
        score = score + 1;
        printScore();
    }
    public int getScore()
    {
        return score;
    }
    public void NextL()
    {
        if (score == 30)
        {
            Greenfoot.setWorld(new Forest());
        }
        if (score == 70)
        {
            Greenfoot.setWorld(new Castle());
        }
        if (score == 120)
        {
            Greenfoot.setWorld(new GameWin());
        }
    }
    public void printScore()
    {
        image.clear();
        image.drawString("Score: "+score, 10, 25);
        setImage(image);
    }
}
danpost danpost

2017/5/5

#
By having the 'score' field as static, you are limiting the Score class to one score and only one unique object per world. As such, you might as well just have an Object type subclass with a static field and static methods:
import greenfoot.*;
import java.awt.Color; // remove if using greenfoot.Color
import java.awt.Font; // remove if using greenfoot.Font
 
public final class Score
{
    private static Actor scoreDisplay;
    private static int score;
 
    private static void updateDisplay()
    {
        if (scoreDisplay == null) scoreDisplay = new ScoreDisplay();
        GreenfootImage image = new GreenfootImage(160, 50);
        image.setColor(Color.RED);
        image.setFont(new Font("Calibri", Font.BOLD, 18));
        image.drawString("Score: "+score, 10, 25);
        scoreDisplay.setImage(image);
    }
 
    public static void addToWorld(World world, int x, int y)
    {
        updateDisplay();
        world.addObject(scoreDisplay, x, y);
    }
 
    public static void setScore(int value)
    {
        score = value;
        updateDisplay();
    }
 
    public static int getScore()
    {
        return score;
    }
 
    public static void add(int amount)
    {
        score += amount;
        updateDisplay();
    }
 
    private static class ScoreDisplay extends Actor
    {
        public void setLocation(int x, int y) {}
    }
}
The check for next level should be done elsewhere (like maybe in the world act method. With the above class, all public methods are accessible by the name of the class (the class is abstract and everything in the class is static). Your initial world constructor should use the following to initialize and add the score display into the world:
Score.setScore(0);
Score.addToWorld(this, 100, 20); // adjust location as desired
From any class and in any world, you can set, get or add to the score using 'Score.publicMethodName'. You can even add the score display into any world without losing the score value.
CodeBoy CodeBoy

2017/5/8

#
Thank you my score class is working now.
You need to login to post a reply.