Function and motion

I need to use a function in an animation program, but can't get it to work. My problem can be related to a very simple basic example taken from the book by Casey Reas and Ben Fry (Processing, page 174):

void setup() { size (100,100); smooth(); }

float y=0.0;

void draw () { frameRate (20); line(0,y,100,y); y=y+0.5;

}

When I try to do the same thing with a function with a single parameter, I only get the line once, not shifting:

void setup() { size (100, 100); smooth(); }

void draw () { frameRate(15); points (0.0); }

void points(float y) { line(0, y, 100, y); y=y+0.5; }

What am I doing wrong?

Answers

  • first off, learn to format your code when posting on the forum, it'll help everybody and give us line numbers we can use in the reply.

    your specific problem is that you're drawing the line with the exact same value every time - points(0.0) and yet are expecting it to change. the y = y + .05 changes the value of y local to the points() method but then that gets de-scoped immediately afterwards so it's useless.

  • Answer ✓

    Please edit your post and fix your formatting by highlighting your code and pressing the code button (it looks like a C).

    Note that changing the y value passed into the points() function does not change the value anywhere else. The next time you call the points() function, you pass 0.0 into it again, so it never changes.

  • edited May 2015 Answer ✓

    Hello,

    it is good that you are using a function (points) in the first place - a lot of people just put everything into draw().... that leads to a mess...

    Each variable (like y) has a scope / an area it is valid in. A change would only affect its current scope. Therefore you could have one y for a player position and one for an enemy position and they wouldn't interfere (if done right).

    Anyway for your problem, try to make a second var y with a global scope. That means, define it before setup(). Increase it in draw(). When you pass it to points be aware that it has the same name there but is in fact another var with the same name (and a local scope, restricted to the function).

    You need to write the code so that the changes to y are persistent and just temporarily. A change to a var with a local scope is just temporarily.

    Well, it's hard to explain really.

    Also, you want to use background(0); at the beginning of draw() to erase your canvas.

    ;-)

  • Thanks for the answer to my question. It really should have been: where else can I put the increment so that it does affect the next pass ? Thanks also for giving me the procedure for fixing the formatting on the forum.

    void setup() { size (100,100); smooth(); }
    
    float y=0.0;
    
    void draw () { frameRate (20); line(0,y,100,y); y=y+0.5;
    
    }
    

    and

    void setup() { size (100, 100); smooth(); }
    
    void draw () { frameRate(15); points (0.0); }
    
    void points(float y) { line(0, y, 100, y); y=y+0.5; }
    
  • edited May 2015

    well, no....

    what would be nice would be to find a version with a global y and a function "points" which gets this y as a parameter.

    also, mostly, we write global vars before setup() not after it (since they are valid in setup() already).

    ;-)

  • edited May 2015

    also try hitting ctrl-t in the processing IDE - makes your code look good....

    ;-)

  • edited May 2015 Answer ✓
    • In Java, variables can be classified as global (a.K.a fields) or local.
    • Variables become fields when they're declared outside of any functions.
    • However, field variables can be re-declared as local variables when using the same name.
    • When that happens, it becomes overshadowed until the end of that local scope.
    • That's exactly what you do w/ the field y.
    • You declare it as a field in float y = 0.0;.
    • However, it is re-declared here: void points(float y) { //... }!
    • That is, inside function points(), y is a local variable, not the global 1! :-&
  • Also, you want to use background(0); at the beginning of draw() to erase your canvas.

  • Thanks a lot for a load of information I'll need to digest and apply...

Sign In or Register to comment.