Using timer to display and hide object in Processing

I'm a beginner program and trying to create a "Simon" memory game using Processing. I'm currently using a timer to display a new object every second. However, I cannot determine how to make the object disappear shortly after it is displayed (e.g., half a second after it appears). I've tried creating a second timer object that redraws the background after a certain amount of time, but it's just not working.

Additional info: I have an array of colors (red blue green and yellow), and if there happen to be two "reds" in a row i.e., [1] = color(255,0,0) and [2] = color(255,0,0), the application displays one red box on top of the other, rather than making the first box disappear first (i.e., it makes it seem as if the red box was displayed for a prolonged period of time). For simplicity on the forum, I use int j=0 and j++ rather than advancing through the array.

int j = 0; void draw() { if (displayTimer.isFinished()){ hideTimer.start(); if(hideTimer.isFinished()){ drawBackground(); } if (nomisSequence[j] == color(255, 0, 0)) { drawBackground(); button1.display(); } else if (nomisSequence[j] == color(0, 255, 0)) { drawBackground(); button2.display(); } else if (nomisSequence[j] == color(0, 0, 255)) { drawBackground(); button3.display(); } else if (nomisSequence[j] == color(255, 255, 0)) { drawBackground(); button4.display(); } if(hideTimer.isFinished()){ drawBackground(); } j++; displayTimer.start(); hideTimer.start(); } }

displayTimer and hideTimer are objects I made in a Timer class:

class Timer { //paramters int savedTime; int totalTime; //constructor Timer(int tempTotalTime){ totalTime = tempTotalTime; } void start(){ savedTime = millis(); } boolean isFinished(){ int passedTime = millis() - savedTime; if(passedTime > totalTime){ return true; } else{ return false; } } }

Tagged:

Answers

  • _vk_vk
    edited December 2015

    I would use a flag to draw stuff.

    You just need two states draw and not-draw

    So you might jus say

    if(drawStuff){//do draw}

    Then use timer to control the flag,

    if(timer1.isFineshed()){drawStuff = true; timer1.reset(); timer2.start()};

    if(timer2.isFinished()){drawStuff = false; //reset start...}

Sign In or Register to comment.