mouseClicked--URGENT help needed
in
Programming Questions
•
2 years ago
I am taking a class in processing and this is part of my first project. What I need is for the image that is active depending on keyPressed (there are 4 images, changing each time a different key is pressed) to change to one of three colors of the active image upon mouseClick. However, whenever I use keyPressed to change to images 2-4 and then click on it, it reverts to image 1. I need something like (keyPressed)image2_color1(click)image2_color2(click)image2_color3(back to beginning) and not (keyPressed)image2_color1(click)image1_color1. Help would be greatly appreciated-- the project is due Monday and this is the only problem I have left with the code, which is below.
Thank!
PImage bg, bigbird_green, bigbird_pink, bigbird_yellow, cookiemonster_purple, cookiemonster_blue, cookiemonster_yellow, elmo_blue, elmo_green, elmo_red, grouch_green, grouch_pink, grouch_purple;
int charcolor;
//declares type and color of image
float xpos, ypos, imw, imh;
PImage active;
float xspeed=1;
float yspeed=1;
int xdirection=1;
int ydirection=1;
//declares integers for speed and direction of object
void setup() {
size(900, 337);
bg=loadImage("sesamestreet.png");
//background img
bigbird_green=loadImage("bigbird_green.png");
bigbird_pink=loadImage("bigbird_pink.png");
bigbird_yellow=loadImage("bigbird_yellow.png");
cookiemonster_purple=loadImage("cookiemonster_purple.png");
cookiemonster_blue=loadImage("cookiemonster_blue.png");
cookiemonster_yellow=loadImage("cookiemonster_yellow.png");
elmo_blue=loadImage("elmo_blue.png");
elmo_green=loadImage("elmo_green.png");
elmo_red=loadImage("elmo_red.png");
grouch_green=loadImage("grouch_green.png");
grouch_pink=loadImage("grouch_pink.png");
grouch_purple=loadImage("grouch_purple.png");
active=grouch_green;
xpos=100;
ypos=100;
imh=200;
imw=150;
//declares base position, h and w of images
image(bg, 0, 0);
charcolor=1;
noFill();
smooth();
}
void mouseClicked(){
if(charcolor==4){
active=grouch_green;
charcolor=5;
}
else if(charcolor==5){
active=grouch_pink;
charcolor=6;
}
else if(charcolor==6){
active=grouch_purple;
charcolor=4;
}
//end grouch mouseclick color change
if(charcolor==7){
active=elmo_red;
charcolor=8;
}
else if(charcolor==8){
active=elmo_blue;
charcolor=9;
}
else if(charcolor==9){
active=elmo_green;
charcolor=10;
}
//end elmo mouseclick color change
if(charcolor==11){
active=cookiemonster_blue;
charcolor=12;
}
else if(charcolor==12){
active=cookiemonster_yellow;
charcolor=13;
}
else if(charcolor==13){
active=cookiemonster_purple;
charcolor=11;
//end cookie monster mouseClick color change
}
if(charcolor==1){
active=bigbird_green;
charcolor=2;
}
else if(charcolor==2){
active=bigbird_yellow;
charcolor=3;
}
else if(charcolor==3){
active=bigbird_pink;
charcolor=1;
}
//end big bird mouseClick color change
}
void draw() {
image(bg, 0, 0);
image(active, xpos, ypos, imw, imh);
xpos=xpos+(xspeed*xdirection);
ypos=ypos+(yspeed*ydirection);
if (xpos > width-imw || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-imh || ypos < 0) {
ydirection *= -1;
}
}
void keyPressed (){
if(key=='g'){
active=grouch_green;
}
else if(key=='c'){
active=cookiemonster_blue;
}
else if(key=='e'){
active=elmo_red;
}
else if(key=='b'){
active=bigbird_yellow;
}
//if keys g, c, e, or b are pressed, image changes accordingly
}
1