This week I myself have concentrated on getting a pseudo gravity system into the game which I severely underestimated on the complexity of the coding that would have to go into this. To start off I figured out how to get a smooth jump from the character instead of just being able to walk up and down whenever the user pleased meaning that they can press the jump key once and the character will the perform the jump, the complex part of this was handling collisions while in the air allowing for the character to come back down if they were to collide with a block above them while jumping, but this was all figured out over the week and has been successfully implemented into the game along with the feature of the character changing its sprite depending on the direction it is facing, this means that if the user presses the left key the character will now face left but there is still no animation within the game, all of these features have been implemented into the game using the code below.
Jumping Gravity + Sprite Change
if ((input.isKeyPressed(Input.KEY_UP) || input.isKeyPressed(Input.KEY_W)) && !jumping){
if(lastKeyLeft){
player = jumpLeft;
} else {
player = jumpRight;
}
gravSpeeds = -12;
jumping = true;
}
if(jumping && gravSpeeds < 12) {
gravSpeeds += 0.5;
}
if (jumping == false){
gravSpeeds = 12;
if (lastKeyLeft == true){
player = moveLeft;
} else {
player = moveRight;
}
}
y += gravSpeeds;
origY = y - gravSpeeds;
playerPoly.setY(y);
if (entityCollisionWith()){
if (jumping && gravSpeeds < ((gravSpeeds * (-1)))){
gravSpeeds = 0;
gravSpeeds += 0.5;
y += gravSpeeds;
}
if ( gravSpeeds > 0) {
gravSpeeds = 0;
jumping = false;
y = origY;
playerPoly.setY(y);
}
}
if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S))
{
y += floatDelta;
playerPoly.setY(y);
if (entityCollisionWith()){
y -= floatDelta;
playerPoly.setY(y);
}
}
if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A))
{
lastKeyLeft = true;
if (jumping){
player = jumpLeft;
jumpLeft.update(delta);
} else {
player = moveLeft;
moveLeft.update(delta);
}
x -= floatDelta;
playerPoly.setX(x);
if (entityCollisionWith()){
x += floatDelta;
playerPoly.setX(x);
}
} else {
moveLeft.setCurrentFrame(0);
}
if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D))
{
lastKeyLeft = false;
if (jumping){
player = jumpRight;
jumpRight.update(delta);
} else {
player = moveRight;
moveRight.update(delta);
}
x += floatDelta;
playerPoly.setX(x);
if (entityCollisionWith()){
x -= floatDelta;
playerPoly.setX(x);
}
}
else {
moveRight.setCurrentFrame(0);
}
Animation Code
player = new Animation();
player.setAutoUpdate(true);
Image [] walkRight = {new Image ("data/Sprites/CharStand.png"),
new Image ("data/Sprites/CharStep1.png"),
new Image ("data/Sprites/CharStep2.png")};
Image [] walkLeft = {new Image ("data/Sprites/CharStandBACKWARDS.png"),
new Image ("data/Sprites/CharStep1BACKWARDS.png"),
new Image ("data/Sprites/CharStep2BACKWARDS.png")};
Image [] jumpingLeft = {new Image ("data/Sprites/CharJumpBACKWARDS.png")};
Image [] jumpingRight = {new Image ("data/Sprites/CharJump.png")};
moveRight = new Animation(walkRight, 200, false);
moveLeft = new Animation(walkLeft , 200, false);
jumpRight = new Animation(jumpingRight, 200, false);
jumpLeft = new Animation(jumpingLeft, 200, false);
jumpRight.setPingPong(true);
jumpLeft.setPingPong(true);
moveRight.setPingPong(true);
moveLeft.setPingPong(true);
player = moveRight;