Hello everyone! Today, I’ve decided to start working on my game. I had some old code for player movement that I wanted to see if I could port over, and here it is. Unfortunately, it keeps throwing me a ‘NullPointerException’ error and crashing the sketch. If anyone could help me, please do!
int[] keys; //I TRIED BOOLEAN AS I THOUGHT IT WOULD WORK BETTER BUT NO DICE.
void keyPressed() {
keys[keyCode] = 1; //IT ERRORS HERE
//print(keyCode);
// print(key);
}
void keyReleased() {
keys[keyCode] = 0; //AND HERE
//AND BASICALLY ANYWHERE I CALL keys[].
}
void PlayerMovement() {
if (keys[65] == 1 || keys[37] == 1) {
accl.x-=ship_acc_amount;
} else if (keys[68] == 1 || keys[39] == 1) {
accl.x+=ship_acc_amount;
}
if (keys[87] == 1|| keys[38] == 1) {
accl.y-=ship_acc_amount;
//pship.y += tempy;
} else if (keys[83] == 1 || keys[40] == 1) {
accl.y+=ship_acc_amount;
}
//ON KEY PRESS, INCREASE THE PLAYER'S ACCELERATION BY THE ACCELERATION AMOUNT.
pship.x+=accl.x;
pship.y+=accl.y;
//ADD THE ACCELERATION FORCE TO THE PLAYER
//ENSURE SPEED CAP:
if (keys[83] == 1 || keys[40] == 1 || keys[87] == 1 || keys[38] == 1 || keys[68] == 1 || keys[39] == 1 || keys[65] == 1 || keys[37] == 1) {
//unimportant speed cap code
}
};
Normally I would set the value of the keycode in the array to true, but it didn’t work so I tried an integer value instead. I think it could be that the value assigned is undefined? But I’m not too sure.