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 & HelpSyntax Questions › loading and image with an if statement
Page Index Toggle Pages: 1
loading and image with an if statement (Read 253 times)
loading and image with an if statement
Mar 10th, 2008, 8:00am
 
how do you load an image with an if statement.

I want to create a if statement that changes the image based on the arrow key I press.

code:

class fire {

PImage b;

void setup() {
 b = loadImage("green_flame.jpg");
 noLoop();
}

{

 if (key == CODED) {
   if (keyCode == UP) {
     
 image(b, 0, 0);

   } else if (keyCode == DOWN) {
     fillVal = 0;
   }
 } else {
   fillVal = 126;
 }
 
}
}

main code:

color fillVal = color(126);

void draw() {
 fill(fillVal);
 rect(25, 25, 50, 50);
}

void keyPressed() {
fire thefire = new fire();
}

Re: loading and image with an if statement
Reply #1 - Mar 10th, 2008, 9:17am
 
First of all draw() is never been called if you use noLoop in the setup. Then you dont need a class for this, only a globale variable that holds your state:

Code:

Image b;
int state;
void setup() {
b = loadImage("green_flame.jpg");
state = 0;
}

void draw() {
noFill();
switch (state){
case 0: image(b, 0, 0); break;
case 1: fill(0); break;
case 2: fill(125); break;
}
rect(25, 25, 50, 50);
}

void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
state = 0;
}else if (keyCode == DOWN) {
state=1;
}
}
else {
state = 2;
}
}

Page Index Toggle Pages: 1