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 › is classes the right kind of thing to use
Page Index Toggle Pages: 1
is classes the right kind of thing to use? (Read 309 times)
is classes the right kind of thing to use?
Oct 24th, 2008, 10:00am
 
I'm trying to make a game which involves creating missiles which are created at the top of the screen and travel slowly to the bottom of the screen...now that's not the problem, the problem is i want to be able to have as many of these missiles on the screen as i want, and i don't know how to go about this. does anybody have any suggestions on what to do?
Re: is classes the right kind of thing to use?
Reply #1 - Oct 25th, 2008, 4:39pm
 
Yep, classes are the way to go.  You'd need something like this:
/*
** pseudo-code only.
*/

Missile missile1;

void setup(){
 missile1 = new Missile();
}

void draw(){
 missile1.draw();
}

class Missile{

 int yPosition=0;

 Missile(){
   //initializing stuff...maybe set the x position?
 }
 void draw(){
   yPosition++;
   //draw the actual missile
 }
}


Hope that helps.
Re: is classes the right kind of thing to use?
Reply #2 - Oct 25th, 2008, 11:49pm
 
i think the bigger problem was the 'i want to be able to have as many of these missiles on the screen as i want' which is the job for the expandable java ArrayList

List missiles = new ArrayList();

then add missiles using
missiles.add(new Missile());

the fiddly thing is to draw the list backwards

for (int i = missiles.size - 1 ; i >= 0 ; i--) {
 missiles.get(i).draw();
}

so that the draw method can remove individual missiles with missiles.remove(i); (when they explode, say, or leave the screen) without affecting the indexes of the missiles yet to process.

there will, obviously, still be limits to the number of missiles on the screen because they will all take a finite time to move and draw...

http://www.anyexample.com/programming/java/java_arraylist_example.xml
Re: is classes the right kind of thing to use?
Reply #3 - Oct 26th, 2008, 3:03am
 
I was thinking of just using the class to make as many as you wanted...but that works too.  Probably better actually from the looks of that remove() method
Re: is classes the right kind of thing to use?
Reply #4 - Oct 27th, 2008, 10:30pm
 
thank you for the suggestions, i'll check the syntax for these commands
Page Index Toggle Pages: 1