We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › click a random appearing image
Page Index Toggle Pages: 1
click a random appearing image (Read 808 times)
click a random appearing image
Feb 2nd, 2010, 2:01pm
 
Hi folks,
this is the first processing project i am working at so if i created only rubbish please let me know...
So what i need is a programm that shows 5 pictures at random changing places. if you click picture nr1 there should appear the 6th picture as long as you hold the mousebutton.

Sounds easy but i cant find my mistakes since two days....
I enclosed my code, id seemes to work first but after the pictures changed their position for the first time nothing appeares if you click picture 1
i think there must be a big syntax error i can not find,
i yould be very pleased if some of you could help me or give me a hint where i must search for the mistake!

Trombone


float y[]= new float [6];
float x[] = new float [6];
PImage bilder[] = new PImage [6];

float o;
float p;
boolean zeichneHelge = false;

void setup(){

 size(600,600);
     for(int i=0;i<y.length;i=i+1)
   {
bilder[i]= loadImage (i+".jpg");
   }  
 }


void draw(){
 
background(255);

 for(int i=1;i<y.length;i=i+1)
 {
   y[i]=random(height-50);
   x[i]= random(width-50);
   image(bilder[i],x[i],y[i]);
 }

if (zeichneHelge){
image(bilder[0],0,0,width, height);
 }  
delay(2000);
}
void mousePressed()
{
 o = mouseX;
 p = mouseY;
 
if(o > x[1] && o < x[1] + 100 && p > y[1] && p < y[1] + 100)
{
 zeichneHelge = true;
}
}
void mouseReleased(){
 zeichneHelge=false;
}
Re: click a random appearing image
Reply #1 - Feb 2nd, 2010, 2:43pm
 
You have almost got it.  Delay is problematic because it actually freezes the program, refusing input.  What you need is a timer, so draw() is actually running continuously, but the squares are not being re-shuffled until the timer reaches a certain point.  Here is an example of what I mean.  In the example, the squares are also prevented from re-shuffling until the mouse is released:

Quote:
float y[]= new float [6], x[] = new float [6];
float o, p;
boolean zeichneHelge = false;
int timer=100;

void setup(){
  size(600,600);
  rectMode(CORNER);
}

void draw(){
  timer++;
  if ((timer>100) && (!zeichneHelge)){
    background(255);
    timer=0;
    stroke(0);
    for(int i=1;i<y.length;i=i+1){
      y[i]=random(height-100);
      x[i]= random(width-100);
      if (i==1) fill(255,0,0);
      else fill(100);
      rect(x[i],y[i],100,100);
    }
  }
  if (zeichneHelge){
    fill(255,0,0,2);
    noStroke();
    rect(0,0,width,height);
  }


void mousePressed(){
  o = mouseX;
  p = mouseY;
  if(o > x[1] && o < x[1] + 100 && p > y[1] && p < y[1] + 100){
    zeichneHelge = true;
  } 
}

void mouseReleased(){
  zeichneHelge=false;
}
Re: click a random appearing image
Reply #2 - Feb 3rd, 2010, 1:17pm
 
Hi,
I supposed that stopping the code with delay() might be a problem but i wasn´t abled to find an other solution!
However now it works well...
Thank YOu
Re: click a random appearing image
Reply #3 - Feb 3rd, 2010, 3:08pm
 
you're welcome -- personally I think delay() could be removed from the API; I don't know of a good use for it...
Page Index Toggle Pages: 1