2013-05-07 30 views
0

進出口正在開發一個處理和Fiducials遊戲。基本上這個遊戲包含提問有關某些國家的問題。每個問題的每個ANS都是一個國家。例如,比薩塔和意大利ASN。用戶必須顯示代表意大利國旗的Fiducial添加計分器與分數

我得到了倒數計時器運行,但我不能讓它重新啓動一個新的水平時。我們有一個開關的每個級別。

非常感謝您的幫助!

這裏是代碼:

 import ddf.minim.*; 
    import TUIO.*; 
    TuioProcessing tuioClient; 

    HashMap<Integer, String> symbolWordMap=new HashMap<Integer, String>(); 

    //The below variables are used to show the background image 
    PImage bg; 
    PImage start; 
    PImage q1; 
    PImage q2; 
    PImage correct; 
    PImage wrong; 

    //The following are the variables used fro playing the sounds 
    AudioPlayer player; 
    AudioPlayer playerfail; 
    Minim minim; 

    int level = 0; 

    int objSize=50; 

    //This variable will be reduced everytime the player gets the answer wrong 
    int lives = 5; 

    //This variable will keep track of the score of the player 
    int score = 0; 

    //Declaring the font variable so as to display text on the screen 
    PFont font; 

    //Declaring the variable to be used for the timer 
    //Timer t; 


    //Below is the timer that will be used 
    void time(){ 
     int c; 
     int csec; 
     int climit = 20; //defined for a 20 second countdown 

     c = climit*1000 - millis(); 
     csec = (c/(1000)); 

     if (csec > 0){ 
     text("TIME: "+csec+" secs",1800,50); 
     } else { 
     background(bg); 
     text("TIME: 0 secs",1800,50); 
     text("Time is up!",950,488); 
     } 
    } 



    void setup() 
    { 
     symbolWordMap.put(0, "Start"); 
     symbolWordMap.put(1, "Italy"); 
     symbolWordMap.put(2, "France"); 
     symbolWordMap.put(3, "Spain"); 
     symbolWordMap.put(4, "UK"); 
     symbolWordMap.put(5, "USA"); 
     symbolWordMap.put(6, "Malta"); 
     symbolWordMap.put(7, "Australia"); 
     symbolWordMap.put(8, "Germany"); 

     size(1920,976); 

     //This is an instance of Minim 
     minim = new Minim(this); 

     //Load the applause mp3 file 
     player = minim.loadFile("applause.mp3",2048); 
     //Load the fail mp3 file 
     playerfail = minim.loadFile("fail.mp3",2048); 

     //Creating an instance of the Timer class 
     //t = new Timer(20000); 

     //savedTime = millis(); 

     //The below statement will load the image to be placed as a background from the Data folder. This image must be the same size as the window in which the program will run 
     bg = loadImage("map.png"); 
     //Load the image for correct answer 
     correct = loadImage("correct.png"); 
     //Load image for the incorrect answer 
     wrong = loadImage("wrong.jpg"); 
     //Load the image for Question 1 
     q1 = loadImage("Question1.png"); 
     //Load the image for Question 2 
     q2 = loadImage("question2.png"); 
     //Load the image for the welcome page 
     start = loadImage("Intro.png"); 

     //Creating the font of text to be displayed on screen; 
     font = createFont("Arial",24,true); 

     rectMode(CENTER); 
     textAlign(CENTER, CENTER); 

     // an instance of the TuioProcessing 
     // since we ad "this" class as an argument the TuioClient expects 
     // an implementation of the TUIO callback methods 
     tuioClient = new TuioProcessing(this); 
    } 

    void draw() 
    { 

     //The following command will present the background 
     background(bg); 

     //Specifying the font to be used for text that is displayed on screen 
     textFont(font,24); 
     //Specifying the colour of the text that will be displayed on screen 
     fill(0); 

     if (level>0){ 
     text("SCORE: "+score,100,50); 
     //text("Lives: "+lives,100,100); 
     } 

     //Creating the different levels 
     switch(level){ 
     case 0: 
     //Welcome Screen 

     // get all the tuio objects detected by reactivision 
     Vector<TuioObject> tuioObjectList =tuioClient.getTuioObjects(); 

     image(start,550,160); 
     // Process each fiducial in turn 
     for (int i=0;i<tuioObjectList.size();i++) { 
      TuioObject tobj = tuioObjectList.get(i); 

      pushMatrix(); 
      // Move origin to location of TUI object and rotate 
      translate(tobj.getScreenX(width), tobj.getScreenY(height)); 
      rotate(tobj.getAngle()); 

      // draw the box 
      fill(255); 
      rect(0, 0, objSize, objSize); 

      // Write the text inside the box 
      fill(0); 

      int id = tobj.getSymbolID(); 
      String txt; 
      if (symbolWordMap.containsKey(id)) { 
      // if ID is in symbolWordMap, then look it up to find word 
      txt = symbolWordMap.get(id); 
      } 
      else { // otherwise, we'll just display the id number with a dot after 
      txt = id+"."; 
      } 
      text(txt, 0, 0); 
      popMatrix(); 

      if (id == 0){ 
      level = 1; 
      delay(2000); 
      } 
     } 

    break; 

    case 1: 
    //Level 1 

     //Start timer 
     //time(); 
     //noLoop(); 

     // get all the tuio objects detected by reactivision 
     Vector<TuioObject> tuioObjectList1 =tuioClient.getTuioObjects(); 

     image(q1,550,160); 
     // Process each fiducial in turn 
     for (int i=0;i<tuioObjectList1.size();i++) { 
      TuioObject tobj = tuioObjectList1.get(i); 

      pushMatrix(); 
      // Move origin to location of TUI object and rotate 
      translate(tobj.getScreenX(width), tobj.getScreenY(height)); 
      rotate(tobj.getAngle()); 

      // draw the box 
      fill(255); 
      rect(0, 0, objSize, objSize); 

      // Write the text inside the box 
      fill(0); 

      int id = tobj.getSymbolID(); 
      String txt; 
      if (symbolWordMap.containsKey(id)) { 
      // if ID is in symbolWordMap, then look it up to find word 
      txt = symbolWordMap.get(id); 
      } 
      else { // otherwise, we'll just display the id number with a dot after 
      txt = id+"."; 
      } 
      text(txt, 0, 0); 
      popMatrix(); 

      if (id == 1){ 
      //Displays the text Correct on screen 
      //text("Correct",500,500); 
      score=score+10; 
      background(bg); 
      image(correct,800,300); 
      player.play(); 
      player.rewind(); 
      //Time delay before advancing to next level 
      delay(5000);    
      //Advance to level 2 
      level++; 
      } else { 
      //Display the text Wrong to the left of the screen 
      //text("Wrong",500,500); 
      lives=lives-1; 
      background(bg); 
      image(wrong,700,300); 
      playerfail.play(); 
      playerfail.rewind(); 
      } 
     } 

    break; 

    case 2: 
    //Level 2 

     //Start timer 
     //time(); 
     //noLoop(); 


     // get all the tuio objects detected by reactivision 
     Vector<TuioObject> tuioObjectList2 =tuioClient.getTuioObjects(); 

     //Load image for question 2 
     image(q2,550,160); 
     // Process each fiducial in turn 
     for (int i=0;i<tuioObjectList2.size();i++) { 
      TuioObject tobj = tuioObjectList2.get(i); 

      pushMatrix(); 
      // Move origin to location of TUI object and rotate 
      translate(tobj.getScreenX(width), tobj.getScreenY(height)); 
      rotate(tobj.getAngle()); 

      // draw the box 
      fill(255); 
      rect(0, 0, objSize, objSize); 

      // Write the text inside the box 
      fill(0); 

      int id = tobj.getSymbolID(); 
      String txt; 
      if (symbolWordMap.containsKey(id)) { 
      // if ID is in symbolWordMap, then look it up to find word 
      txt = symbolWordMap.get(id); 
      } 
      else { // otherwise, we'll just display the id number with a dot after 
      txt = id+"."; 
      } 
      text(txt, 0, 0); 
      popMatrix(); 

      if (id == 8){ 
      //Displays the text Correct on screen 
      //text("Correct",500,500); 
      score=score+10; 
      background(bg); 
      image(correct,900,500); 
      player.play(); 
      player.rewind(); 
      //Time delay before advancing to next level 
      delay(5000);    
      //Advance to level 2 
      level++; 
     } else { 
      //Display the text Wrong to the left of the screen 
      //text("Wrong",500,500); 
      lives=lives-1; 
      background(bg); 
      image(wrong,700,300); 
      playerfail.play(); 
      playerfail.rewind(); 
     } 
     } 

    break; 

    case 3: 
    //Level 3 
    break; 

    case 4: 
    //Level 4 
    break; 

    case 5: 
    //Level 5 
    break; 

    } 
    //End of switch 
} 


void stopSound() 
{ 
    player.close(); 
    playerfail.close(); 
    minim.stop(); 

    super.stop(); 
} 

回答

0

你的計時器的實施表明,它是一個繁忙的計時器。這意味着它依賴於多次調用並檢查它是否晚於其他時間。一個繁忙的計時器的幼稚實施將如下:

while(getTime() < someTime) { do nothing; } 

這種類型的計時器是不好的。你應該嘗試產生一個線程,並使用睡眠。這有點像JavaScript事件處理程序。操作系統在詢問時喚醒線程,然後您可以提醒程序計時器已啓動。如果你同時不需要做任何事情,你可以輕鬆地睡覺你的主體,但是在你跟蹤時間的時候,我覺得你在做某件事。

PS:它顯示你使用Java ...根據你的「進口」。它是否正確?如果是這樣,我可以爲你確定一些參考。

+0

我使用的代碼是處理,這是一種Java的味道。 – user1515230 2013-05-07 17:54:59

+0

http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html – ChrisCM 2013-05-07 17:56:41

+0

@ChrisCM我以爲我會澄清[你的問題](http://stackoverflow.com/questions/16423321 /細微存儲器泄漏和 - 是此結果共同實踐/ 16423475#comment23588408_16423475);僅供參考我使用「你的價值觀」。是什麼讓你以爲我用了別的東西對我來說是個謎。 (我告訴過你我用過的東西,以及如何。)祝你有美好的一天。 – sehe 2013-05-08 17:16:59

0

我不完全瞭解定時器在您的程序中發生的位置。我看到一個計時器類:

//Declaring the variable to be used for the timer 
//Timer t; 

但我沒有看到在你的代碼中使用的類。僅適用於時間的函數:

//Below is the timer that will be used 
void time(){ 
    int c; 
    int csec; 
    int climit = 20; //defined for a 20 second countdown 

    c = climit*1000 - millis(); 
    csec = (c/(1000)); 

    if (csec > 0){ 
    text("TIME: "+csec+" secs",1800,50); 
    } else { 
    background(bg); 
    text("TIME: 0 secs",1800,50); 
    text("Time is up!",950,488); 
    } 
} 

我建議不要在處理草圖使用delay(),而是使用當你想在一定時間後發生的不同事件不同的定時器。

我發現Daniel Shiffman的例子非常簡單,對理解如何在Processing中實現定時器很有幫助。 Here是一個面向對象的計時器,在這種情況下對你更有幫助。以下是鏈接中的計時器類:

class Timer { 

    int savedTime; // When Timer started 
    int totalTime; // How long Timer should last 

    Timer(int tempTotalTime) { 
    totalTime = tempTotalTime; 
    } 

    // Starting the timer 
    void start() { 
    // When the timer starts it stores the current time in milliseconds. 
    savedTime = millis(); 
    } 

    // The function isFinished() returns true if 5,000 ms have passed. 
    // The work of the timer is farmed out to this method. 
    boolean isFinished() { 
    // Check how much time has passed 
    int passedTime = millis()- savedTime; 
    if (passedTime > totalTime) { 
     return true; 
    } else { 
     return false; 
    } 
    } 
} 

用戶如何在級別之間切換?是否有按鍵或鼠標按鍵會觸發下一個關卡?還是完全基於計時器?如果它是基於一個按鍵,你可以結合下面的代碼:

int level; 
Timer levelTimer; 

void setup() { 
    levelTimer = new Timer(5000); 
} 

void draw() { 
    println(levelTimer.isFinished()); 
} 

void keyPressed() { 
    switch(key) { 
    case '1': 
    level = 1; 
    levelTimer.start(); 
    break; 
    case '2': 
    level = 2; 
    levelTimer.start(); 
    break; 
    case '3': 
    level = 3; 
    levelTimer.start(); 
    break; 
    } 
} 

這樣,你的每個用戶發展到他們選擇的級別時啓動級定時器。只是一個想法。

+0

我相信他創建的Timer是[java Timer](http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html) – 2013-05-09 00:07:04