Hi guys,
I’m an absolute newbie, and during the past weeks I have started writing a small point and click adventure game.
Until yesterday I have a “game” that was able to load a background and the sprite for the player, and listening to the mouse move the sprite to the point were you click. But I only have one animation, so the player animation was always “walking”.
I have read a bit about how to manage more than one animation using arrays, so now I’m trying to have the same thing but with two animations: one when the player is walking, and another one for when it is not moving at all (in this animation the sprite will blink or something like that). My problem is that now I cannot make it run anymore, right now I have a null pointer exception that I cannot explain… (because I think I didn’t change anything there!)
Exception in thread “main” java.lang.NullPointerException
at InputManagerTest.checkGameInput(InputManagerTest.java:135)
and line 135 is just
player.setVelocityX(velocityX);
The problem is calling the player.anything, but I don’t know why it always give the null pointer exception. but it was working fine before last changes!
Aaaaany help will be super useful for me, and I thank you in advance because I’m sure I am doing hundreds of things incorrectly… but I already spent some hours playing around, looking on my java book and on the forum I am stuck.
I copy the relevant code:
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import graphics.*;
import input.*;
import test.GameCore;
public class InputManagerTest extends GameCore {
public static void main(String[] args) {
new InputManagerTest().run();
}
protected GameAction exit;
protected GameAction moveRight;
protected GameAction pause;
protected InputManager inputManager;
private Player player;
private Image bgImage;
private boolean paused;
private int mouseX;
private Sprite playerSprite;
public void init() {
super.init();
Window window = screen.getFullScreenWindow();
inputManager = new InputManager(window);
createGameActions();
createSprite();
paused = false;
}
/**
Tests whether the game is paused or not.
*/
public boolean isPaused() {
return paused;
}
/**
Sets the paused state.
*/
public void setPaused(boolean p) {
if (paused != p) {
this.paused = p;
inputManager.resetAllGameActions();
}
}
public void update(long elapsedTime) {
// check input that can happen whether paused or not
checkSystemInput();
if (!isPaused()) {
// check game input
checkGameInput();
// update sprite
player.update(elapsedTime);
}
}
/**
Checks input from GameActions that can be pressed
regardless of whether the game is paused or not.
*/
public void checkSystemInput() {
if (pause.isPressed()) {
setPaused(!isPaused());
}
if (exit.isPressed()) {
stop();
}
}
/**
Checks input from GameActions that can be pressed
only when the game is not paused.
*/
public void checkGameInput() {
float velocityX = 0;
if (moveRight.isPressed()) {
player.walk();
player.setState(Player.STATE_WALK);
}
player.setVelocityX(velocityX);
}
public void draw(Graphics2D g) {
// draw background
g.drawImage(bgImage, 0, 0, null);
// draw sprite
g.drawImage(player.getImage(),
Math.round(player.getX()),
Math.round(player.getY()),
null);
}
/**
Creates GameActions and maps them to keys.
*/
public void createGameActions() {
exit = new GameAction("exit",
GameAction.DETECT_INITAL_PRESS_ONLY);
moveRight = new GameAction("moveRight");
pause = new GameAction("pause",
GameAction.DETECT_INITAL_PRESS_ONLY);
inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
inputManager.mapToKey(pause, KeyEvent.VK_P);
inputManager.mapToMouse(moveRight, InputManager.MOUSE_BUTTON_1);
}
/**
Load images and creates the Player sprite.
*/
private void createSprite() {
Image[][] images = new Image[4][];
// load images
bgImage = loadImage("dcity.JPG");
images[0] = new Image[] {
loadImage("titocamina1.PNG"),
loadImage("titocamina2.PNG"),
loadImage("titocamina3.PNG"),
loadImage("titowink3.PNG"),
};
images[1] = new Image[images[0].length];
// create animation
Animation [] playerAnim = new Animation[2];
playerAnim[0] = createPlayerAnim(images[1][0], images[1][1], images[1][2]);
playerAnim[1] = createPlayerAnim(images[1][2], images[1][2], images[1][3]);
playerSprite = new Player(playerAnim[0], playerAnim[1]);
}
private Animation createPlayerAnim(Image player1,Image player2, Image player3)
{
Animation anim = new Animation();
anim.addFrame(player1, 150);
anim.addFrame(player2, 150);
anim.addFrame(player3, 150);
anim.addFrame(player2, 150);
return anim;
}
}
and now the Player:
package input;
import graphics.*;
//import input.InputManager;
/**
The Player class extends the Sprite class to add states
*/
public class Player extends Sprite {
protected InputManager inputManager;
public static final int STATE_WALK = 0;
public static final int STATE_STAND = 1;
public static final float SPEED = .4f;
private int state;
private Animation animWalk;
private Animation animStand;
private int mouseX;
public Player(Animation animWalk, Animation animStand) {
super(animStand);
this.animWalk=animWalk;
this.animStand=animStand;
state = STATE_STAND;
}
/**
Gets the state of the Player;
*/
public int getState() {
return state;
}
/**
Sets the state of the Player;
*/
public void setState(int state) {
if (this.state != state) {
this.state = state;
}
}
/**
Causes the Player to Walk
*/
public void walk() {
mouseX=inputManager.mouseLocation.x;
state = STATE_WALK;
if ((mouseX-(getX()))>10 && mouseX>(getX()+200)){
setVelocityX(+1);
}
else {
setState(STATE_STAND);
}
if ((mouseX-(getX()))<10 && mouseX<getX()){
setVelocityX(-1);
}
else {
setState(STATE_STAND);
}
}
/**
Updates the player's position and animation. Also, sets the
Player's state.
*/
public void update(long elapsedTime) {
// select the correct Animation
Animation newAnim = anim;
if (getVelocityX() < 0) {
newAnim = animWalk;
}
else if (getVelocityX() > 0) {
newAnim = animWalk;
}
else if (getVelocityX() == 0) {
newAnim = animStand;
}
// update the Animation
if (anim != newAnim) {
anim = newAnim;
anim.start();
}
else {
anim.update(elapsedTime);
}
// move player
super.update(elapsedTime);
}
}
and the Sprite class:
package graphics;
import java.awt.Image;
public class Sprite {
protected Animation anim;
// position (pixels)
private float x=0;
private float y=0;
// velocity (pixels per millisecond)
private float dx;
private float dy;
/**
Creates a new Sprite object with the specified Animation.
*/
public Sprite(Animation anim) {
this.anim = anim;
}
/**
Updates this Sprite's Animation and its position based
on the velocity.
*/
public void update(long elapsedTime) {
x += dx * elapsedTime;
y += dy * elapsedTime;
anim.update(elapsedTime);
}
/**
Gets this Sprite's current x position.
*/
public float getX() {
return x;
}
/**
Gets this Sprite's current y position.
*/
public float getY() {
return y=750;
}
/**
Sets this Sprite's current x position.
*/
public void setX(float x) {
this.x = x;
}
/**
Sets this Sprite's current y position.
*/
public void setY(float y) {
this.y = y;
}
/**
Gets this Sprite's width, based on the size of the
current image.
*/
public int getWidth() {
return anim.getImage().getWidth(null);
}
/**
Gets this Sprite's height, based on the size of the
current image.
*/
public int getHeight() {
return anim.getImage().getHeight(null);
}
/**
Gets the horizontal velocity of this Sprite in pixels
per millisecond.
*/
public float getVelocityX() {
return dx;
}
/**
Gets the vertical velocity of this Sprite in pixels
per millisecond.
*/
public float getVelocityY() {
return dy;
}
/**
Sets the horizontal velocity of this Sprite in pixels
per millisecond.
*/
public void setVelocityX(float dx) {
this.dx = dx;
}
/**
Sets the vertical velocity of this Sprite in pixels
per millisecond.
*/
public void setVelocityY(float dy) {
this.dy = dy;
}
/**
Gets this Sprite's current image.
*/
public Image getImage() {
return anim.getImage();
}
}