Custom Game project

Hello everyone once again! I’m having a problem trying to make a game, i have my concept and have gotten some help from my professor but im still coming up with errors.
The concept of the game is you control a bubble that you need to keep away from a pin floating around. There is more I want to add but for now, I would like help figuring out why my timer won’t work.

class timer {
  // props
  int startTime;
  int interval;

  //constructor
  Timer(int timeInterval) {
    interval = timeInterval;
  }
  //methods
  void start() {
    startTime = millis();
  }
  
  void complete(){
    
    int elapsedTime = millis() - startTime;
    if (elapsedTime > interval) 
    {
      return true;
    } 
    else {
      return false;
    }//end if
  } 
}//end class

I’ve also attached screenshots to everyone who helps can have a visual understanding of what’s going on



The name of the class and constructor must be exactly the same including case so use either

class timer {
  // props
  int startTime;
  int interval;

  //constructor
  timer(int timeInterval) {
    interval = timeInterval;
  }

or

class Timer {
  // props
  int startTime;
  int interval;

  //constructor
  Timer(int timeInterval) {
    interval = timeInterval;
  }

Although both options are correct the second option is preferred because in Java class names are capitalized by convention.

3 Likes

Wonderful that worked for that problem! my next issue comes with the if statements it’s saying these variables don’t exsits im confused if I need to replace the words with myTimer or something else

Timer myTimer;

int currentTime;
String s;

void setup() {
  size(1200, 600);
  myTimer = new Timer(1000);
  s ="";
}



void draw() {
  background(200);

  //timer stuff
  if (myTimer.complete()== true){
    if (countdownTime > 0){
    currentTime++;
    countdownTIme--;
    timer.start();
  } 
  }
  
  else {
    fill (255, 0, 0);
    rect(0,0, width, height);
  }// end cdt


  // text stuff
  fill(255);
  textSize(48);
  textAlign(CENTER);
  s= "Current time is" + currentTime;
  s +="Countdown time is " + countdownTime;
  text(s,width/2, height/2);




}Timer myTimer;

int currentTime;
String s;

void setup() {
  size(1200, 600);
  myTimer = new Timer(1000);
  s ="";
}



void draw() {
  background(200);

  //timer stuff
  if (myTimer.complete()== true){
    if (countdownTime > 0){
    currentTime++;
    countdownTIme--;
    timer.start();
  } 
  }
  
  else {
    fill (255, 0, 0);
    rect(0,0, width, height);
  }// end cdt


  // text stuff
  fill(255);
  textSize(48);
  textAlign(CENTER);
  s= "Current time is" + currentTime;
  s +="Countdown time is " + countdownTime;
  text(s,width/2, height/2);

}

You need to correct the code above you seem to have the sketch coded duplicated. Also you need to include the code for your timer class.

1 Like

Please post your entire code. In one go.

Please not as images but as text.

Thank you.

processing is case sensitive, so the typo here is relevant, use a small i

Won’t work, you need myTimer.start() - the name of the object

1 Like

Fixed that issue now saying it missing a “;” but i have one implemented there

class myTimer;

int currentTime;
String s;

void setup() {
  size(1200, 600);
  myTimer = new myTimer(1000);
  s ="";
}



void draw() {
  background(200);

  //timer stuff
  if (timer.complete()== true){
    if (countdownTime > 0){
    currentTime++;
    countdownTIme--;
    mytimer.start();
  } 
  }
  
  else {
    fill (255, 0, 0);
    rect(0,0, width, height);
  }// end cdt


  // text stuff
  fill(255);
  textSize(48);
  textAlign(CENTER);
  s= "Current time is" + currentTime;
  s +="Countdown time is " + countdownTime;
  text(s,width/2, height/2);




}

Are you still using the class?

2 Likes

That won’t work. (the keyword class is only used where you define the class)

When your class is named Timer the line to use the class is:

Timer myTimer = new Timer();

It’s convention to have a class name (Timer) start with a Capital Letter. The object derived from the class is with a small letter: myTimer

To use a variable or function of the class, say myTimer.start(); etc., using

  • the object name and
  • a dot . and
  • the function name / variable name

you don’t use Timer there, you don’t the content of the class directly, only as myTimer.something…

1 Like

Many errors just occurred saying the class “timer” doesnt exists and is suggesting a java import timer but that then makes even more issues

Timer myTimer = new Timer();

int currentTime;
String s;

void setup() {
  size(1200, 600);
  myTimer = new myTimer(1000);
  s ="";
}



void draw() {
  background(200);

  //timer stuff
  if (timer.complete()== true){
    if (countdownTime > 0){
    currentTime++;
    countdownTIme--;
    mytimer.start();
  } 
  }
  
  else {
    fill (255, 0, 0);
    rect(0,0, width, height);
  }// end cdt


  // text stuff
  fill(255);
  textSize(48);
  textAlign(CENTER);
  s= "Current time is" + currentTime;
  s +="Countdown time is " + countdownTime;
  text(s,width/2, height/2);




}

You need to copy paste the class at the end

Nice tutorial Objects / Processing.org

2 Likes

i apologize for not responding for so long I was contemplating restarting from the ground up and decide to do that. I’m trying to figure out why “millis” won’t work

class myTimer {
  // props
  int startTime;
  int interval;
  
  //constructor
  myTimer(int timeInterval) {
    interval = timeInterval;
  }
  //methods
  void start() {
    startTime = millis();
  }
  
  boolean complete(){
    
    int elapsedTime = millis() - startTime;
    if (elapsedTime > interval) 
    {
      return true;
    } 
    else {
      return false;
    }//end if
  } 
}//end class

The class is called Timer …

As I said, post your entire code, class and the rest…

sorry was driving but here is the class code and the button code




class myTimer {
  // props
  int startTime;
  int interval;
  
  //constructor
  myTimer(int timeInterval) {
    interval = timeInterval;
  }
  //methods
  void start() {
    startTime = millis();
  }
boolean complete(){
int elapsedTime = millis() - startTime;
if (elapsedTime > interval) 
{
  return true;
} 
else {
  return false;
}//end if

}
}//end class

class Button
{
  PVector Pos= new PVector(0, 0);
  float Width = 0;
  float Height = 0;
  color Color;
  String Text;
  Boolean Pressed = false;
  Boolean Clicked = false;
  // constructor to create a button
  Button(int x, int y, int w, int h, String t, int r, int g, int b)
  {
    Pos.x = x;
    Pos.y = y;
    Width = w;
    Height = h;
    Color = color(r, g, b);
    Text= t;
  }
  void update() //must be placed in void draw() to work
  {
    if (mousePressed==true &&mouseButton == LEFT && Pressed== false)
    {
      Pressed = true;
      if (mouseX>=Pos.x && mouseX <= Pos.x+Width && mouseY >= Pos.y && mouseY <= Pos.y+Height)
      {
        Clicked = true;
      } else
      {
        Clicked = false;
      }
      if (mousePressed !=true)
      {
        Pressed = false;
      }
    }
  }
    void render()// must be placed void draw to render the button to the screen
    {
      fill(Color);
      rect(Pos.x,Pos.y,Width,Height);
      
      
      fill(0);
      
      textAlign(CENTER,CENTER);
      text(Text,Pos.x+(Width/2),Pos.y+(Height/2));
      
    }
    //boolean isClicked()// Use this in a if statement to check if the button has clicked
    //{
    //return Clicked;
    
  }

This line implies that your class is named Timer,
not myTimer

Read the tutorial I posted above

1 Like