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 › Arrays and Instantiation
Page Index Toggle Pages: 1
Arrays and Instantiation (Read 440 times)
Arrays and Instantiation
Feb 6th, 2010, 3:56pm
 
Hi all,

I am facing a problem here.
I am working at a project where i need to instantiate some objects dynamically, and to keep every thing organized, insert them in an array.

Here is my simplified code:

Code:

Plant[] myPlants = new Plant[1];

void setup(){
    size(200,200);
    PVector loc = new PVector(100,100);
    myPlants[0] = new Plant(loc, 1000, 10, 1);
}

void draw(){
    for(int i=0 ; i<plants.length ; i++){
         if (myPlants[i] != null) myPlants[i].live();
    }
}


well...
To solve the problem I've created a function in the main file, that is been called by the myPlants's instances, and inside this function... I was trying to use the function apped(), but it's not working:

Code:

void instantiateNewPlant(PVector p, int temp, float ener, float forc){
    Plant plt = new Plant(p, temp, ener, forc);
    myPlants = append(myPlants, plt);
}


... and I am receiving this error: "cannot convert from Object to ecologia.Plant[]"

Does someone know why? Does the append() function works with non native objects?

Thanks guys!

Paulo
Re: Arrays and Instantiation
Reply #1 - Feb 6th, 2010, 5:13pm
 
The append() documentation includes the following information:

Quote:
When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) append(originalArray, element).


So you will need something like...

Code:
void instantiateNewPlant(PVector p, int temp, float ener, float forc){
    Plant plt = new Plant(p, temp, ener, forc);
    myPlants = (Plant[]) append(myPlants, plt);
}
Page Index Toggle Pages: 1