Help with click to shoot

hello i need help with my code. i need to be able to click the picture with the cross hair and for it to fall but nothing ive done seems to work. if possible could you guys show me how to do it using a while loop? also hi m new

int rate = 1000000000;
int rad = 10;        
int xpos, ypos;     

int xspeed = 1; 
int yspeed = 1; 
 
int xdirection = 4; 
int ydirection = 5; 
PImage img;

int vrint = 1;
float i = 0;

void setup() {
  size(800, 400);
  frameRate(rate);
  
  xpos = width/2;
  ypos = height/2;
}
void draw(){
  background(102, 204, 255);
  display();
  
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
 
  if (xpos > width-rad || xpos < rad) {
    xdirection *= -1;
    xpos += random(-1.1,1.1); 
  }
  if (ypos > height-rad || ypos < rad) {
    ydirection *= -1;
    ypos += random(-1.1,1.1);
    
    
    
    
     
  }
 
  
  
}
void display(){
 
  
  img = loadImage("vrinten1.png");
 image(img, xpos, ypos, width/9, height/9);
 img = loadImage("rock1.png");
     
     
     
     
     image(img, 0, 360, width/9, height/9);
  img = loadImage("groundsprite.png");
     image(img, 0, 335, height, width/9);
      img = loadImage("groundsprite.png");
     image(img, 200, 335, height, width/9);
      img = loadImage("groundsprite.png");
     image(img, 400, 335, height, width/9);
      img = loadImage("groundsprite.png");
     image(img, 600, 335, height, width/9);
  


if (mousePressed) {
    stroke(255);
  } else {
    stroke(0);
  }
  line(mouseX-20, mouseY, mouseX+20, mouseY);
  line(mouseX, mouseY-20, mouseX, mouseY+20);      
}

void mouseReleased() {
    while(mouseX ==xpos && mouseY==ypos){
       img = loadImage("groundsprite.png");
        image(img, 0, 100, height, width/9);

      
      
      


    
      
    }
}
1 Like

Hello and welcome,

First, please format your code so that it will be easier for others to read and easier for people to help.

You can do this by highlighting your original post (just the code areas) and click on </> in the menu bar above.
And voila!
:slightly_smiling_face:

1 Like

bar

where is it? i cant seem to find it. is there any way to just do it manually? also thank you

After posting it’s in the command bar in the forum

1 Like

there i got it thank you
@Chrisir

so does any know how to help me

1 Like

Hello and welcome to Processing!

So slight issue with your code… It contains images. You see, when you download an image and import it to your code, then it becomes unique to you. In order to run this code, I have to find and download each and everyone of those images. But I can’t do that, so i can’t really run your code…

However, I can tell you how a while loop works. So here’s how:

while (something is true)
{
    do something
}

So basically, the code inside the loop with run as long as the condition is true. For example:

int number = 0;

while (number < 10)
{
    println(number + " ");
    number += 1;
}

So this will print a sort of list to the console. It will print number, and then add one to it. So the output should look something like this:

0 1 2 3 4 5 6 7 8 9

Notice how the loop stopped after number equaled 10.

And yeah, that’s about it for while loops. Hope this helps!

2 Likes

Hello,

Since we can’t run your code because there are image files connected to your sketch, I can help only based on your description of what you are trying to do.

It sounds like the goal is:

  1. if the mouse is over (also known as a rollover) a certain image and
  2. you click the mouse while over the image, it will fall to the bottom of the screen.

First, look at the switch button (via Boolean) ex 5-5 on the Learn Processing site, link below, to see how to write the code for a rollover. And its placement within the code flow.

You can also watch on The Coding Train channel – aka Dan Shiffman – on youtube for additional explanation about conditional statements / boolean expressions. That playlist is here:

Second, after you have set up the boolean switch statement, then you need to add a line of code that moves the y position of the image down after you have clicked on it.
This should get you going in the right direction but, if you need more help, please ask.

****I know you mentioned in your original question about using a while loop. I recommend reading about the while loop in the reference to confirm how and when to use. Here:

2 Likes