Scope of Variables & Errors?

I am wondering about how exactly the scope of variables work. I am trying to actively process a ball that conforms to the mouse. Depending on where I put the variable "ballRadius" it changes how the program works. If I set the variable in the Draw section, it works just fine, but if it is a global variable, it does not(full white screen). I need it to be a global variable so that I can change its value later.

I know that if the variable is just in the Setup or Draw it will be constrained to the block, but I thought that if it is global, everything can access it. I would like to know how I can make "ballRadius" a global variable and still have it work.

If anyone could provide feedback, I would be thankful.

    float ballRadius = sqrt( ( ( mouseX - 250 ) * ( mouseX - 250) ) + ( ( mouseY - 250 ) * ( mouseY - 250 ) ) );

void setup(){
  size(500,500);

}

void draw(){
  background(0); //Black background
    float ballRadius = sqrt( ( ( mouseX - 250 ) * ( mouseX - 250) ) + ( ( mouseY - 250 ) * ( mouseY - 250 ) ) );

  ellipse((width/2),(height/2),ballRadius*2,ballRadius*2);
}
``

Answers

  • Answer ✓

    you need to distinguish between declaring a var and setting it

    before setup say float ballRadius; // declare

    in draw() don't repeat the word float because with it you declare a new var with scope draw that has the same name!!! then you'd have 2 vars. bad.

    just say ballRadius= ........ without float

  • Thank you very much.

  • to give it a mouse depending value in line 1 won't work, you need to do it in draw() to get always the current values

  • edited September 2015

    Thanks, ultimately I want the ellipse to conform to the mouse when expanding, but to shrink at a constant rate.

    My first thought was to have three variables, "ballRadius", "mouseRadius" and "finalRadius" I would find the distance from the centre point to the mouse using Pythagoras, which is what you helped me with. Than I would draw the ellipse to it, which is now working.

    Now the problem I am having is how to get the ellipse shrink. I was going to set "ballRadius = mouseRadius-shrink, than have use a max statement (finalRadius=max(ballRadius,mouseRadius) to draw the larger of the two ellipses. That way the ellipse would shrink at a constant rate, but would expand to the mouseRadius, but that isn't working for me.

    If you have any further insight, I would greatly appreciate it.

  • just mult by factor f and decrease that?

    from 1 in steps of 0.01 ?

  • also look at map() please

    you can map the mouse to a range which gets smaller

  • void draw(){
      background(0); //Black background
    //  float ballonRadius =ballRadius+1;
    
        mouseRadius = sqrt( ( ( mouseX - 250 ) * ( mouseX - 250) ) + ( ( mouseY - 250 ) * ( mouseY - 250 ) ) );
    
        shrink = shrink-.01;
    
        ballRadius = mouseRadius*shrink;
    
        finalRadius = max(ballRadius,mouseRadius);
    
      ellipse((width/2),(height/2),finalRadius*2,finalRadius*2);
    }
    

    Is this on the right track? It is giving me an ellipse drawn to the mouse. I have all of these declared at top, with shrink set as a value of 1 globally. The problem I see is that because the ballRadius uses mouseRadius, and mouseRadius updates every "frame", it will never decrease in value.

    Is there a way to keep the largest value of mouseRadius, so that ballRadius will actually do something?

    Thanks for your help, I have been working back to back 8 hour shifts, so my tired brain is having some trouble understanding.

  • edited September 2015

    Hey, try running this tell me if its what you're looking for:

    ` float mouseRadius, shrink, ballRadius, finalRadius;

    void setup(){
     size(500,500); 
    }
    void draw(){
      background(0); //Black background
    //  float ballonRadius =ballRadius+1;
    
        mouseRadius = sqrt( ( ( mouseX - 250 ) * ( mouseX - 250) ) + ( ( mouseY - 250 ) * ( mouseY - 250 ) ) );
    
        shrink = shrink-.01;
    
        ballRadius = mouseRadius*shrink;
    
        finalRadius = max(ballRadius,mouseRadius);
    
      ellipse((width/2),(height/2),finalRadius*2,finalRadius*2);
    } `
    
  • Thanks for the input, but this still has the problem that has been stumping me. The ellipse isn't shrinking at a constant rate. I can draw the ellipse to the mouse perfectly fine. I need it so that if the mouse goes quickly to the centre, the ellipse will still be big, but slowly shrink in radius. The main problem I think I have right now is that mouseRadius is constantly updating, so that ballRadius will not properly shrink.

    Thanks again for the help!

  • Well either it shrinks OR it goes with the mouse. You can't have both.

    Did you try TechWiz code? It's very good. He is very good.

    Did you try my map() idea? See reference : map()

  • Thanks for your help guys, I figured it out. I had to set the radius as a global variable, than have it overridden by then mouse radius.

Sign In or Register to comment.