Drag & Drop
in
Programming Questions
•
6 months ago
In my code, i have six images that i want to drag and drop, but i want them to drag and drop separately. Right now when i drag one image all images move simultaneously. Can anyone help me make them drag and drop separately?
Here's the code:
PImage img1;
PImage img;
PImage img2;
PImage img3;
PImage img4;
PImage img5;
PImage img6;
PImage img7;
PImage img8;
PImage img9;
PImage img10;
PImage img11;
PImage img12;
PImage img13;
PImage img14;
PImage img15;
PImage img16;
PImage img17;
PImage img18;
PImage img19;
PImage img20;
PImage img21;
PImage img22;
float rockX;
float rockY;
float rockZ;
float rockW;
float rockA;
float rockB;
float rockC;
float rockD;
float rockE;
float rockF;
float rockG;
float rockH;
float watermelonX;
float watermelonY;
boolean rockDragging;
void setup() {
size (700, 700);
rockX = 150;
rockY = 400;
rockZ = 200;
rockW = 400;
rockA = 250;
rockB = 400;
rockC = 150;
rockD = 370;
rockE = 200;
rockF = 370;
rockG = 250;
rockH = 370;
watermelonX = 250;
watermelonY = 600;
rockDragging = false;
img1 = loadImage("background2.jpg");
img = loadImage("house.png");
img2 = loadImage("watermelon back.png");
img3 = loadImage("rock.png");
img4 = loadImage ("rock2.png");
img5 = loadImage("rock3.png");
img6 = loadImage ("rock4.png");
img7 = loadImage("rock5.png");
img8 = loadImage ("rock6.png");
img9 = loadImage("rock7.png");
img10 = loadImage ("rock8.png");
img11 = loadImage("rock9.png");
img12 = loadImage ("rock10.png");
img13 = loadImage("rock11.png");
img14 = loadImage ("rock12.png");
img15 = loadImage("rock13.png");
img16 = loadImage ("rock14.png");
img17 = loadImage("rock15.png");
img18 = loadImage ("rock16.png");
img19 = loadImage("rock17.png");
img20 = loadImage ("rock18.png");
img21 = loadImage("rock19.png");
img22 = loadImage ("rock20.png");
background (#2BD8ED);
}
void mousePressed()
{
if (mouseInRock (rockX, rockY, 60, 50));
if (mouseInRock (rockZ, rockW, 60, 50));
if(mouseInRock (rockA, rockB, 60, 50));
if (mouseInRock (rockC, rockD, 60, 50));
if (mouseInRock (rockE, rockF, 60, 50));
if (mouseInRock (rockG, rockH, 60, 50));
if (mouseInRock (watermelonX, watermelonY, 150, 80));
rockDragging = true;
}
void mouseReleased()
{
rockDragging = false;
}
void draw() {
//draws the still images
image(img1, 0, 0, 700, 700); //background
image(img, 0, 0, 700, 700); //house
image (img2, watermelonX, watermelonY, 150, 80); //watermelon
//rocks
if (rockDragging) {
rockX = mouseX;
rockY = mouseY;
rockZ = mouseX;
rockW = mouseY;
rockA = mouseX;
rockB = mouseY;
rockC = mouseX;
rockD = mouseY;
rockE = mouseX;
rockF = mouseY;
rockG = mouseX;
rockH = mouseY;
watermelonX = mouseX;
watermelonY = mouseY;
}
image(img3, 0, 400, 300, 300);
image (img4, 50, 400, 300, 300);
image(img5, 100, 400, 300, 300);
image (img6, rockX, rockY, 60, 50);
image(img7, rockZ, rockW, 60, 50);
image (img8, rockA, rockB, 60, 50);
image(img9, 300, 400, 300, 300);
image (img10, 350, 400, 300, 300);
image(img11, 400, 400, 300, 300);
image (img12, 450, 400, 300, 300);
image(img13, 0, 370, 300, 300);
image (img14, 50, 370, 300, 300);
image(img15, 100, 370, 300, 300);
image (img16, rockC, rockD, 60, 50);
image (img17, rockE, rockF, 60, 50);
image(img18, rockG, rockH, 60, 50);
image (img19, 300, 370, 300, 300);
image(img20, 350, 370, 300, 300);
image (img21, 400, 370, 300, 300);
image (img22, 450, 370, 300, 300);
fill (255);
noStroke();
rect (10, 600, 200, 90);
fill (0);
textSize(12);
text("Move the rocks that are", 20, 630);
text("in Watermelon's way,", 20, 645);
text("and then take him", 20, 660);
text("to the neighbor's house.", 20, 675);
}
boolean mouseInRock (float _x, float _y, float _w, float _h) {
if (mouseX > _x && mouseY > _y && mouseX < _x +_w && mouseY < _y + _h) {
return true;
}
return false;
}
1