Trouble with mousePressed!

edited December 2016 in Questions about Code

Hey, I have a problem with some code and would really appreciate it if someone could help me with it.

I am trying to create a duck clicking game, but I have some problems with the clicking: So I need to get the duck back to x = 0, as it is moving from left to right, and get the int score to + 1, but those parts aren't working! And my assignment is due in pretty soon. Here is the code:

        if((mouseX>dX && mouseX<dX+dW && mouseY>dY && mouseY<dY+dH)) {
           image(duck, dX, dY, 100, 90);
           score = score + 1;
           if (mousePressed == true) {
              dX = 0;
           } else {
          }
        } else {
        }

dX is the x value of the duck. dY is the y value of the duck. dW is the width of the duck. dH is the height of the duck.

Please respond soon!

P.S. I am new to Processing :P and please notify me of my mistakes. Thanks :3

Tagged:

Answers

  • edited November 2016

    Here is all of my code in the next comment.

  • edited November 2016
    int x = 40;
    int y = 10;
    
    int dX = 0;
    int dY = 134;
    int dW = 100;
    int dH = 90;
    
    int mouseX;
    int mouseY;
    
    int score = 0;
    
    PImage duck;
    PImage bg;
    PImage scope;
    
    void setup(){
      size(800,600);
      textSize(35);
      noStroke();
      duck= loadImage("bird.png");
      bg = loadImage("bg.jpg");
      scope = loadImage("scope.png");
    }
    
    void draw() {
      background(bg);
    
      cursor(scope, mouseX, mouseY);
    
      image(duck, dX, dY, 100, 90);
    
      text("Score : "+ score, x, y);
    
      frameRate(9000);
      dX = dX + 1;  
      if (dX > 800) {
        float rand = random(0, width);
        dY = round(rand);
        dX = 0;
      }  
    
    
      if((mouseX>dX && mouseX<dX+dW && mouseY>dY && mouseY<dY+dH)) {
        fill(255);
        image(duck, dX, dY, 100, 90);
        score = score + 1;
          if (mousePressed == true) {
            dX = 0;
          } else {
          }
      } else {
    
      }
    }
    
  • what are mouseX and mouseY? print them out in draw().

    then tell me why they are always 0. where do you set them?

  • What koogs wants you to discover by your own is that you should remove line 9 and 10.

    You don't have to declare variables like mouseX, mouseY, width, height etc. In fact, you should most definitely not name your variables names that are already taken. Whether it's by you or by processing. The being that you'll confuse yourself, the programming or both. It is only a matter of time.

  • What koogs wants you to discover by your own is that you should remove line 9 and 10.

    yeah, so much for that, eh?

  • Thanks! I'll try it!

  • It's not working ;( . When I hover my mouse on the window it says: " "IndexOutOfBoundsException: invalid hotSpot". Please help me someone!!!

  • Koogs mouseX and mouseY are the x and y values of the mouse. Also, when I state mouseX and mouseY in void draw it still doesn't work.

  • The mouseX and mouseY values are continuously detected as it works on the outside before void setup.

  • edited November 2016

    mouseX and mouseY are reserved variables, you shouldn't be defining them at all.

    when you do you override the standard variables which are the ones that are being updated internally.

    remove lines 9 and 10. (as eeyorelife pointed out)

  • if you still have errors post your updated code.

  •     int x = 40;
        int y = 10;
    
        float dX = 0;
        int dY = 134;
        int dW = 100;
        int dH = 90;
    
        int score = 0;
    
        PImage duck;
        PImage bg;
        PImage scope;
    
        void setup(){
          size(800,600);
          textSize(35);
          noStroke();
          //fullScreen();
          //textSize(25);
          duck= loadImage("bird.png");
          bg = loadImage("bg.jpg");
          scope = loadImage("scope.png");
        }
    
        void draw() {
    
          background(bg);
          //image(scope, mouseX, mouseY, 273, 281);
    
          cursor(scope, mouseX, mouseY);
    
          image(duck, dX, dY, 100, 90);
    
          text("Score : "+ score, x, y);
    
          frameRate(9000);
          dX = dX + 2;  
          //float rand = random(0, width);
          //y = round(rand);
          if (dX > 800) {
            float rand = random(0, width);
            dY = round(rand);
            dX = 0;
          }  
    
    
          if((mouseX>dX && mouseX<dX+dW && mouseY>dY && mouseY<dY+dH)) {
            fill(255);
            image(duck, dX, dY, 100, 90);
            score = score + 1;
              if (mousePressed == true) {
                dX = 0;
              } else {
              }
          } else {
    
          }
        }
    
  • edited November 2016

    It has errors with the cursor(scope, mouseX, mouseY) only when I move my mouse back into the opened window. What shall I put in place of it?

  • Hello:

    Just remove mouseX and mouseY from this line

    cursor(scope, mouseX, mouseY);, so it stays like this

    cursor(scope);

    besides you should change line 51 to 53 and prove this

    score = score + 1;
    println("Score now is "+score);
    

    I hope it works fine fopr you

  • Thanks so much laimperiestro!!! :3

    I did what you said, and the clicking element worked!!!

    BUT, when I changed the cursor(scope, mouseX, mouseY); to cursor(scope); there was a error: ' IndexOutOfBoundsException: invalid hotSpot '

    But thanks anyway!!! :)

  • edited December 2016

    I added some lines of code because I the cursor mouseX and mouseY was interfering with the clicking concept. I added and changed a few lines:

        int x = 10;
        int y = 40;
    
        int mX = mouseX;
        int mY = mouseY;
    
        int dX = 0;
        int dY = 134;
        int dW = 100;
        int dH = 90;
    
        int score = 0;
    
        PImage duck;
        PImage bg;
        PImage scope;
    
        void setup(){
          size(800,600);
          textSize(35);
          noStroke();
          //fullScreen();
          //textSize(25);
          duck= loadImage("bird.png");
          bg = loadImage("bg.jpg");
          scope = loadImage("scope.png");
        }
    
        void draw() {
    
          background(bg);
          //image(scope, mouseX, mouseY, 273, 281);
    
          cursor(scope, mX, mY);
    
          image(duck, dX, dY, 100, 90);
    
          text("Score : "+ score, x, y);
    
          frameRate(9000);
          dX = dX + 2;  
          //float rand = random(0, width);
          //y = round(rand);
          if (dX > 800) {
            float rand = random(0, width);
            dY = round(rand);
            dX = 0;
          }  
    
    
          if((mouseX>dX && mouseX<dX+dW && mouseY>dY && mouseY<dY+dH)) {
            fill(255);
            image(duck, dX, dY, 100, 90);
              if (mousePressed == true) {
                score = score + 1;
                println("Score now is "+score);
                dX = 0;
              } else {
              }
          } else {
    
          }
        }
    

    As you can see I added in line 4 and 5 int mX = mouseX; int mY = mouseY; and then replaced the mouseX and mouseY in the scope code cursor(scope, mouseX, mouseY); to cursor(scope, mX, mY); and it worked!

    Thanks to everyone who helped me!

  • @SowrCrem You need to understand mouseX and mouseY are reserved system variables. You are allowed to use them anytime. You can use them in draw to get the actual mouse pointer. The following statements:

    int mouseX;
    int mouseY;
    

    are not proper. You are blocking access to the values of the mouse position. That is the reason why koogs was telling you to remove lines 9 and 10 in your first post. Please notice your new values mx and my do not have any effect in your code, only because you don't really use them.

    I provide a link to the reference for you to explore:

    https://processing.org/reference/mouseX.html

    Kf

  • @SowrCrem Have you read the reference for cursor() ? https://processing.org/reference/cursor_.html is the reference if you'd like to.

    As @kfrajer here points out, you mX and mY is not actually being used. Actually, they are not even being updated.

    It seems like you want to feed the location of the cursor to the function cursor(). But that is not necessary. The cursor knows exactly where itself is at all times.

    But it works, so why change it? Correct. At least for now. If you don't understand how or why you code works it is likely to become a problem later when editing it.

  • @Eeyorelife Your link to the reference of cursor is broken, there shouldn't be a space between cursor and .html .
    As to the question, the second and third arguments in cursor(img, x, y) are not the position variables...

  • @Eeyorelife I have read the reference for it by finding cursor(CROSS);. I tried my just inserting my PImage name 'scope', but it didn't work so I googled it. I did find the cursor( img, x, y), and then I place mouseX and mouseY in the c and y positions, and it did work (it was before I added the clicking system). What I don't understand is that why didn't it work ( I know @Lord_of_the_Galaxy just told me, but I don't understand arguments and position variables - sorry!). Anyways thanks for all the support! :)

  • Oh well, I'll try it myself. It just doesn't make sense though. Why don't you draw an ellipse at the position of the cursor to help troubleshoot?(use ellipse(mouseX, mouseY, 10, 10) after cursor())

  • Hi

    It's curious because I only changed that line and it worked fine here, no mistake appears.

    cursor(scope, mouseX, mouseY);
    for 
    cursor(scope);
    
  • HI

    I remember that I also changed the size of scope to 16x16, or it can vary to 32x32, that's all

  • @Iaimperiestro So cursor(img) worked for you?

  • Hello

    Yes, of course, for that reason I gave that new modification, it really worked, no more changes were necessary for me, and besides cursor should have 16x16 pixels...

  • So I guess the size of scope may be the problem.

Sign In or Register to comment.