code for shifting between images is not working
in
Programming Questions
•
1 year ago
Initially I was trying to pull out the pixels of an image and draw circles using the color of that pixel when the mouse is at that position. It worked, then I tried to create an array of 6 images so every time when I press the right arrow key, it shifts to the next image and do the same thing to the new image. But this loop is not working(I highlighted this part). I tried printing out the boolean "next", which controls the shifting of images. The boolean value is right, but the loop it controls is not working. Can anybody help me? I have my code below.
PImage actress;
PImage rose;
PImage cake;
PImage phantom;
PImage rabbit;
PImage rain;
int s=600, minSize = 2;
int steps = 5;
boolean next;
boolean previous;
boolean shft;
boolean imp;
int curimage = 0;
PImage image1;
PImage iarray[];
dot mom;
void setup() {
size(s,s);
iarray = new PImage[6];
actress = loadImage("actress.jpg");
iarray[0]=actress;
rose = loadImage("rose.jpg");
iarray[1]=rose;
cake = loadImage("cake.png");
iarray[2]=cake;
phantom = loadImage("phantom of the opera.png");
iarray[3]=phantom;
rabbit = loadImage ("rabbit.png");
iarray[4]=rabbit;
rain = loadImage("raindrops on window.jpg");
iarray[5]=rain;
image1=iarray[curimage];
//image1.resize(s,s);
smooth(); noStroke();
mom = new dot(s/2,s/2,s,s/2,s/2,s);
}
void draw(){
updateImage();
mom.draw();
}
void updateImage() {
background(0);
println(next);
if(next=true){
if (curimage<5) {
curimage=curimage++;
}
else if (curimage==5){
curimage=0; }
next=false;
fill(0);
rect(0,0,600,600);
}}
void mouseMoved() {
mom.interact();
}
class dot {
boolean alife = true;
int x0, y0, d0, x, y, d;
color c0, c;
float t;
dot[] kids;
dot(int _x0, int _y0, int _d0, int _x, int _y, int _d) {
x0=_x0; y0=_y0; d0=_d0; x=_x; y=_y; d =_d;
c = iarray[curimage].get(x,y); c0 = iarray[curimage].get(x0,y0);
}
void impressionize() {
if (imp==true) {
background(0);
rect(0,0,600,600);
} }
void draw() {
t = constrain(t + 1f/steps ,0,1);
if(alife) {
float xt = lerp(x0, x, t), yt = lerp(y0, y, t), dt = lerp(d0, d, t);
color ct = lerpColor(c0, c, t);
fill(ct);
if (shft==false){
ellipse(xt,yt,dt,dt);
}else{
rect(xt,yt,dt,dt);
}
}
else for(int i=0; i<4; i++) kids[i].draw();
}
void interact() {
if(alife) { if (t==1 && d>minSize) giveBirth(); }
else kids[(mouseX-x<0?2:0) + (mouseY-y<0?1:0)].interact();
}
void giveBirth() {
int e = d/4, f = d/2;
kids = new dot[] {
new dot(x,y,d,x+e,y+e,f), new dot(x,y,d,x+e,y-e,f),
new dot(x,y,d,x-e,y+e,f), new dot(x,y,d,x-e,y-e,f)
};
alife = false;
}}
void keyPressed() {
if (key == CODED) {
if (keyCode == RIGHT) {
next=true;
println("right key now pressed");
}
if (keyCode == SHIFT) {
if (shft == true){
shft = false;}
else if (shft == false){
shft = true;}
}}
if (key == 'i' || key == 'I') {
if (imp == true){
imp= false;}
else if (imp == false){
imp = true;}
}}
1