nothing happens - mouseX problem?

edited June 2015 in Questions about Code

Hi, I'm rather surprised that for some reason I can't get some examples in Dan Shiffman's Processing book to work. As an example this works:

void draw(){ background(204); line(mouseX, 20, mouseY, 80); }

however, this doesn't:

if (mouseX < width/2) { background (255); } else { background (0); }

Can anyone explain what's happening (or not). I did some of these examples a year ago and I'm sure they worked then???

Maybe I need to debug, but I don't know how (as yet) being a newbie.

Thanks in advance - Joe

Answers

  • See How to format code and text (beware of order of operations).

    Show a full sketch, perhaps something gets in the way.

    Also tell us which version of Processing you are using.

  • I agree with PhiLho :)

    We don't want to discourage the newbies to post a question in this forum unlike stackoverflow which is for advance programmers. We are really happy to help to new processing users and we want them to learn processing and catch up with the other users. :)

    So we just demand some extra effort from you to format your code while posting. If you are not sure how to do this : Here is a video:

    Moreover, I agree with PhiLho that posting full code will help us figure out the problem quickly.

  • edited June 2015

    Thanks for your suggestion, I did indeed try ctrl+k for formatting, but nothing happens, it just wipes the code (or text)! Any other suggestions?

    As for posting the whole code, I have. I've copied the code from Dan Shiffman's book texto. Is something missing from his book's explanation?

    As for the version I'm still on 2.2.1, maybe I should update it?

    Thanks in advance.

    p.s. I should add that I tried the auto format but nothing changes.

  • Just to clarify, if you put a code like this in your PDE and run a sketch, the background doesn't change when you move your mouse from left side to right?

    void setup(){
    size(500,500);
    } 
    
    void draw(){
    if (mouseX < width/2) {
      background (255);
      } else {
        background (0);
      }
    }
    
  • edited June 2015

    Hi Ater

    I think you've just sorted out the misunderstanding - in the book. I hadn't added the

    void setup()

    or

    void draw()

    i guess this makes the problem. What's strange is there seems to be know explanation in the book to do this, maybe I misread it!

    However, when I try the same thing with this code, also from the book, nothing happens!

    void draw() {
     if (mouseX < width/2) {
     fill(255);
     rect (0, 0, width/2, height);
     }
    }
    

    Any suggestions?

  • IIRC setup() must run before width and height have any value, so omitting it is a problem. I don't know the book, but maybe there's an early section that states the requirements fit setup and it's not included in all code examples to keep things short?

  • Great, I'll look into that and get back if it doesn't solve the problem, thanks.

  • blindfish, without setup(), width and height just get the default size of the sketch.

    joesh, the problem with your code is that mouseX is initially zero, so the condition is true, and the rectangle is drawn. But on next draw() call, there is no background() call that would clear the sketch, so the previous rectangle remains drawn...

    Just add background(155); at the first line of draw(), to see the effect.

  • Cool, thanks, I'll try all that tomorrow.

    Unfortunately I guess I must have misunderstood the book (and code) of Dan Shiffman, a reputable Processing man if ever there was!

Sign In or Register to comment.