Webcam memory game Timer value problem
in
Integration and Hardware
•
1 year ago
Hey guys/girls,
I'm having an problem with a timer. together with some other student we're making an adaption of a mouse event based Memory game.
Whe changed the code so that it will response to a color tracking event. Now for it to work smoothly it should start the flip animation after the timer had finished. However I'm getting back strange value's from the timer.
Its the timer provided by:
Learning Processing
Daniel Shiffman
http://www.learningprocessing.com
Example 10-5: Object-oriented timer
The values im getting in return from println (timer); are:
Femi$Timer@578dfb
Femi$Timer@88b1e6
and so one.
Now even I can understand that the timer will never reach the limit of 5000 ms. So my event will never start.
What can I do to solve this problem. Make my on timer with some kind of iteration? Or am I on the right track but implementing it wrong?
The project is programmed OOP but for this forum Ive putted all the code together in one file. The resolutions are off so there are some memory blocks missing. But that isn't the bug I want to get rid of. (I could only copy the project from my classmates in this resolution or it wouldn't work). The timer to finish so my animation can start is the bug.
Thanks in advance
P.S. you got to use a red object for the tracking to work.
Line 174 should start when line 171 is finished. But with this timer value it never reaches 5000
The code is:
I'm having an problem with a timer. together with some other student we're making an adaption of a mouse event based Memory game.
Whe changed the code so that it will response to a color tracking event. Now for it to work smoothly it should start the flip animation after the timer had finished. However I'm getting back strange value's from the timer.
Its the timer provided by:
Learning Processing
Daniel Shiffman
http://www.learningprocessing.com
Example 10-5: Object-oriented timer
The values im getting in return from println (timer); are:
Femi$Timer@578dfb
Femi$Timer@88b1e6
and so one.
Now even I can understand that the timer will never reach the limit of 5000 ms. So my event will never start.
What can I do to solve this problem. Make my on timer with some kind of iteration? Or am I on the right track but implementing it wrong?
The project is programmed OOP but for this forum Ive putted all the code together in one file. The resolutions are off so there are some memory blocks missing. But that isn't the bug I want to get rid of. (I could only copy the project from my classmates in this resolution or it wouldn't work). The timer to finish so my animation can start is the bug.
Thanks in advance
P.S. you got to use a red object for the tracking to work.
Line 174 should start when line 171 is finished. But with this timer value it never reaches 5000
The code is:
- import processing.video.*;
- Capture cam;
- PImage photo, verschil;
- float[] x = new float[500];
- float[] y = new float[500];
- float[] speed=new float[500];
- float[] Size=new float[500];
- float[] Color=new float[500];
- float[] xpos=new float[5];
- float[] ypos=new float[5];
- float[] opas=new float[500];
- int click;
- int md=0;
- int blockMode=2;
- int Delay;
- int matchNum;
- int [] pairs= {
- -9999,6,7,5,15,13,16,9,11,4,10,8,2,3,1,12,14
- };
- boolean fail;
- boolean won;
- PImage[] allIm=new PImage[50];
- int col=2;
- int row=3;
- Cell[][]grid;
- int blockX=300;
- int blockS=1;
- PFont myfont;
- String []R;
- int textAll=3;
- float picLoc;
- int kleurX = 0;
- int kleurY = 0;
- void setup() {
- size(640,360);
- strokeWeight(1);
- cam = new Capture(this, 640,360);
- photo = createImage(640,360, RGB);
- verschil = createImage(640,360, RGB);
- smooth();
- R=loadStrings("dieruitleg.txt");
- picLoc=random(40,430);
- for(int g=1;g<8;g++) {
- allIm[g]=requestImage("afbeelding boerderij "+g+".png");
- }
- for(int i=0;i<100;i++) {
- x[i]=random(-50,550);
- y[i]=random(700,900);
- speed[i]=random(1,3);
- Size[i]=random(50,80);
- Color[i]=random(255);
- opas[i]=0;
- }
- grid=new Cell[col][row];//maak een grid aan
- for( int j=0;j<col;j++) {
- for(int k=0;k<row;k++) {
- grid[j][k] = new Cell(20+j*409,55+k*205,130,(j*4)+k);
- xpos[j]=23+j*140;
- ypos[k]=23+k*140;
- }
- }
- }
- int vPixels;
- void draw() {
- if (cam.available() == true) {
- photo.copy(cam, 0, 0, cam.width, cam.height, 0, 0, photo.width, photo.height);
- cam.loadPixels();
- int index = 0;
- for (int y = 0; y < cam.height; y++) {
- for (int x = 0; x < cam.width; x++) {
- color pixelWaarde = cam.pixels[index];
- float kleurAfstand = abs(red(pixelWaarde)-255)+abs(green(pixelWaarde)-0)+abs(blue(pixelWaarde)-0);
- if (kleurAfstand > 120 && kleurAfstand < 160) {//kleuren range
- kleurY = y;
- kleurX = x;
- }
- index++;
- }
- }
- cam.read();
- image(cam, 0, 0);
- fill(255,255,255,100);
- ellipse(kleurX,kleurY,30,30);
- }
- fill(255,255,255);
- stroke(0,0,0);
- strokeWeight(2);
- fill(0);
- strokeWeight(2);
- fill(255,90);
- for( int j=0;j<col;j++) {
- for(int k=0;k<row;k++) {
- grid[j][k].display();
- }
- }
- if((kleurX>520 &&kleurX<600 && kleurY>580&&kleurY<600) && mousePressed==true) {
- md=2;
- blockMode=2;
- for( int j=0;j<col;j++) {
- for(int k=0;k<row;k++) {
- grid[j][k].flip=5;
- }
- }
- }
- }
- // Learning Processing
- // Daniel Shiffman
- // http://www.learningprocessing.com
- // Example 10-5: Object-oriented timer
- 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;
- }
- }
- }
- Timer timer;
- class Cell {
- float x;
- float y;
- float s;
- int flip=1;
- int I;
- float[]X=new float[20];
- Cell(float celX,float celY, float celS,int index) {
- x=celX;
- y=celY;
- s=celS;
- I=index+1;
- click=0;
- }
- void display() {
- fill(40,152,37);
- stroke(#34d614);
- strokeWeight(1);
- rect(x,y,s,130);
- if(md!=5 && blockMode==2) {
- if(mousePressed == false) md=0;//timer is afgelopen
- }
- if((kleurX>x && kleurX< x+s && kleurY>y && kleurY<y+s) && md!=1)//rode kleur over blokje
- {
- timer = new Timer(5000);
- timer.start();
- println (timer);
- }
- else if(((kleurX>x && kleurX< x+s && kleurY>y && kleurY<y+s) && md!=1) && timer.isFinished())//rode kleur voor blokje en timer is afgelopen
- {
- click+=1;
- md=1;
- Delay=0;
- flip=0;
- X[I]=x;
- if(click==1)
- {
- matchNum=pairs[I];
- }
- if(click==2)
- {
- if(I==matchNum)
- {
- won=true;
- click=0;
- md=5;
- }
- if(I!=matchNum)
- {
- click=0;
- fail=true;
- }
- }
- }//einde kleur match
- ////fliping animation
- if(flip==0) {
- s=s-6;
- x=x+3;
- }
- if(s<-130 && x>X[I]+130) {
- s=130;
- x=X[I];
- flip=2;
- }
- if(flip==2)image(allIm[I],X[I],y);
- ///////////////////////////////////////////////////////////////////
- if(won==true) {
- Delay=Delay+1;
- if(Delay>800) {
- blockMode=1;
- md=5;
- block();
- }
- }
- ////fail/////
- if(fail==true) {
- if(flip==0) {
- s=s-6;
- x=x+3;
- }
- if(s<-130 && x>X[I]+130) {
- s=130;
- x=X[I];
- flip=2;
- }
- if(flip==2) {
- image(allIm[I],X[I],y);
- Delay=Delay+1;
- }
- if(Delay>100) {
- for( int j=0;j<col;j++) {
- for(int k=0;k<row;k++) {
- grid[j][k].flip=5;
- fail=false;
- md=0;
- click=0;
- }
- }
- }
- }
- ////fail ends////
- }
- void block() {
- if(blockMode==1) {
- fill(255,255,255);
- stroke(#3fb926);
- strokeWeight(3);
- rect(blockX,165,blockS,345);
- if(blockX>25) {
- blockX=blockX-6;
- blockS=blockS+12;
- }
- else {
- fill(0);
- R=loadStrings("dieruitlegscheiding.txt");
- String rovioAll=R[matchNum];
- String[]AllText=split(rovioAll,"/");
- for(int k=0;k<AllText.length;k=k+1) {
- textSize(20);
- text(AllText[k],40,220+(25*k));
- image(allIm[matchNum],picLoc,355);
- }
- textSize(10);
- text("Sluit door plaatje te raken",300,500);
- if(kleurX>x && kleurX< x+s && kleurY>y && kleurY<y+s) {
- Delay=0;
- blockX=300;
- blockS=1;
- blockMode=2;
- md=2;
- won=false;
- }
- }
- }
- }
- }
1