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
1
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| 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; }
public boolean isPaused() { return paused; }
public void setPaused(boolean p) { if (paused != p) { this.paused = p; inputManager.resetAllGameActions(); } }
public void update(long elapsedTime) { checkSystemInput();
if (!isPaused()) { checkGameInput();
player.update(elapsedTime); } }
public void checkSystemInput() { if (pause.isPressed()) { setPaused(!isPaused()); } if (exit.isPressed()) { stop(); } }
public void checkGameInput() { float velocityX = 0;
if (moveRight.isPressed()) { player.walk(); player.setState(Player.STATE_WALK); } player.setVelocityX(velocityX);
}
public void draw(Graphics2D g) { g.drawImage(bgImage, 0, 0, null);
g.drawImage(player.getImage(), Math.round(player.getX()), Math.round(player.getY()), null); }
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);
}
private void createSprite() { Image[][] images = new Image[4][]; 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]; 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package input;
import graphics.*;
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; }
public int getState() { return state; }
public void setState(int state) { if (this.state != state) { this.state = state; } }
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); } }
public void update(long elapsedTime) { Animation newAnim = anim; if (getVelocityX() < 0) { newAnim = animWalk; } else if (getVelocityX() > 0) { newAnim = animWalk; } else if (getVelocityX() == 0) { newAnim = animStand; }
if (anim != newAnim) { anim = newAnim; anim.start(); } else { anim.update(elapsedTime); } super.update(elapsedTime); }
} |
and the Sprite class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| package graphics;
import java.awt.Image;
public class Sprite {
protected Animation anim; private float x=0; private float y=0; private float dx; private float dy;
public Sprite(Animation anim) { this.anim = anim; }
public void update(long elapsedTime) { x += dx * elapsedTime; y += dy * elapsedTime; anim.update(elapsedTime); }
public float getX() { return x; }
public float getY() { return y=750; }
public void setX(float x) { this.x = x; }
public void setY(float y) { this.y = y; }
public int getWidth() { return anim.getImage().getWidth(null); }
public int getHeight() { return anim.getImage().getHeight(null); }
public float getVelocityX() { return dx; }
public float getVelocityY() { return dy; }
public void setVelocityX(float dx) { this.dx = dx; }
public void setVelocityY(float dy) { this.dy = dy; }
public Image getImage() { return anim.getImage(); } } |