@Override public void reactToTimer(long l) { if (moveRight) { movement = Math.min(speedX, checkDistanceX(speedX)); moveRight = false; globe.animMoveAllRel(movement, 0, "Player"); xTopLeft += movement; } else if (moveLeft) { movement = Math.max(-1 * speedX, checkDistanceX(-1 * speedX)); moveLeft = false; globe.animMoveAllRel(movement, 0, "Player"); xTopLeft += movement; } if (playerIsJumping) { speedY += gravity; if (speedY > 0) { speedY = Math.min(speedY, checkDistanceY(speedY)); } else if (speedY < 0) { speedY = Math.max(speedY, checkDistanceY(speedY)); } globe.animMoveAllRel(0, speedY, "Player"); yTopLeft += speedY; } else { speedY = 0; speedY = Math.max(speedY, checkDistanceY(gravity * 5)); globe.animMoveAllRel(0, speedY, "Player"); yTopLeft += speedY; } b.updatePosition(xTopLeft, yTopLeft); globe.repaintPanel(); }
@Override public void reactToTimer(long l) { if (player_is_jumping) { if (playerIsOnGround()) { player_is_jumping = false; } else { player_velocity_Y += gravity; player_Y += player_velocity_Y; globe.animMoveAllRel(0, player_velocity_Y, "Player"); } } if (player_X + player_size >= globe.getWidth() || player_X <= 0) { player_velocity_X = 0; } if (player_is_moving_left) { /* If left key was pressed */ // Check if player is stationary or already moving left if (player_velocity_X <= 0 && player_velocity_X > -1 * max_velocity) { player_velocity_X += -1 * acceleration; } // If player is moving right else if (player_velocity_X > 0) { player_velocity_X += -5 * acceleration; } // If player is in bounds moving left if (player_X > 0) { player_X += player_velocity_X; globe.animMoveAllRel(player_velocity_X, 0, "Player"); } player_is_moving_left = false; } else if (player_is_moving_right) { /* If right key was pressed */ // If player is stationary or already moving right if (player_velocity_X >= 0 && player_velocity_X < max_velocity) { player_velocity_X += acceleration; } // If player is moving left else if (player_velocity_X < 0) { player_velocity_X += 5 * acceleration; } // If the player is in bounds moving right if (player_X + player_size < globe.getWidth()) { player_X += player_velocity_X; globe.animMoveAllRel(player_velocity_X, 0, "Player"); } player_is_moving_right = false; } else { /* If no X direction key was pressed */ if (player_velocity_X > 0) { player_velocity_X += -2 * acceleration; player_X += player_velocity_X; globe.animMoveAllRel(player_velocity_X, 0, "Player"); } else if (player_velocity_X < 0) { player_velocity_X += 2 * acceleration; player_X += player_velocity_X; globe.animMoveAllRel(player_velocity_X, 0, "Player"); } } globe.repaintPanel(); }