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 › Creating multiple instances of an object
Page Index Toggle Pages: 1
Creating multiple instances of an object (Read 613 times)
Creating multiple instances of an object
Jun 5th, 2008, 6:37pm
 
So this seems more confusing than it should be.

I'm learning OOP, and so I made an app with a Tank object. Each tank has a unique starting position on the map, orientation, speed, color, etc.

I built my app off of this demo:
http://processing.org/learning/basics/objects.html

See how in this demo, each instance is defined in the first line of the program? What if I don't want to do that? What if I want to have a new tank be created every time I click the mouse?
Re: Creating multiple instances of an object
Reply #1 - Jun 5th, 2008, 7:02pm
 
You can create an instance of a class at any point in your program. In the demo progam you cite, the first line just declares the objects. The instances are actually created in lines 4-7 of setup().

For your tank program, presumably you don't know in advance how many tank objects will be created (depends on how many times the user presses the mouse). You will therefore need to store the tank objects in some kind of dynamic collection that can grow as more objects are added. The easiest to use is probably the Java Vector. The code below provides a simple example.

Code:
Vector tanks;

void setup()
{
tanks = new Vector();
 size(400,400);
}

void draw()
{
 Iterator i = tanks.iterator();
 while (i.hasNext())
 {
   Tank tank = (Tank)i.next();
   tank.display();
 }
 
 if (mousePressed)
 {
   tanks.add(new Tank(random(10,width-10),random(10,height-10)));
 }
}


class Tank
{
 float x,y;    // Tank location.
 
 Tank(float x, float y)
 {
   this.x = x;
   this.y = y;
 }

 // Draws the tank at its current location.
 void display()
 {
   rect(x,y,10,10);
 }
}


Obviously, your tank class will be more sophisticated than mine, but I hope you get the idea.

Oh, and just because it might help in getting your head round OO concepts, you create instances of classes, not objects. An object is itself an instance of a class.

Re: Creating multiple instances of an object
Reply #2 - Jun 5th, 2008, 8:01pm
 
Since the use of Vector is deprecated, I would just suggest the use of ArrayList! The syntax is quite the same, so let's start with good habits Wink

ArrayList tanks;
...
tanks = new ArrayList();
...
ListIterator i = tanks.listIterator();
while (i.hasNext()) {
 ...
}
Re: Creating multiple instances of an object
Reply #3 - Jun 5th, 2008, 9:25pm
 
Actually, Vector is (no longer) deprecated. I recall having seen that too in the JavaDoc, but you won't find it there anymore.
That's because Vector is synchronized: it is slower, hence its usage is discouraged for simple cases, but it still have its usefulness...
So your advice is still useful! Wink
Likewise, it is advised to use HashMap instead of the old Hashtable.
Re: Creating multiple instances of an object
Reply #4 - Jun 6th, 2008, 11:17am
 
oops, my mistake, then. hope I haven't confuse zacharyr with all this...  Wink
Re: Creating multiple instances of an object
Reply #5 - Jun 8th, 2008, 7:49pm
 
Thank you people!

One thing: When I use the above code, either integrated into my program or pasted into a new sketch, I am getting some kind of error I don't understand.

java.io.FileNotFoundException: /tmp/build30636.tmp/Temporary_70010_8760.java (No such file or directory)

Ideas?
Re: Creating multiple instances of an object
Reply #6 - Jun 9th, 2008, 3:27pm
 
Strange... By the look of the path, I will suppose you are on an Unix system, like Linux or MacOS X.
AFAIK, /tmp directory is always writable by everybody otherwise lot of programs would broke. But just in case, check the rights on this folder.
As you might know, PDE is doing lot of things behind your back when you hit the Run button: it takes all the .pde files in the sketch folder and merge them in a .java file (Temporary_70010_8760.java in your example, it changes on each run) with a bit of mandatory additional code to make it a valid Java program (and changing a bit the code, eg. to transform #AABBCC to 0xFFAABBCC), and copies this .java (and the other .java files that might be in the sketch folder) to a temporary folder (the build30636.tmp in your example, it also changes on each run) and compiles there.
The message you report looks like you have not the rights (or disk space, but it is unlikely...) to create a folder or a file there.

You can also edit your preferences.txt file (with PDE closed) and set preproc.save_build_files to true, to see if the files are there.
Page Index Toggle Pages: 1