基本上这是主类中的主要事件(记录用户输入)。
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move up");
break;
case KeyEvent.VK_DOWN:
currentSprite = characterDown;
if (knight.isJumped() == false){
knight.setDucked(true);
knight.setSpeedX(0);
}
break;
case KeyEvent.VK_RIGHT:
knight.moveRight();
knight.setMovingRight(true);
knight.setMOVESPEED(5);
knight.setMovingRight(false);
break;
case KeyEvent.VK_SPACE:
knight.jump();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Stop moving up");
break;
case KeyEvent.VK_DOWN:
currentSprite = character;
knight.setDucked(false);
break;
case KeyEvent.VK_RIGHT:
knight.stopRight();
knight.setMOVESPEED(5);
break;
case KeyEvent.VK_SPACE:
break;
}
}
这是背景根据我设定的移动速度滚动的部分。
//Scrolls Background accordingly.
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (speedX > 0 && centerX > 200) {
bg1.setSpeedX(-getMOVESPEED());
bg2.setSpeedX(-getMOVESPEED());
}
嘿伙计们,整个角色课都希望它有所帮助
package com.jisc.framework;
import java.awt.Rectangle; import java.awt.Graphics;
公共阶级骑士{
final int JUMPSPEED = -15;
private int MOVESPEED = 5;
final int GROUND = 202;
private int centerX = 350;
private int centerY = GROUND;
private boolean jumped = false;
private boolean movingLeft = false;
private boolean movingRight = false;
private boolean ducked = false;
private static Background bg1 = Main.getBg1();
private static Background bg2 = Main.getBg2();
private int speedX = 0;
private int speedY = 0;
public void update() {
// Moves Character or Scrolls Background accordingly.
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (speedX > 0 && centerX > 200) {
bg1.setSpeedX(-getMOVESPEED());
bg2.setSpeedX(-getMOVESPEED());
}
// Updates Y Position
if (centerY + speedY >= 202) {
centerY = 202;
}else{
centerY += speedY;
}
// Handles Jumping
if (jumped == true) {
speedY += 1;
if (centerY + speedY >= 202) {
centerY = 202;
speedY = 0;
jumped = false;
}
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
}
public void moveRight() {
if (ducked == false) {
speedX = MOVESPEED;
if (MOVESPEED >= 5){
MOVESPEED ++;
}
if (MOVESPEED >= 7 ){
MOVESPEED = 0;
}
}
}
public void moveLeft() {
if (ducked == false) {
speedX = -getMOVESPEED();
}
}
public void stopRight() {
setMovingRight(false);
stop();
}
public void stopLeft() {
setMovingLeft(false);
stop();
}
public void stop() {
if (isMovingRight() == false && isMovingLeft() == false) {
speedX = 0;
}
if (isMovingRight() == false && isMovingLeft() == true) {
moveLeft();
}
if (isMovingRight() == true && isMovingLeft() == false) {
moveRight();
}
}
public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public boolean isJumped() {
return jumped;
}
public int getSpeedX() {
return speedX;
}
public int getSpeedY() {
return speedY;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setJumped(boolean jumped) {
this.jumped = jumped;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public boolean isDucked() {
return ducked;
}
public void setDucked(boolean ducked) {
this.ducked = ducked;
}
public boolean isMovingRight() {
return movingRight;
}
public void setMovingRight(boolean movingRight) {
this.movingRight = movingRight;
}
public boolean isMovingLeft() {
return movingLeft;
}
public void setMovingLeft(boolean movingLeft) {
this.movingLeft = movingLeft;
}
public int getMOVESPEED() {
return MOVESPEED;
}
public void setMOVESPEED(int mOVESPEED) {
MOVESPEED = mOVESPEED;
}
}
答案 0 :(得分:2)
使用布尔标志。这适用于keyPressed
方法。
private boolean canMoveUp = true; // same for up, down, etc.
...
case KeyEvent.VK_UP:
if (canMoveUp)
{
System.out.println("Move up");
canMoveUp = false;
}
break;
仅此一项只能工作一次。释放钥匙时,您必须再次移动。这适用于keyReleased
方法。
case KeyEvent.VK_UP:
System.out.println("Stop moving up");
canMoveUp = true;
break;
这将确保您只需按一次键移动一次,并允许您多次按该键。