Mouse Pressed condition over moving images with random parameters?
in
Core Library Questions
•
1 years ago
Mouse Pressed condition over moving images with random parameters?
//How to execute the void that triggers the sound, my_image2 and color change (mousePressionado) ONLY when mousePressed over my_image (my image moves with random coordinates)???
int x;
int y;
int Imagem;
PImage my_image;
PImage my_image2;
PImage my_imageBackground;
int random;
import ddf.minim.*;//importing audio code
AudioPlayer player;
Minim minim;
Imagem Icon;
Imagem Icon2;
Imagem Icon3;
Imagem Icon4;
Imagem Icon5;
Imagem Icon6;
Imagem Icon7;
void setup() {
size(1024,768);
minim = new Minim(this);//stating audio player
Icon = new Imagem();
Icon2= new Imagem();
Icon3= new Imagem();
Icon4= new Imagem();
Icon5= new Imagem();
Icon6= new Imagem();
Icon7= new Imagem();
my_image= loadImage("lilas-toy-icon.png");
my_image2= loadImage("bma.gif");
my_imageBackground=loadImage("black_cube_world-1024x768.jpg");
}
void draw() {
background(my_imageBackground);
cursor(CROSS);
Icon.display();
Icon.moveHorizontally();
Icon.mousePressionado();
Icon.over();
Icon2.display();
Icon2.moveHorizontally();
Icon2.mousePressionado();
Icon2.over();
Icon3.display();
Icon3.moveHorizontally();
Icon3.mousePressionado();
Icon3.over();
Icon4.display();
Icon4.moveHorizontally();
Icon4.mousePressionado();
Icon4.over();
Icon5.display();
Icon5.moveHorizontally();
Icon5.mousePressionado();
Icon5.over();
Icon6.display();
Icon6.moveHorizontally();
Icon6.mousePressionado();
Icon6.over();
Icon7.display();
Icon7.moveHorizontally();
Icon7.mousePressionado();
Icon7.over();
}
class Imagem {
int xPos=x;
int yPos=y;
int direction=20;
boolean over;
Imagem() {
xPos=10;
yPos=200;
direction=20;
}
void display() {
image(my_image,xPos,yPos);
}
void over(){
if(mouseX == my_image.width || mouseY==my_image.height) {
over = true;
} else {
over = false;
}
}
void moveHorizontally(){
xPos=xPos+direction+(int)random(-50,+50);
yPos=yPos+direction+(int)random(-50,+50);
if (xPos>1000){
direction= -17;
}
if (xPos<10) {
direction= +17;
}
if (yPos>600){
direction= -17;
}
if (yPos<10) {
direction= +17;
}
}
void mousePressionado(){
if (over && mousePressed){
background(frameCount * 3 % 255, frameCount * 5 % 255,
frameCount * 7 % 255);
image(my_image2,mouseX,mouseY);
player = minim.loadFile("cartoon054.mp3");
player.play();
}
}
}
void stop()// void that is a part of the audio input
{
player.close();
minim.stop();
super.stop();
}
1