We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › beginner questions
Page Index Toggle Pages: 1
beginner questions (Read 602 times)
beginner questions
Apr 22nd, 2006, 1:32pm
 
Hi, I am new to processing and programming in general. My only previous experience is with the functional programming language Haskell.

Ive figured out enough to create a ball that bounces around the window, and that accelerates toward the cursor upon mouse-click. Lets say I wanted to make the right mouse button create a new ball. The only way I can currently conceive to do this is to keep a list of particle positions, x velocities, and y velocities, and add to this upon right mouse click.

But there must be a less clumsy way to do it. Can functions actually create new global variables? As far as I know any variables declared within a function are only accessible to tha function.

Re: beginner questions
Reply #1 - Apr 22nd, 2006, 2:07pm
 
If you use arrays, you can either just add a new pair of positions/velocities to an existing array, or you could look into creating a class for your ball, and just add a new ball to an array of balls.
Re: beginner questions
Reply #2 - Apr 24th, 2006, 3:17pm
 
classes..reading the tutorial linked in the 'class' entry of 'Reference', and looking at some Processing examples..I think I pretty much get them. Just have to code up some experiments now. How do I destroy an object? Say a ball moves, and eventually exits the window. I dont like the idea of a bunch of invisible balls accumulating in memory.
Re: beginner questions
Reply #3 - Apr 24th, 2006, 4:05pm
 
You can't manually delete anything, but it'll happen automaticly.

If you remove the reference to it in the array, e.g. make the array shorter, assign a new ball to that part of it, or set it to null, then the ball will get cleand up by Java's garbage collector.
Re: beginner questions
Reply #4 - Apr 27th, 2006, 2:22pm
 
hi again, thanks for the replies.
Another question: Is it possible to make a function return two values of different types, eg a float and an array of ints?
Re: beginner questions
Reply #5 - Apr 27th, 2006, 3:01pm
 
not really in any efficient way.. but if it's a float and array of ints, then you can return the float, and pass in an array to be set by the function and that'll work:

Code:
public float myfunction(int[] outgoing) {
for (int i = 0; i < outgoing.length; i++) {
outgoing[i] = 45;
}
return 45.2;
}


note that you'll have to create the "outgoing" array beforehand.
Page Index Toggle Pages: 1