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.
Page Index Toggle Pages: 1
Array problems (Read 489 times)
Array problems
Dec 11th, 2009, 3:43pm
 
Code:
class Particle
{
 float x, y;
 float speedx, speedy;
 float speed;
 float mass;
 color c;
 Particle(float tempx, float tempy, float tempspeed, float tempmass)
 {
   x = tempx;
   y = tempy;
   speed = tempspeed;
   mass = tempmass;
   c = color(random(225), random(225), random(225));
 }
 void display()
 {
   fill(c);
   noStroke();
   ellipse(x, y, mass, mass);
 }
 void move()
 {
   speedx = speedx+((random(1)-0.5)*speed);
   speedy = speedy+((random(1)-0.5)*speed);
   x = x + speedx;
   y = y + speedy;
 }
}
Particle[] particles;
void setup()
{
 size(200, 200);
 particles = new Particle[0];
}
void draw()
{
 background(0);
 particles = append(particles, new Particle(mouseX, mouseY, 1, 20));
 for(int i = 0; i < particles.length; i++)
 {
   particles[i].display();
   particles[i].move();
 }
}

I'm getting an error with the line that uses the append function.
What am I doing wrong here? The error I'm getting:
Code:

Cannot convert from Object to sketch_dec11a.Particle[]

It doesn't matter whether I use append or splice.
I want to make an array of objects that gets bigger every frame, so that I can access the functions in them that make them move and display. If there's a more efficient way to do all this, I'd appreciate knowing it.
Re: Array problems
Reply #1 - Dec 12th, 2009, 12:26am
 
You have to explicitly cast the result of append() method.

Code:
particles = (Particle[]) append(particles, new Particle(mouseX, mouseY, 1, 20)); 


Page Index Toggle Pages: 1