gpt4 book ai didi

java - java关键监听器问题确实很新

转载 作者:行者123 更新时间:2023-12-02 05:46:54 25 4
gpt4 key购买 nike

快速问题,基本上任何人都可以告诉我如何制作它,这样当我在java中按下“所说”键时,它就会执行与我按住它时相同的任务。例如,一旦我单击某个键,他就会移动一定的距离,但他移动的距离与我按住该键的时间成正比。我需要的是,即使我按下它 0.5 秒或 20 秒,它也会移动相同的距离。

基本上这是我在主类中的关键事件(记录用户输入)。

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;

导入java.awt.矩形;导入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;
}

}

最佳答案

使用 boolean 标志。这将出现在您的 keyPressed 方法中。

private boolean canMoveUp = true; // same for up, down, etc.
...
case KeyEvent.VK_UP:
if (canMoveUp)
{
System.out.println("Move up");
canMoveUp = false;
}
break;

仅此方法只能起作用一次。当松开 key 时,您必须能够再次移动。这将出现在您的 keyReleased 方法中。

case KeyEvent.VK_UP:
System.out.println("Stop moving up");
canMoveUp = true;
break;

这将确保您在按下某个键时仅移动一次,并允许您按该键任意多次。

关于java - java关键监听器问题确实很新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23976097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com