I'm trying to make a power-up that permanently speeds up a paddle that you move around. Originally I had a problem with the paddle infinitely speeding up, so I wanted to access the speeding up method from the power-up itself. So I checked the tutorial on how to access an object from another object, which produced no errors. However, the moment I hit the power-up, an error was thrown:
java.lang.NullPointerException
at PaddleSpeedUp.activatePowerUp(PaddleSpeedUp.java:29)
at PaddleSpeedUp.removePowerUp(PaddleSpeedUp.java:23)
at PaddleSpeedUp.act(PaddleSpeedUp.java:18)
Here's the code for the Paddle:
This is the Power-up's:
And the code for the World:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Paddle extends Actor { public int paddleSpeed = 10; int speedy; int i; public void act() { movement(); speedy = PaddleSpeedUp.speedUp; } public void movement(){ if(Greenfoot.isKeyDown("left")){ setLocation(getX()-paddleSpeed, getY()); } if(Greenfoot.isKeyDown("right")){ setLocation(getX()+paddleSpeed, getY()); } } public void updateSpeed(){ paddleSpeed = paddleSpeed + speedy; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class PaddleSpeedUp extends Actor { public static int speedUp; /** * Act - do whatever the Paddle_Speed_Up wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { removePowerUp(); } public void removePowerUp(){ if(isTouching(Ball.class)){ getWorld().removeObject(this); activatePowerUp(); } } public void activatePowerUp(){ speedUp += 1; firstLevel theWorld = (firstLevel) getWorld(); Paddle paddle = theWorld.getPaddle(); paddle.updateSpeed(); } public static int getSpeedUp(){ return speedUp; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class firstLevel extends World { /** * Constructor for objects of class MyWorld. * */ public int spawnTimer; private int timerThing = 600; private Paddle thePaddle; GreenfootSound backgroundMusic = new GreenfootSound("03_Otherworldly_Corridor.mp3"); public firstLevel() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); //backgroundMusic.playLoop(); prepare(); } public void act(){ randomlySpawn(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Ball ball = new Ball(); addObject(ball,239,144); thePaddle = new Paddle(); addObject(thePaddle,243,296); Ground ground = new Ground(); addObject(ground,300,399); } public void randomlySpawn(){ spawnTimer = (spawnTimer+1)%timerThing; // repeat every 10 seconds (about) if (spawnTimer == 0) // at each timer reset { timerThing = getRandomNumber(10, 500); addObject(new PaddleSpeedUp(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290)); } } public int getRandomNumber(int start, int end){ int normal = Greenfoot.getRandomNumber(end-start+1); return normal+start; } public Paddle getPaddle(){ return thePaddle; } }