Java 游戏动作执行题
Java Game actionPerformed questions
大家好,最近我做了一个类似于涂鸦跳跃的游戏,jumper/object 跳到上面的下一个平台,如果你摔倒了,你就输了。所以无论如何我的游戏都可以运行,但有一个问题,我已经做到了,如果你按下 space 条,jumper/object 就会跳起来,好的,问题来了:如果你一直按住space bar 然后跳线会一直向上移动,直到我松开 space bar,你是怎么做到的,当你按住 space bar 时 jumper/object 只跳跃而不是继续上升直到我放开 space 条?
这是我的代码中发生的事情:
这是我执行的动作class跳线向上或向下跳动的地方
static boolean gameplay=false;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (gameplay==true){
if (jumper==true){
DoodleHeight+20;
}
if (jumper==false){
DoodleHeight=DoodleHeight-10;
}
这里有控制跳跃的按键
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=false;
}
}
你会如何解决这个问题?我需要帮助,但我想不通。
这是我的带有标志的代码:
import java.awt.event.*;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class DoodleJumpp extends JPanel implements ActionListener,KeyListener{
static JFrame f;
static int width=1200, height=930;
static int DoodleW=500, DoodleH=500;
static int DoodlePlatformWidth=200, DoodlePlatformHeight=400;
static int DoodlePlatformWidth1=400, DoodlePlatformHeight1=530;
static int DoodlePlatformWidth2=900, DoodlePlatformHeight2=874;
static int DoodlePlatformWidth3=345, DoodlePlatformHeight3=643;
static int DoodlePlatformWidth4=711, DoodlePlatformHeight4=957;
static boolean rightjumper,leftjumper;
static boolean jumper;
static boolean test=true;
static boolean gameplay=true;
public void paintComponent (Graphics g){
g.setColor(Color.green);
g.fillRect(DoodlePlatformWidth, DoodlePlatformHeight,200,30);
g.fillRect( DoodlePlatformWidth1, DoodlePlatformHeight1,200,30);
g.fillRect( DoodlePlatformWidth2, DoodlePlatformHeight2,200,30);
g.fillRect( DoodlePlatformWidth3, DoodlePlatformHeight3,200,30);
g.fillRect( DoodlePlatformWidth4, DoodlePlatformHeight4,200,30);
g.setColor(Color.blue);
g.fillRect(DoodleW, DoodleH,50,50);
}
public static void main(String a[]){
DoodleJumpp D = new DoodleJumpp();
f = new JFrame();
D.init();
f.add(D);
f.setSize(width,height);
f.setVisible(true);
f.repaint();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t=new Timer(10,D);
t.start();
}
public void init (){
this.addKeyListener(this);
setFocusable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (gameplay==true){
if (jumper==true&&test==false){
DoodlePlatformHeight=DoodlePlatformHeight+20;
DoodlePlatformHeight1=DoodlePlatformHeight1+20;
DoodlePlatformHeight2=DoodlePlatformHeight2+20;
DoodlePlatformHeight3=DoodlePlatformHeight3+20;
DoodlePlatformHeight4=DoodlePlatformHeight4+20;
}
if (jumper==false&&test==false){
DoodlePlatformHeight=DoodlePlatformHeight-10;
DoodlePlatformHeight1=DoodlePlatformHeight1-10;
DoodlePlatformHeight2=DoodlePlatformHeight2-10;
DoodlePlatformHeight3=DoodlePlatformHeight3-10;
DoodlePlatformHeight4=DoodlePlatformHeight4-10;
}
if (leftjumper==true&&test==false){
DoodleW=(DoodleW-15);
}
if (rightjumper==true&&test==false){
DoodleW=(DoodleW+15);
}
if (DoodlePlatformHeight>height){
DoodlePlatformWidth=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight=0;
}
if (DoodlePlatformHeight1>height){
DoodlePlatformWidth1=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight1=0;
}
if (DoodlePlatformHeight2>height){
DoodlePlatformWidth2=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight2=0;
}
if (DoodlePlatformHeight3>height){
DoodlePlatformWidth3=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight3=0;
}
if (DoodlePlatformHeight4>height){
DoodlePlatformWidth4=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight4=0;
}
if (DoodleH==DoodlePlatformHeight-50 && DoodleW>=DoodlePlatformWidth-30 && DoodleW<=DoodlePlatformWidth+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight1-50 && DoodleW>=DoodlePlatformWidth1-30 && DoodleW<=DoodlePlatformWidth1+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight2-50 && DoodleW>=DoodlePlatformWidth2-30 && DoodleW<=DoodlePlatformWidth2+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight3-50 && DoodleW>=DoodlePlatformWidth3-30 && DoodleW<=DoodlePlatformWidth3+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight4-50 && DoodleW>=DoodlePlatformWidth4-30 && DoodleW<=DoodlePlatformWidth4+180){
test=true;
}
f.repaint();
}
}
static boolean flag=true;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){
flag = false;
jumper=true;
test=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=true;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=false;
}
}
}
好吧,您可以在 keyReleased
和 keyPressed
代码之间共享一个布尔变量。当按下键时,将值设置为 false,然后将其作为条件添加到您的 keyPressed
代码中,这样当标志为 false 时,它不会执行任何操作。与您的 keyReleased
代码类似,再次将标志设置为 true,以便 keyPressed
代码再次运行。
因此,通过这些修改,您的代码将变为:
static boolean flag = true;//<- your flag
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){//<- only jump if the flag is true
flag = false;//<-- set flag to false, this will be reset by the keyReleased code again
jumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;//<-- reset flag to be able to jump again
jumper=false;
}
}
希望对您有所帮助:)
编辑 #1
第 2 版仍然有效,但您可能希望在登陆平台时重置标志,否则用户可以向 space 栏发送垃圾邮件。
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){
flag = false;
jumper=true;
test=false;
}else{
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=true;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=false;
}
}
问题是您只有在松开按键时才会关闭角色!
特别是这个逻辑:
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=false;
}
}
然后在 actionPerformed 时使用此逻辑
if (jumper==false){
DoodleHeight=DoodleHeight-10;
}
你需要类似于重力的东西,在一段时间后将角色降到地面
一种方法是使用 TimerTask
,它在角色跳跃时立即开始,每个计时器刻度上的任务都会降低角色的高度,直到为 0 或与跳跃前相同.
大家好,最近我做了一个类似于涂鸦跳跃的游戏,jumper/object 跳到上面的下一个平台,如果你摔倒了,你就输了。所以无论如何我的游戏都可以运行,但有一个问题,我已经做到了,如果你按下 space 条,jumper/object 就会跳起来,好的,问题来了:如果你一直按住space bar 然后跳线会一直向上移动,直到我松开 space bar,你是怎么做到的,当你按住 space bar 时 jumper/object 只跳跃而不是继续上升直到我放开 space 条?
这是我的代码中发生的事情:
这是我执行的动作class跳线向上或向下跳动的地方
static boolean gameplay=false;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (gameplay==true){
if (jumper==true){
DoodleHeight+20;
}
if (jumper==false){
DoodleHeight=DoodleHeight-10;
}
这里有控制跳跃的按键
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=false;
}
}
你会如何解决这个问题?我需要帮助,但我想不通。
这是我的带有标志的代码:
import java.awt.event.*;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class DoodleJumpp extends JPanel implements ActionListener,KeyListener{
static JFrame f;
static int width=1200, height=930;
static int DoodleW=500, DoodleH=500;
static int DoodlePlatformWidth=200, DoodlePlatformHeight=400;
static int DoodlePlatformWidth1=400, DoodlePlatformHeight1=530;
static int DoodlePlatformWidth2=900, DoodlePlatformHeight2=874;
static int DoodlePlatformWidth3=345, DoodlePlatformHeight3=643;
static int DoodlePlatformWidth4=711, DoodlePlatformHeight4=957;
static boolean rightjumper,leftjumper;
static boolean jumper;
static boolean test=true;
static boolean gameplay=true;
public void paintComponent (Graphics g){
g.setColor(Color.green);
g.fillRect(DoodlePlatformWidth, DoodlePlatformHeight,200,30);
g.fillRect( DoodlePlatformWidth1, DoodlePlatformHeight1,200,30);
g.fillRect( DoodlePlatformWidth2, DoodlePlatformHeight2,200,30);
g.fillRect( DoodlePlatformWidth3, DoodlePlatformHeight3,200,30);
g.fillRect( DoodlePlatformWidth4, DoodlePlatformHeight4,200,30);
g.setColor(Color.blue);
g.fillRect(DoodleW, DoodleH,50,50);
}
public static void main(String a[]){
DoodleJumpp D = new DoodleJumpp();
f = new JFrame();
D.init();
f.add(D);
f.setSize(width,height);
f.setVisible(true);
f.repaint();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t=new Timer(10,D);
t.start();
}
public void init (){
this.addKeyListener(this);
setFocusable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (gameplay==true){
if (jumper==true&&test==false){
DoodlePlatformHeight=DoodlePlatformHeight+20;
DoodlePlatformHeight1=DoodlePlatformHeight1+20;
DoodlePlatformHeight2=DoodlePlatformHeight2+20;
DoodlePlatformHeight3=DoodlePlatformHeight3+20;
DoodlePlatformHeight4=DoodlePlatformHeight4+20;
}
if (jumper==false&&test==false){
DoodlePlatformHeight=DoodlePlatformHeight-10;
DoodlePlatformHeight1=DoodlePlatformHeight1-10;
DoodlePlatformHeight2=DoodlePlatformHeight2-10;
DoodlePlatformHeight3=DoodlePlatformHeight3-10;
DoodlePlatformHeight4=DoodlePlatformHeight4-10;
}
if (leftjumper==true&&test==false){
DoodleW=(DoodleW-15);
}
if (rightjumper==true&&test==false){
DoodleW=(DoodleW+15);
}
if (DoodlePlatformHeight>height){
DoodlePlatformWidth=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight=0;
}
if (DoodlePlatformHeight1>height){
DoodlePlatformWidth1=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight1=0;
}
if (DoodlePlatformHeight2>height){
DoodlePlatformWidth2=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight2=0;
}
if (DoodlePlatformHeight3>height){
DoodlePlatformWidth3=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight3=0;
}
if (DoodlePlatformHeight4>height){
DoodlePlatformWidth4=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight4=0;
}
if (DoodleH==DoodlePlatformHeight-50 && DoodleW>=DoodlePlatformWidth-30 && DoodleW<=DoodlePlatformWidth+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight1-50 && DoodleW>=DoodlePlatformWidth1-30 && DoodleW<=DoodlePlatformWidth1+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight2-50 && DoodleW>=DoodlePlatformWidth2-30 && DoodleW<=DoodlePlatformWidth2+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight3-50 && DoodleW>=DoodlePlatformWidth3-30 && DoodleW<=DoodlePlatformWidth3+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight4-50 && DoodleW>=DoodlePlatformWidth4-30 && DoodleW<=DoodlePlatformWidth4+180){
test=true;
}
f.repaint();
}
}
static boolean flag=true;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){
flag = false;
jumper=true;
test=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=true;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=false;
}
}
}
好吧,您可以在 keyReleased
和 keyPressed
代码之间共享一个布尔变量。当按下键时,将值设置为 false,然后将其作为条件添加到您的 keyPressed
代码中,这样当标志为 false 时,它不会执行任何操作。与您的 keyReleased
代码类似,再次将标志设置为 true,以便 keyPressed
代码再次运行。
因此,通过这些修改,您的代码将变为:
static boolean flag = true;//<- your flag
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){//<- only jump if the flag is true
flag = false;//<-- set flag to false, this will be reset by the keyReleased code again
jumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;//<-- reset flag to be able to jump again
jumper=false;
}
}
希望对您有所帮助:)
编辑 #1
第 2 版仍然有效,但您可能希望在登陆平台时重置标志,否则用户可以向 space 栏发送垃圾邮件。
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){
flag = false;
jumper=true;
test=false;
}else{
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=true;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=false;
}
}
问题是您只有在松开按键时才会关闭角色!
特别是这个逻辑:
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=false;
}
}
然后在 actionPerformed 时使用此逻辑
if (jumper==false){
DoodleHeight=DoodleHeight-10;
}
你需要类似于重力的东西,在一段时间后将角色降到地面
一种方法是使用 TimerTask
,它在角色跳跃时立即开始,每个计时器刻度上的任务都会降低角色的高度,直到为 0 或与跳跃前相同.