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

2013/12/8

Non Static method cannot be referenced from static.

Eir3 Eir3

2013/12/8

#
I'm trying to increase the score every time I create a mine but it keeps giving me this error.
non static method addScore(int) cannot be referenced from static context
but what do i change to get this to work?
public class BubbleWorld extends World
{
    private int loop = 0;
    GreenfootSound backgroundMusic = new GreenfootSound("bubbletheme.mp3");
    public void endGame()
    {
        GameOver go = new GameOver();
        Greenfoot.setWorld(go);
    }
    /**
     * Constructor for objects of class BubbleWorld.
     * 
     */
    public BubbleWorld()
    {    
        super(560, 560, 1); 
        if (loop == 0)
        {
            backgroundMusic.playLoop();
            loop++;
        }
        prepare();
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Bubble bubble = new Bubble();
        addObject(bubble, 269, 498);
        Cursor cursor = new Cursor();
        addObject(cursor, 204, 100);
        Shuriken shuriken = new Shuriken();
        addObject(shuriken, 451, 59);
        Deck deck = new Deck();
        addObject(deck, 273, 557);
        removeObject(bubble);
        Bubble bubble2 = new Bubble();
        addObject(bubble2, 291, 472);
        removeObject(bubble2);
        Bubble bubble3 = new Bubble();
        addObject(bubble3, 278, 456);
        ScoreBoard scoreboard = new ScoreBoard();
        addObject(scoreboard, 56, 542);
    }

    public void act()
    {
        createMine();
        createPowerUp();
    }
    public void createMine()
    {
        if (Greenfoot.getRandomNumber(1000) < 23)
        {
            Mine e = new Mine();
            addObject(e, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
        }
    }  
    public void createPowerUp()
    {
      if (Greenfoot.getRandomNumber(1000) < 1)
        {
            PowerUp f = new PowerUp();
            addObject(f, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
            ScoreBoard.addScore(10);
        }  
    }
}
public class ScoreBoard extends Actor
{
    private GreenfootImage board;
    private GreenfootImage sb;
    private int score = 0;
    private String label = "Score : ";
    public ScoreBoard()
    {
        super();
        
        int boardWidth = 150; // I set the width
        int boardHeight = 30; // i set the height to a variable
        int boardTransparency = 125; // put in some transparency for effect
        
        board = new GreenfootImage(boardWidth, boardHeight);
        board.setColor(Color.gray);
        board.setTransparency(boardTransparency);
        board.fillRect(0, 0, boardWidth, boardHeight);
        this.setImage(board);
        
        update();
    }
    public void act() 
    {
        // Add your action code here.
    } 
    public int getScore()
    {
        return score;
    }
    public void addScore(int points)
    {
        score += points;
        update();
    }
    private void update()
    {
        sb = new GreenfootImage(board);
        sb.drawImage(new GreenfootImage(label + score, 18, Color.WHITE, new Color(0,0,0,0)),25,5);
        this.setImage(sb);
    }
}
danpost danpost

2013/12/8

#
Line 68 of your world class is calling addScore on a class and not on a ScoreBoard object. The class does not in itself have a 'score' field; but, each ScoreBoard object created from the class will have one of its own. The class is just a blueprint from which the objects are created and give those objects their state and behavior through their given fields and methods. You should probably have an instance field in your world class to hold the ScoreBoard object so you can call the method on the object when needed. I noticed that you have an instance field in your world class called 'loop' which is only used within the constructor. Basically, because the constructor is only called once, the use of 'loop' has no purpose. You can remove lines 3, 17, 18, 20 and 21 without causing any change in operation.
Eir3 Eir3

2013/12/8

#
How would you recommend that I get it to add 10 points for each mine created. Im not a fully flown programmer but have an ok knowledge on the above code. Regarding the loop also, when I have a game over, it moves to a game over screen. Once the player clicks restart in the game over screen the song will be played again, after i implemented that loop it was ok. thanks for your reply
danpost danpost

2013/12/8

#
For the music, use this:
if (!backgroundMusic.isPlaying()) backgroundMusic.playLoop();
Add the ten points from within the 'if' block of the 'createMine' method.
Eir3 Eir3

2013/12/8

#
ScoreBoard scoreboard = new ScoreBoard(); does this not create a scoreboard in world. then Scoreboard.addScore(10); calls addScore in the Scoreboard class and adds ten? should i put ScoreBoard scoreboard = new ScoreBoard(); in the if block of the createMine method? im not sure what i have to do here, apologies!
danpost danpost

2013/12/8

#
What you stated could be taken ambiguously, making it difficult to say 'Yes' or 'No'. If the first line in your last post is declaring an instance field in your world class, then 'Yes'; it creates a ScoreBoard object and assigns it to the instance field 'scoreboard' of your world instance. However, the class ('ScoreBoard') does not have a value for 'scoreboard', only the instance of that class do (the objects created from the class). Therefore, you cannot use the class name 'ScoreBoard' to call the method on; you must use the name of the ScoreBoard object that was created ('scoreboard').
Eir3 Eir3

2013/12/8

#
i have added to the beginning of code
private ScoreBoard scoreboard;
and changed the createMine add score to
scoreboard.addScore(10);
i thne get an error
java.lang.NullPointerException
	at BubbleWorld.createMine(BubbleWorld.java:70)
	at BubbleWorld.act(BubbleWorld.java:61)
	at greenfoot.core.Simulation.actWorld(Simulation.java:574)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:509)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/12/8

#
Your constructor should have this:
scoreboard = new ScoreBoard();
not this:
ScoreBoard scoreboard = new ScoreBoard();
Eir3 Eir3

2013/12/8

#
Thank you very much for your replies. :)
You need to login to post a reply.