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

2017/5/28

Why won't my scenario run?

SilentScoper8 SilentScoper8

2017/5/28

#
I am trying to make a Pong game, and everything is functioning correctly, but when I try to add a way to keep the score in the game, everything compiles but the scenario isn't running. Here is my code.
import greenfoot.*;
public class PongGame extends World
{
    private int p1score;
    private int p2score;
    private Paddle leftPaddle;
    private Paddle rightPaddle;

    public PongGame()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        p1score = 0;
        p2score = 0;
        addBall();
        addScore();
        addPaddle(true);
        addPaddle(false);
    }

    public void addPaddle(boolean isLeft)
    {
        if (isLeft == true)
        {
            Paddle leftPaddle = new Paddle(true);
            leftPaddle.turn(270);
            addObject(leftPaddle, 40, 200);
        }
        else if (isLeft == false)
        {
            Paddle rightPaddle = new Paddle(false);
            rightPaddle.turn(270);
            addObject(rightPaddle, 560, 200);
        }
    }

    public void addBall()
    {
        Ball ball = new Ball(5);
        addObject(ball, 240, 200);
    }

    public void addScore()
    {
        Score score = new Score();
        addObject(score, 250, 50);
    }

    public int getP1score()
    {
        return p1score;
    }

    public int getP2score()
    {
        return p2score;
    }

    public void setP1score(int score)
    {
        p1score = score;
    }

    public void setP2score(int score)
    {
        p2score = score;
    }

    public String toString(int p1score)
    {
        return Integer.toString(p1score);
    }
}
import greenfoot.*;
import java.awt.Font;

public class Score extends Actor
{
    private String points;
    
    public void act() 
    {
        setText(points);
    }

    public Score()
    {
        PongGame world = (PongGame) getWorld();
        int p1score = world.getP1score();
        int p2score = world.getP2score();
        points = p1score + "\t" + p2score;
        GreenfootImage image = new GreenfootImage(150, 20);
        float fontSize = 12f;
        image.setColor(Color.WHITE);
        image.drawString(points, 2, 15);
        setImage(image);
    }

    public void checkForScore()
    {
        PongGame world = (PongGame) getWorld();
        int margin = getImage().getWidth() / 2;
        if (getX() - margin <= 0)
        {
            int p2score = world.getP2score();
            p2score++;
            world.setP2score(p2score);
        }
        else if (getX() + margin >= 600)
        {
            int p1score = world.getP1score();
            p1score++;
            world.setP1score(p1score);
        }
        setText(points);
    }
    
    public void setText(String text)
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text, 2, 20);
    }
}
import greenfoot.*;
public class Ball extends Actor
{
    private int moveX;
    private int moveY;
    public void act() 
    {
        move();
        bounce();
        checkForScore();
    }

    public Ball()
    {
        moveX = 5;
        moveY = 5;
    }

    public Ball(int speed)
    {
        moveX = speed;
        moveY = speed;
    }

    public void move()
    {
        int newX = getX() + moveX;
        int newY = getY() + moveY;
        setLocation(newX, newY);
    }

    public boolean checkForScore()
    {
        PongGame world = (PongGame) getWorld();
        int margin = getImage().getWidth() / 2;
        if (getX() - margin <= 0)
        {
            int p2score = world.getP2score();
            p2score++;
            world.setP2score(p2score);
            world.removeObject(this);
            world.addBall();
            return true;
        }
        else if (getX() + margin >= 600)
        {
            int p1score = world.getP1score();
            p1score++;
            world.setP1score(p1score);
            world.removeObject(this);
            world.addBall();
            return true;
        }
        else
        {
            return false;
        }
    }

    public void bounce()
    {
        int margin = getImage().getHeight() / 2;
        if (getY() - margin <= 0 || getY() + margin >= 400)
        {
            moveY = -moveY;
        }
        if (isTouching(Paddle.class) == true)
        {
            moveX = -moveX;
        }
    }
}
import greenfoot.*;
public class Paddle extends Actor
{
    private boolean left;
    private int moveDistance;
    public void act() 
    {
        move();
    }

    public Paddle(boolean isLeft)
    {
        left = isLeft;
        moveDistance = 5;
    }

    public Paddle(boolean isLeft, int speed)
    {
        left = isLeft;
        moveDistance = speed;
    }

    public void move()
    {
        if (left == true)
        {
            if (Greenfoot.isKeyDown("w"))
            {
                move(moveDistance);
            }
            else if (Greenfoot.isKeyDown("s"))
            {
                move(-moveDistance);
            }
        }
        else
        {
            if (Greenfoot.isKeyDown("up"))
            {
                move(moveDistance);
            }
            else if (Greenfoot.isKeyDown("down"))
            {
                move(-moveDistance);
            }
        }
    }

    public void moveLeft()
    {
        if (Greenfoot.isKeyDown("w") )
        {
            move(moveDistance);
        }
        else if (Greenfoot.isKeyDown("s"))
        {
            move(-moveDistance);
        }
    }

    public void moveRight()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            move(moveDistance);
        }
        else if (Greenfoot.isKeyDown("down"))
        {
            move(-moveDistance);
        }
    }
}
danpost danpost

2017/5/28

#
Fact 1: an actor must be created prior to it being placed into a world Fact 2: the constructor of an object is executed at the time the object is created This being said, 'getWorld', when used in the constructor of an Actor subclass, must return a 'null' value. Line 15 of the Score class constructor sets 'null' as the value of the 'world' variable. Then, line 16 and 17 tries to call the 'getP!score' and 'getP2score' methods on that 'null' value. Hence, the 'NullPointerException' error you were getting. You could make use of the 'addedToWorld(World) method of the Actor class, which is called when the actor IS placed into a world; but, since you know the score will be zero to zero when the Score object is created, why not just replace lines 15 through 18 with this:
points = "0\t0";
You need to login to post a reply.