Hello all! I am very very new to coding and am attempting to create a code in which I can click on an image, have it change into a different image, then be able to click and drag the new image. I want this to be able to happen with four separate images. To further specify what I am trying to do, I hope to have a kind of garden with 4 plant sprouts that once clicked turn into various objects and can be moved around the screen.
This is the code that I have made so far with what I have been able to learn on my own.
Unfortunately, I am barely at the point where I can get the plant sprout images (I have called them farm_image# in the code) to change to the object image when clicked. It changed no matter where I click on the screen, not only on the image, and the old image is still appearing even though I re-added the background. I also don’t have any idea where to start in terms of dragging the images around the screen. In short, I am quite lost and would really appreciate any help!
keep your positions of the images stored as variable (also in 2 arrays). Otherwise you cannot change the position when dragging, you need to store and change the position
You have 3 parallel arrays: data
for image 0 are in slot 0 of farm_images, x, and y array
for image 1 are in slot 1 of farm_images, x, and y array
and so on
What’s going on
Then the dragging is easy (and changing of images):
dragging starts with mousePressed: we search the clicked image and store its number in selected . We set a marker hold to true so we know, we drag the mouse.
During hold is true, a red corner is shown (in draw()). Also the position of the selected image is changed (in draw()).
dragging ends with mouseReleased(). We set hold to false.
The function mouseClicked is called when we release the mouse immediately. We swap an image. Not sure what you want to happen here.
Tricks used
A trick is to use return to leave a function immediately.
This for (PImage pg : farm_images) { is a short form of a for-loop. It loops over all images in the array and in each loop one image is loaded in to pg, then the next and so on.