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

2016/1/24

Please help score counter doesn't work

Milan00 Milan00

2016/1/24

#
Greenfoot Terminal Window: java.lang.NullPointerException at AvoiderWorld.act(AvoiderWorld.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:600) at greenfoot.core.Simulation.runOneLoop(Simulation.java:535) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) In game when enemy is spawned it should add 5 points this is part of the source code
 public void act()
    {
        if(Greenfoot.getRandomNumber(1000) < 20)
        {
            Negativac e = new Negativac();
            addObject(e, Greenfoot.getRandomNumber(getWidth()-20)+10, - 30);
            theCounter.addScore(5);
        }

    }  
theCounter.addScore(5) is 37.
danpost danpost

2016/1/25

#
You will need to show the entire AvoiderWorld class code for us to determine why you are getting that exception.
Milan00 Milan00

2016/1/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */ 
public class AvoiderWorld extends World
{
    
    private GreenfootSound SpaceMusic;
    private ScoreBoard theCounter;
    
    /**
     * Constructor for objects of class AvoiderWorld.
     * 
     */
    public AvoiderWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 700, 1); 
        
        GreenfootSound SpaceMusic = new GreenfootSound("sounds/SpaceMusic.mp3");
        SpaceMusic.playLoop();
        
        
    }

    public void act()
    {
        if(Greenfoot.getRandomNumber(1000) < 20)
        {
            Negativac e = new Negativac();
            addObject(e, Greenfoot.getRandomNumber(getWidth()-20)+10, - 30);
            theCounter.addScore(5);
        }

    }   
    

    public void endGame()
    {
        SpaceMusic.stop();
        GameOverScreen go = new GameOverScreen();
        Greenfoot.setWorld(new GameOverScreen());
    }    

} 
Also if you can check this code because for some reason my sound won't stop when i die And this is ScoreBoard Actor
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo
import java.awt.Color;
/**
 * Write a description of class ScoreBoard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

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;
        int boardHeight = 30;
        int boardTransparency = 125;
        
        board = new GreenfootImage(boardWidth, boardHeight);
        board.setColor(Color.RED);
        board.setTransparency(boardTransparency);
        board.fillRect(0, 0, boardWidth, boardHeight);
        this.setImage(board);
        
        update();
    }
    
    /**
     * Act - do whatever the ScoreBoard 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.
    }   
    
    public int getScore()
    {
        return score;
    }    
    
    public void addScore(int pts)
    {
        score += pts;
        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

2016/1/25

#
Both issues deal with assigning values to your fields -- 'theCounter' and 'SpaceMusic'. Nowhere is 'theCounter' being assigned anything and neither is 'SpaceMusic', actually. It may appear to you that 'SpaceMusic' is being assigned a value; but, you have two 'SpaceMusic' variables in your code -- the one local to the AvoidWorld constructor (declared at line 24) and the instance field (declared at line 12). The one assigned a value (at line 24) is valid only until the constructor finishes. So, the one that is valid throughout the life of the AvoiderWorld object is never assigned a value. Do not declare a new variable (at line 24) if you want to refer to the declared instance field (at line 12).
Milan00 Milan00

2016/1/25

#
Can you tell me what should i write i need to make this work fast.
danpost danpost

2016/1/25

#
For the sound, change line 24 to this:
SpaceMusic = new GreenfootSound("sounds/SpaceMusic.mp3");
For the counter, a ScoreBoard object needs to be created and assigned to the 'theCounter' field. That same object should also be the ScoreBoard object that is added into the world.
Milan00 Milan00

2016/1/25

#
I fixed the counter but music still plays when it should not . :/
danpost danpost

2016/1/25

#
Milan00 wrote...
I fixed the counter but music still plays when it should not . :/
Show the method (and indicate the class it is in) where you call the 'endGame' method of your AvoiderWorld class.
Milan00 Milan00

2016/1/25

#
i somehow fixed the music too :D
You need to login to post a reply.