Hello
Can any kind person please take a look at my code below and give me suggestions on how to improve this? I am new to processing and basically have minimal experience in programming
I placed several icons on the screen and the idea is to move them around when the mouse hovers near each one of them. I got it to work for the first icon (img2), but when I added img3 and img4 they only move when the mouse hovers near the upper left corner of img2. What did i do wrong?
// this one is the background
PImage img;
// icons
PImage img2;
PImage img3;
PImage img4;
// icons starting x and y position on the screen.
int img2x = 30;
int img2y = 50;
int img3x = 30;
int img3y = 150;
int img4x = 30;
int img4y = 200;
// this is the size for each icon
int boxSize = 100;
void setup() {
size(800,600);
img = loadImage("Default_800x600.jpg");
img2 = loadImage("p_icon_01.png");
img3 = loadImage("itunes.png");
img4 = loadImage("My-computer-icon.png");
}
void draw() {
background(0);
image(img,0,0);
image(img2, img2x, img2y);
image(img3, img3x, img3y);
image(img4, img4x, img4y);
checkMouse();
}
void checkMouse(){
// If the mouse is inside the box..
if(mouseX > img2x &&
mouseX < img2x+boxSize &&
mouseY > img2y &&
mouseY < img2y+boxSize
)
{
//if the mouse is in the area, rough syntax
//suggestions for improvement?
img2x = img2x + int(random(-30, 30));
img2y = img2y + int(random(-30, 30));
img3x = img3x + int(random(-30, 30));
img3y = img3y + int(random(-30, 30));
img4x = img4x + int(random(-30, 30));
img4x = img4y + int(random(-30, 30));
}
}