2017-01-23 56 views
-2

我正在做一個有多個結果的自選項目。我想要做的事情之一就是加入快速時間事件(QTEs),或者您必須在短時間內點擊按鈕或者遇難的遊戲中的事件。在這種情況下,我試圖做到這一點,以便在時間到了的時候,遊戲會說:「突然間你聽到了從你右邊傳來的蹄聲!」,此時調用了一個方法使GUI彈出用一個按鈕,你只需要幾秒鐘就可以點擊按鈕。如果您在適當的時間內點擊該按鈕,它應該繼續遊戲(以'CRACK !!'開頭)。如果沒有,遊戲應該結束了,並且會打印一條消息('GAME OVER - 你被Minotaur監獄看守殺死了,但是當我運行它時,GUI不會彈出,Game Over消息會打印三次在sleepLines延遲,然後將「裂紋!你快轉...」消息出現在這裏是我的代碼:在Java中使用GUI的快速事件

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(""); 

    } 

} 

    } 
+1

很抱歉的直率,但這種貌似有人在太急於創造閱讀教程意識碼隨機流。接受我的建議,放棄這些代碼,全部選擇你想使用的任何GUI庫,無論是Swing還是更新的JavaFX,然後在嘗試與庫創建複雜程序之前先學習這些教程。如果你遵循這個規定,你將會拯救你自己(和我們)很多的悲傷。 –

回答

2

讓我們開始的事實,Swing是單線程的,這意味着任何行動塊事件指派線程(如Thread.sleep)會阻止它處理任何新的事件(包括油漆事件),直到它放開。

兩個黃金法則...

  1. 不要堵塞EDT
  2. 不更新從EDT

外的UI在你的情況,你可以通過做一個Swing的使用Timer

查看達到您想要的結果How to use Swing Timers瞭解更多詳情。

在我的腦海裏,我會考慮製作某種類型的QTE類,這需要大量的時間,用戶需要點擊的按鈕的引用,以及可能的某種監聽器/觀察者/回調。

然後,QTE將附加一個ActionListener按鈕,如果觸發,將停止Timer並通知觀察員成功。否則,如果Timer首先觸發,它會通知觀察者失敗。

作爲一種理念

+0

我一定錯過了我們不想幫助人們的會議:( – MadProgrammer

+0

這不像是一個會議,更像是一個證券交易所的地板。 – trashgod