Java游戏动作执行问题

Java游戏动作执行问题,java,jframe,jpanel,Java,Jframe,Jpanel,大家好,最近我做了一个类似于涂鸦跳跃的游戏,跳投者/物体跳到顶部的下一个平台,如果你摔倒了,你就输了。不管怎样,我的游戏都能正常运行,但有一个问题,我已经确定,如果你按下空格键,跳线/物体会跳起来,好吧,问题是:如果你一直按住空格键,那么跳线会一直向上移动,直到我放开空格键,当你按下或按住空格键时,如何使跳线/对象只跳一个,而不是一直向上,直到我放开空格键 下面是我的代码中发生的事情: 这是我的动作表演课,跳高者跳上跳下 static boolean gameplay=false; public

大家好,最近我做了一个类似于涂鸦跳跃的游戏,跳投者/物体跳到顶部的下一个平台,如果你摔倒了,你就输了。不管怎样,我的游戏都能正常运行,但有一个问题,我已经确定,如果你按下空格键,跳线/物体会跳起来,好吧,问题是:如果你一直按住空格键,那么跳线会一直向上移动,直到我放开空格键,当你按下或按住空格键时,如何使跳线/对象只跳一个,而不是一直向上,直到我放开空格键

下面是我的代码中发生的事情:

这是我的动作表演课,跳高者跳上跳下

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;
    }
}
}
导入java.awt.event.*;
导入java.awt.*;
导入java.util.Scanner;
导入javax.swing.*;
公共类DoodleJumpp扩展JPanel实现ActionListener、KeyListener{
静态jf帧;
静态整数宽度=1200,高度=930;
静态int DoodleW=500,DoodleH=500;
静态int DoodlePlatformWidth=200,DoodlePlatformHeight=400;
静态int DoodlePlatformWidth1=400,DoodlePlatformHeight1=530;
静态int涂鸦平台宽度2=900,涂鸦平台高度2=874;
静态int DoodlePlatformWidth3=345,DoodlePlatformHeight3=643;
静态int DoodlePlatformWidth4=711,DoodlePlatformHeight4=957;
静态布尔右跳线,左跳线;
静态布尔跳线;
静态布尔测试=真;
静态布尔游戏性=真;
公共组件(图形g){
g、 setColor(Color.green);
g、 fillRect(涂鸦平台宽度,涂鸦平台高度,200,30);
g、 fillRect(涂鸦平台宽度1,涂鸦平台高度1200,30);
g、 fillRect(涂鸦平台宽度2,涂鸦平台高度2200,30);
g、 fillRect(涂鸦平台宽度3,涂鸦平台高度3200,30);
g、 fillRect(涂鸦平台宽度4,涂鸦平台高度4200,30);
g、 setColor(Color.blue);
g、 fillRect(DoodleW,DoodleH,50,50);
}
公共静态void main(字符串a[]{
DoodleJumpp D=新的DoodleJumpp();
f=新的JFrame();
D.init();
f、 加(D);
f、 设置尺寸(宽度、高度);
f、 setVisible(真);
f、 重新油漆();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
定时器t=新定时器(10,D);
t、 start();
}
公共void init(){
this.addKeyListener(this);
设置聚焦(真);
}
@凌驾
已执行的公共无效操作(操作事件e){
//TODO自动生成的方法存根
如果(游戏性==true){
如果(跳线==true&&test==false){
涂鸦平台高度=涂鸦平台高度+20;
涂鸦平台高度1=涂鸦平台高度1+20;
涂鸦平台高度2=涂鸦平台高度2+20;
涂鸦平台高度3=涂鸦平台高度3+20;
涂鸦平台高度4=涂鸦平台高度4+20;
}
如果(跳线==false&&test==false){
涂鸦平台高度=涂鸦平台高度-10;
涂鸦平台高度1=涂鸦平台高度1-10;
涂鸦平台高度2=涂鸦平台高度2-10;
涂鸦平台高度3=涂鸦平台高度3-10;
涂鸦平台高度4=涂鸦平台高度4-10;
}
if(leftjumper==true&&test==false){
DoodleW=(DoodleW-15);
}
if(rightjumper==true&&test==false){
杜德洛=(杜德洛+15);
}
if(涂鸦平台高度>高度){
DoodlePlatformWidth=(int)Math.floor(Math.random()*1201);
涂鸦平台高度=0;
}
if(涂鸦平台高度1>高度){
DoodlePlatformWidth1=(int)Math.floor(Math.random()*1201);
涂鸦平台高度1=0;
}
if(涂鸦平台高度2>高度){
DoodlePlatformWidth2=(int)Math.floor(Math.random()*1201);
涂鸦平台高度2=0;
}
if(涂鸦平台高度3>高度){
DoodlePlatformWidth3=(int)Math.floor(Math.random()*1201);
涂鸦平台高度3=0;
}
if(涂鸦平台高度4>高度){
DoodlePlatformWidth4=(int)Math.floor(Math.random()*1201);
涂鸦平台高度4=0;
}

如果(DoodleH==DoodlePlatformHeight-50&&DoodleW>=DoodlePlatformWidth-30&&DoodleW=DoodlePlatformWidth1-30&&DoodleW=DoodlePlatformWidth2-30&&DoodleW=DoodlePlatformWidth4-30&&DoodleW您可以在
keyreased
按键之间共享一个布尔变量按下y键时,将值设置为false,然后将其作为条件添加到按下的
键中,这样当标志为false时,它将不会执行任何操作。与此类似,在按下
键时,再次将标志设置为true,以便按下的
键再次工作

因此,通过这些修改,您的代码将变为:

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;
    }
} 

问题是,只有在释放钥匙时,您的角色才会下降

具体来说,按照这一逻辑:

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyCode()==KeyEvent.VK_SPACE){
        jumper=false;
    }
} 
然后在执行操作时使用此逻辑

    if (jumper==false){
        DoodleHeight=DoodleHeight-10;
    }
你需要一些类似于重力的东西,经过一段时间的推移,将角色带到地面


一种方法是使用
TimerTask
,该任务在角色跳转时立即启动,并且每个计时器刻度上的任务都会降低角色的高度,直到该高度为0或与跳转前的高度相同。

一个键被多次按下或一直按下(两种情况相同).因此,在您的情况下,程序会得到相同的输入,并且它会
DoodleHeight+20;
,跳线会一直向上移动,直到您松开空格键**好的,但是我如何更改代码,使其只跳转一个,而不是一直向上跳转?好的,谢谢,无论如何,我会尝试考虑它myself@jackzhang因此,如果你的水平有30个平台,你会e将有30个变量和条件?您需要保持平台的当前高度
    if (jumper==false){
        DoodleHeight=DoodleHeight-10;
    }