0

Having a massive problem with this piece of code. I'm working with Java in processing.

I've created a game where users must guide a character away from objects.

All the objects, health system and score system are based on mills().

Once the game ends we need to reset millis(), resetting the objects, score and health system.

I have searched implemented advice from friends and previously asked questions on here but the advice differs very slightly. I'm assuming it's something that I can't see.

I would really appreciate your help with this, I only ever use this site as a last resort not just when I'm feeling lazy.

//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()
   {


   }
  • Humm... what is the problem you have? It's not clear, and the code is not easy to run as there is a lot of images. millis() can't really be reset, as it counts time since the program starts. You can reset timer, but this is being done every frame... I really don't understand what is your problem. – v.k. May 21 '13 at 16:51
  • Basically, I need the millis to start from zero on a mouse click. This is the example of what I'm aiming to apply.. int timeStamp = 5000; int timer; void setup() { size(500, 500); timer = millis(); } void draw() { int timer2 = millis() - timer; if(timer2 > timeStamp) { println("oh lalala"); timer = millis(); } } – user2236032 May 21 '13 at 17:09
  • Very well, but this, oh lala :), works perfectly, so again what's wrong? :) I'm sure this is to simple to be what youy are looking for: void mousePressed(){ timer = millis(); } – v.k. May 21 '13 at 17:50
  • MAybe you can make an example with uploaded images and commun fonts so we can run it. loadImage("http:/someurl.png"); – v.k. May 21 '13 at 17:51

2 Answers2

2

Your question is not at all clear, but it sounds like you should be able to wrap System.currentTimeMillis() in an object that maintains a starting point and returns the offset. Something like

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

You create an instance of this class at startup

Millis timer = new Millis();

then call reset() to set it back to zero at the beginning of each game. Everywhere in your code you currently have millis() you would have timer.getMillis()

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
1

Use a custom timer class

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

Then to call

 firstTimer = new Timer(50000);

And then in the draw to check

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