2013-05-21 60 views
0

這段代碼存在大量問題。我正在處理Java。重新啓動millis處理錯誤

我創建了一個遊戲,用戶必須引導角色遠離對象。

所有對象,健康系統和評分系統都基於工廠()。

一旦遊戲結束,我們需要重置millis(),重置對象,分數​​和健康系統。

我已經搜索了來自朋友的實施建議以及之前在這裏提出的問題,但建議略有不同。我假設這是我看不到的東西。

我真的很感謝你的幫助,我只是把這個網站當作最後的手段,而不僅僅是當我感到懶惰時。

//these are used to set the times which the games increases difficulty 
//int timeDelay = 30000; 

int delayOne = 2000; 
int delayTwo = 5000; 
int delayThree = 80000; 
int delayFour = 90000; 
int display = 2000; 

//for collisions 
float[] xpos = new float[6]; 
float[] ypos = new float[6]; 


//timer counts how many millis() each game lasts for 
int timeStamp = 5000; 
int timer; 
int timer2 = millis() - timer; 
//always at zero 


//outputting score at the end of a game 
int score; 
int start; 



//trying to get lives working 
boolean lost = false; 

//variable to store & output gale force when giving score 
int Gale = 0; 

//Changing length rectangle 
float rX = 350.0; 

float x1 = 20; 
float y1 = 20; 
float w1 = 100; 
float h1 = 30; 

//DECLARE OBJECTS JELLY CLASS 
Jelly myObject; 
Jelly myObject1; 
Jelly myObject2; 
Jelly myObject3; 

//GENTLEMAN CLASS 
gentleMan mygentleMan; 

//LOLLY CLASS 
Lolly myCow; 
Lolly myCow1; 

//PImages 
PImage loader; 
PImage bg; 
PImage uh; 
PImage bg1; 
PImage bolt; 
PImage over; 


void setup() 
{ 
      bg=loadImage("backy1.png"); 
      bg1 = loadImage("backy.png"); 
      over = loadImage("over.png"); 


      PFont L = loadFont("Lobster1.3-48.vlw"); 
      textFont(L, 16); 

      size(400, 600); 
      smooth(); 

      //begin = millis(); 

      imageMode(CENTER); 

      //INITIALISE 
      myObject = new Jelly(320, 500); 
      myObject1 = new Jelly(150, 200); 
      // myObject2 = new Jelly(550, 500); 
      //myObject3 = new Jelly(300, 100); 

      mygentleMan = new gentleMan(200, 300); 
      //myObject.run(); 
      //myObject1.run(); 
      //myObject2.run(); 

      myCow = new Lolly(400, 250); 
      myCow1 = new Lolly(150, 350); 

      timer = millis(); 
} 

void draw() 
{ 


    start = 0; 

    //because we have image mode set to center for collisions 
    //we have to divide the height & width of the screen by 2 for hte image to fit 
    image(bg, 200, 300); 

    if (millis() >= start + delayOne) 
    { 

     image(bg1, 200, 300); 
    } 
     //CALL FUNCTIONALITY 
     myObject.run(); 
     myObject.put_in_array(0); 
     myObject1.run(); // this one is going top to bottom 
     myObject1.put_in_array(1); 
    // myObject2.run(); 
     //myObject2.put_in_array(2); 
    // myObject3.run(); 
     //myObject3.put_in_array(3); 

     myCow.run(); 
     myCow.put_in_array(4); 
     myCow1.run(); 
     myCow1.put_in_array(5); 


     mygentleMan.run(); 

     //health bar 
     fill(161, 221, 16); 
     noStroke(); 
     rect(10, 24, rX, 10); 


     if(rX <= 100) 
     { 
     fill(221, 59, 16); 
     rect(10, 24, rX, 10); 
     } 

     else 

     if(rX <= 200) 
     { 
     fill(221, 137, 16); 
     rect(10, 24, rX, 10); 
     } 


     if(rX == 5.0) 
     { 
     lost = true; 
     noLoop(); 
     // lives = lives - 1; 
     image(over, width/2, height/3); 
     fill(255); 
     text("Your Score Is: " + timer, width/2.7, height/2); 
     text("Gale Force Is; " + Gale, width/2.7, height/1.8); 
     score = timer; 
     } 

    //For Loop detecting collisions between mygentleMan & objects 
     for (int i=0; i < 6; i++) { 

     if (xpos[i] > 150 && xpos[i] < 250 && ypos[i] > (mygentleMan.y-58) && ypos[i] < (mygentleMan.y+58)) 
     { 
     // text("collision", 200, 300); 
      bolt = loadImage("bolt.png"); 
      image(bolt, xpos[i], ypos[i]); 

      rX = rX - 1; 

     } 

     //outputting score on screen @ at all times 
     fill(255); 
     text("Score: " + timer, 320, 20); 
     } 


    //timer which will be score counter essentially 
     timer = millis(); 
     //text(timer, 20, 20); 

    //moving the man up the screen if button is pressed, if not he levitates downward 

     if (keyPressed) 
     { 
     mygentleMan.y -= mygentleMan.moveY; 

     mygentleMan.moveY += 0.4; 
     } 
     else 
     { 
     mygentleMan.y += mygentleMan.moveY; 

     mygentleMan.moveY += 0.2; 
     } 

     fill(255); 
     text("Health", 20, 20); 

    if(mousePressed) 
    { 
    if(timer2 > timeStamp) 
    { 
    println("tit"); 
    mygentleMan.y = height/2; 
    loop(); 
    } 
    } 

    } 

//class for first objects that move into the screen 

class Jelly 
{ 
        //GLOBAL VARIABLES 
        float x = 0; 
        float y = 0; 
        float speedX = 1.8; 
        float speedY = 1.8; 
        float speedX2 = 2.1; 
        float speedY2 = 2.1; 
        float speedX3 = 2.2; 
        float speedY3 = 2.2; 
        PImage jelly = loadImage("jelly.png"); 
        PImage hat = loadImage("hat.png"); 
        PImage gale = loadImage("g1.png"); 
        PImage force = loadImage("force.png"); 
        PImage news = loadImage("news.png"); 



        //CONSTRUCTOR 
        Jelly(float _x, float _y) 
        { 
         x = _x; 
         y = _y; 
        } 


        //FUNCTIONS 
        void run() 
        { 
         display(); 
         move(); 
         bounce(); 
         image(force, 330, 550); 



         if (millis() >= start + delayOne) 
         { 
         display(); 
         moveFast(); 
         bounceFast(); 
         image(gale, 280, 560); 
         Gale = 1; 

         if (start + delayOne + display >= millis()) 
         { 
         image(news, 200, 300); 
         } 
         } 

         if (millis() >= start +delayTwo) 
         { 
         display(); 
         moveFaster(); 
         bounceFaster(); 
         image(gale, 310, 560); 
         Gale = 2; 

         if (start + delayTwo + display >= millis()) 
         { 
         image(news, 200, 300); 
         } 


         } 
         } 

         void bounce() 
         { 
         if (x > width) 
         { 
          speedX = speedX * -1; //multiply by -1 to make it bounce 
         } 

         if (x < 0) 
         { 
          speedX = speedX * -1; 
         } 

         if (y > height) 
         { 
          speedY = speedY * -1; 
         } 

         if (y < 0) 
         { 
          speedY = speedY * -1; 
         } 
         } 

         void bounceFast() 
         { 
         if (x > width) 
         { 
          speedX2 = speedX2 * -1; //multiply by -1 to make it bounce 
         } 

         if (x < 0) 
         { 
          speedX2 = speedX2 * -1; 
         } 

         if (y > height) 
         { 
          speedY2 = speedY2 * -1; 
         } 

         if (y < 0) 
         { 
          speedY2 = speedY2 * -1; 
         } 
         } 

         void bounceFaster() 
         { 
         if (x > width) 
         { 
          speedX3 = speedX3 * -1; //multiply by -1 to make it bounce 
         } 

         if (x < 0) 
         { 
          speedX3 = speedX3 * -1; 
         } 

         if (y > height) 
         { 
          speedY3 = speedY3 * -1; 
         } 

         if (y < 0) 
         { 
          speedY3 = speedY3 * -1; 
         } 
         } 



         void move() 
         { 
         x = x + speedX; 
         y = y + speedY; 
         } 

         void moveFast() 
         { 
         x = x + speedX2; 
         y = y + speedY2; 
         } 

         void moveFaster() 
         { 
         x = x + speedX3; 
         y = y + speedY3; 
         } 

         void put_in_array(int a) 
         { 

         xpos[a] = x; 
         ypos[a] = y; 
         } 



         void display() 
         { 
         image(hat, x, y); 
         } 
} 


//class for gentleman that floats 

class gentleMan 
{ 
    //GLOBAL VARIABLES 
    float y = 400; 
    float x = 400; 
    float moveY; 
    //PImage umbrella; 
    PImage umbrella = loadImage("dafuq.png"); 
    PImage over = loadImage("over.png"); 



    //CONSTRCUTOR --- PIECES OF INFO PROVDE TO BUILD CLASS -- INTIIALIZE VARIBALE 
    gentleMan(float _x, float _y) 
    { 
    y = _y; 
    x = _x; 
    moveY = 2; 
    } 

    //FUNCTIONS 
    void run() 
    { 
    display(); 
    keyReleased(); 
    bounce(); 
    // collision(); 
    } 


    void display() 
    { 

    image(umbrella, x, y); 
    } 


    void keyReleased() 
    { 

    mygentleMan.moveY = 4; 
    } 

    void bounce() 
    { 
    if (y < 0) 
    { 
     y = 0; 
    } 

    if (y > height) 
    { 
    //score = millis(); 
    lost = true; 
    noLoop(); 
    // lives = lives - 1; 
    image(over, width/2, height/3); 
    text("Your Score Is: " + timer, width/2.7, height/2); 
    text("Gale Force Is; " + Gale, width/2.7, height/1.8); 

    } 

    } 
} 

class Lolly 
{ 
    //GLOBAL VARIABLES 
    float x = 0; 
    float y = 0; 
    float speedX = 2; 
    float speedY = 2; 
    float speedX1 = 2.1; 
    float speedY1 = 2.1; 
    float speedX2 = 2.3; 
    float speedY2 = 2.3; 
    PImage cow = loadImage("cow.png"); 



    //CONSTRUCTOR 
    Lolly(float _x, float _y) 
    { 
    x = _x; 
    y = _y; 
    } 


    //FUNCTIONS 
    void run() 
    { 


    // display(); 
    //move(); 
    //bounce(); 

    if (millis() >= start + delayThree) 
    { 
     display(); 
     moveFast(); 
     bounceFast(); 
    } 

    if (millis() >= start +delayFour) 
    { 
     display(); 
     moveFaster(); 
     bounceFaster(); 
    } 
    } 

    void put_in_array(int a) 
    { 

    xpos[a] = x; 
    ypos[a] = y; 
    } 




    void bounce() 
    { 
    if (x > width) 
    { 
     speedX = speedX * -1; //multiply by -1 to make it bounce 
    } 

    if (x < 0) 
    { 
     speedX = speedX * -1; 
    } 

    if (y > height) 
    { 
     speedY = speedY * -1; 
    } 

    if (y < 0) 
    { 
     speedY = speedY * -1; 
    } 
    } 

    void bounceFast() 
    { 
    if (x > width) 
    { 
     speedX1 = speedX1 * -1; //multiply by -1 to make it bounce 
    } 

    if (x < 0) 
    { 
     speedX1 = speedX1 * -1; 
    } 

    if (y > height) 
    { 
     speedY1 = speedY1 * -1; 
    } 

    if (y < 0) 
    { 
     speedY1 = speedY1 * -1; 
    } 
    } 

    void bounceFaster() 
    { 
    if (x > width) 
    { 
     speedX2 = speedX2 * -1; //multiply by -1 to make it bounce 
    } 

    if (x < 0) 
    { 
     speedX2 = speedX2 * -1; 
    } 

    if (y > height) 
    { 
     speedY2 = speedY2 * -1; 
    } 

    if (y < 0) 
    { 
     speedY2 = speedY2 * -1; 
    } 
    } 



    void move() 
    { 
    x = x + speedX; 
    y = y + speedY; 
    } 

    void moveFast() 
    { 
    x = x + speedX1; 
    y = y + speedY1; 
    } 

    void moveFaster() 
    { 
    x = x + speedX2; 
    y = y + speedY2; 
    } 


    void display() 
    { 
    image(cow, x, y); 
    } 
}//end of cow class 


    void mousePressed() 
    { 


    } 
+0

Humm ...你有什麼問題?目前還不清楚,代碼很難運行,因爲有很多圖像。 millis()不能真正被重置,因爲它計數自程序開始以來的時間。你可以重置計時器,但這是每幀完成的......我真的不明白你的問題。 –

+0

基本上,我需要millis從零開始點擊鼠標。這是我打算申請的例子。int timeStamp = 5000; int timer; void setup() size(500,500); timer = millis(); () } void draw() int timer2 = millis() - timer;如果(timer2> timeStamp) {0} timer = millis(); } } – user2236032

+0

非常好,但是,哦,拉拉:),完美的作品,所以又是怎麼回事? :)我敢肯定,這是簡單的是你在找什麼:void mousePressed(){ timer = millis(); } –

回答

2

你的問題並不清楚,但它聽起來像是你應該能夠在維持一個出發點和返回偏移一個對象來包裝System.currentTimeMillis()。像

public class Millis 
{ 
    long start; 
    public Millis() { this.reset(); } 
    public void reset() { this.start = System.currentTimeMillis(); } 
    public long getMillis() { return System.currentTimeMillis() - start; } 
} 

您在啓動

Millis timer = new Millis(); 

創建這個類的一個實例東西然後調用reset()在每場比賽開始時將其設置回零。到處都在你的代碼,你現在有millis()你會timer.getMillis()

1

使用自定義定時器類

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




}//end class 

然後調用

firstTimer = new Timer(50000); 

然後在抽籤檢查

if (firstTimer.isFinished()) { 
    //do sopemthing 
    //then restart timer 
    firstTimer.start(); 
    } 
    else 
    { 
    // do soemthing else for the rest of the time.... 
    }