player.java
来自「JAVA游戏编程」· Java 代码 · 共 66 行
JAVA
66 行
package com.brackeen.javagamebook.tilegame.sprites;
import com.brackeen.javagamebook.graphics.Animation;
/**
The Player.
*/
public class Player extends Creature {
private static final float JUMP_SPEED = -.95f;
private boolean onGround;
public Player(Animation left, Animation right,
Animation deadLeft, Animation deadRight)
{
super(left, right, deadLeft, deadRight);
}
public void collideHorizontal() {
setVelocityX(0);
}
public void collideVertical() {
// check if collided with ground
if (getVelocityY() > 0) {
onGround = true;
}
setVelocityY(0);
}
public void setY(float y) {
// check if falling
if (Math.round(y) > Math.round(getY())) {
onGround = false;
}
super.setY(y);
}
public void wakeUp() {
// do nothing
}
/**
Makes the player jump if the player is on the ground or
if forceJump is true.
*/
public void jump(boolean forceJump) {
if (onGround || forceJump) {
onGround = false;
setVelocityY(JUMP_SPEED);
}
}
public float getMaxSpeed() {
return 0.5f;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?