Java游戏动作执行的问题

时间:2015-06-13 22:40:40

标签: java jframe jpanel

大家好,所以最近我做了一个类似于涂鸦跳跃的游戏,跳投/对象跳到下一个平台上,如果你摔倒,你输了。所以无论如何我的游戏都有效,但有一个问题,我已经说过,如果你按空格键跳线/对象跳起来,那么问题就在于:如果你继续按住空格键,那么跳线将会继续向上移动,直到我放开空格键,你怎么做到当你按下或按住空格键时,跳线/物体只跳过那些然后继续向上直到我放开空格键?

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

这是我的动作表演课程,其中跳线向上或向下跳跃

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

2 个答案:

答案 0 :(得分:1)

您可以在keyReleasedkeyPressed代码之间共享一个布尔变量。按下键时,将值设置为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这适用于你可能希望在登陆平台时重置标志,否则用户只能向空格键发送垃圾邮件。

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

答案 1 :(得分:1)

问题是你只有在释放钥匙时才会打倒角色!

特别是这个逻辑:

@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或与跳跃之前相同。 / p>