Hi, everyone! I'm having some trouble doing a very simple thing. I'm trying to create an Entity that only moves left and right at a specific speed. For some strange reason, the program keeps crashing whenever it gets to the Gameplay state I've created, more specifically right after the entering transition ends. here's the Player class:
Code:
public class Player extends Entity {
public final String MOVE_LEFT= "left";
public final String MOVE_RIGHT= "right";
public final String CMD_FIRE= "fire";
public boolean bulletOnScreen= false;
public Player(int startX, int startY) throws SlickException {
super(startX, startY);
defineCommands();
this.setGraphic(ResourceManager.getImage("spaceInvadersMain"));
setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
this.addType(SOLID);
}
public void render(GameContainer gameContainer, Graphics graphics) throws SlickException {
super.render(gameContainer, graphics);
}
public void update(GameContainer gameContainer, int delta) throws SlickException {
super.update(gameContainer, delta);
dealWithInput();
}
private void dealWithInput() {
if(check(MOVE_LEFT)) {
moveLeft();
} else if(check(MOVE_RIGHT)) {
moveRight();
} else if(pressed(CMD_FIRE)) {
fireBullet();
} else {
this.speed.x= 0;
}
}
private void moveLeft() {
if(collide(SOLID, this.x - this.speed.x, this.y)== null && this.x - this.speed.x > 30) {
this.speed.x= -6;
}
}
private void moveRight() {
if(collide(SOLID, this.x + this.speed.x, this.y)== null && this.x + this.speed.x + (float) this.height < 610) {
this.speed.x= 6;
}
}
private void fireBullet() {
if(!bulletOnScreen) {
}
}
private void defineCommands() {
define(MOVE_LEFT, Input.KEY_LEFT);
define(MOVE_RIGHT, Input.KEY_RIGHT);
define(CMD_FIRE, Input.KEY_SPACE);
}
}
This is the error I'm getting:
Code:
Wed May 02 16:55:04 BST 2012 ERROR:null
java.lang.NullPointerException
at actors.Player.dealWithInput(Player.java:44)
at actors.Player.update(Player.java:33)
at it.randomtower.engine.World.update(World.java:120)
at states.GameplayState.update(GameplayState.java:45)
at org.newdawn.slick.state.StateBasedGame.update(StateBasedGame.java:268)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at game.SMGame.main(SMGame.java:29)
I've looked to the Pong example's code in github, and I just don't understand what I'm doing wrong. Any help would be appreciated!