繁体   English   中英

在 Java 中使用 GUI 的 Quick-Time 事件

[英]Quick-Time Event Using a GUI in Java

我正在制作一个具有多种结果的选择你自己的冒险项目。 我正在尝试做的一件事是合并快速时间事件 (QTE),或者在游戏中您必须在短时间内单击按钮或被杀死的那些事件。 在这种情况下,我试图让它在时机成熟时,游戏说“突然你听到从你右边传来的蹄声!”,此时会调用一个方法来弹出一个 GUI用一个按钮,您只有几秒钟的时间点击该按钮。 如果您在适当的时间内单击按钮,它应该继续游戏(以“CRACK!!”开头)。 如果没有,游戏应该结束,并打印一条消息('GAME OVER - You are slained by Minotaur Prison Guard。但是,当我运行它时,GUI 没有弹出,Game Over 消息打印了三遍sleepLines 延迟,然后出现 'CRACK!! You quick turn...' 消息。这是我的代码:

    import java.util.Scanner;

    import java.awt.Container;

    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;


    public class KCP1Main extends JPanel{

    public static JButton AttackButton;
    static Scanner myScanner = new Scanner(System.in);

    static int quicktimecompletion = 0;


    public static void main(String[] args)  throws InterruptedException {

        //insert leading up to this point here

        System.out.println("Suddenly, you hear the clomping of hooves to your right!");


        QuickTimeEvent(args);

        sleepLines(500,2);
        System.out.println("CRACK!!");
        sleepLines(1000,1);
        scrollText("You quickly turn right, swinging your metal pole at the same time... it  swings into the head of ");
        scrollText("a minotaur that was charging towards you, with enough force to knock him out.");


    }

}



public static void QuickTimeEvent(String[] args)  throws InterruptedException
{



    for(int timer=0 ; timer<3 ; timer++){

        ButtonHandler handler = new ButtonHandler();

        AttackButton = new JButton("<!");
        //static add(AttackButton);
        AttackButton.addActionListener(handler);

        sleepLines(1000,1);

        if(quicktimecompletion == 1)
        {
            break;
        }

        if(timer > 3);
        {

            System.out.println("GAME OVER");
            System.out.printf("You were slain by a Minotaur Prison Guard.");

        }

    }

}

public static class ButtonHandler implements ActionListener
{

    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource() == AttackButton)
        {
            quicktimecompletion = 1;

        }
    }
}



public static void scrollText(String message) throws InterruptedException{

    for(int i = 0; i < message.length(); i++)
    {
        System.out.print(message.charAt(i));
        Thread.sleep(62);
    }
    System.out.print("\n");

}

public static void JPanel(String[] args){

    JFrame theGUI = new JFrame();
    theGUI.setTitle("Incoming!");
    KCP1Main makeButtons = new KCP1Main();
    theGUI.add(makeButtons);
    theGUI.setSize(300, 200);
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theGUI.setBackground(Color.red);

    Container pane = theGUI.getContentPane();

    theGUI.setVisible(true);


}


public static void sleepLines(int duration, int lines) throws InterruptedException{

    for(int i=0; i < lines; i++){

        Thread.sleep(duration);
        System.out.println("");

    }

}

    }

让我们从 Swing 是单线程的事实开始,这意味着任何阻塞事件调度线程的操作(如Thread.sleep )都将阻止它处理任何新事件(包括绘制事件),直到它解除阻塞。

两条黄金法则...

  1. 不要阻止 EDT
  2. 不要从 EDT 外部更新 UI

在您的情况下,您可以通过使用 Swing Timer来达到您想要的结果

有关更多详细信息,请参阅如何使用摆动计时器

在我的脑海中,我可能会考虑制作某种 QTE 类,这需要花费大量时间,对用户需要单击的按钮的引用以及可能的某种侦听器/观察器/回调。

然后 QTE 将一个ActionListener附加到按钮,如果触发,将停止Timer并通知观察者成功。 否则,如果Timer首先触发,它会通知观察者失败。

作为一个想法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM