We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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.
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 ofdraw()
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.
and
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).
;-)
also try hitting ctrl-t in the processing IDE - makes your code look good....
;-)
float y = 0.0;
.void points(float y) { //... }
!Also, you want to use
background(0);
at the beginning ofdraw()
to erase your canvas.Thanks a lot for a load of information I'll need to digest and apply...