Can I pass a Sprite object as a parameter for a new DisplayObjectClass?

16 views
Skip to first unread message

Martin

unread,
Sep 2, 2010, 4:24:49 PM9/2/10
to Stardust Particle Engine
Hello there,

Some of my code looks like this:


private var star:Sprite;

public function drawStar() {
star = new Sprite();
star.graphics.beginFill(0xFFFFFF, 1.0);
star.graphics.drawCircle(0,0,0.5);
star.graphics.endFill();

}

When I try to pass this star variable:

drawStar();
emitter.addInitializer(new DisplayObjectClass(star));

I can't. I get an error 1067: Implicit coercion of a value of type
flash.display:Sprite to an unrelated type Class.

Is there anyway I can pass this star object to the
DisplayObjectConstructor?

Thanks in advance.

Allen Chou

unread,
Sep 3, 2010, 12:46:53 AM9/3/10
to stardust-par...@googlegroups.com
You must provide a reference to the display object class you want to
instantiate as particles, so you cannot simply pass an instance of
your display object to the DisplayObjectClass constructor.

You may refactor your code in the drawStar() function into a Sprite
subclass, like this:

public class Star extends Sprite {
    public function Star() {
        graphics.beginFill(0xFFFFFF, 1.0);
        graphics.drawCircle(0,0,0.5);
        graphics.endFill();
   }
}

And you can pass a reference of this Star class to the
DisplayObjectClass constructor, like this:

emitter.addInitializer(new DisplayObjectClass(Star));

Don't instantiate the display object class yourself. The
DisplayObjectClass initializer instantiates your class for you.

--
http://cjcat.blogspot.com

Martin

unread,
Sep 3, 2010, 9:54:04 AM9/3/10
to Stardust Particle Engine
Oh, thank you, I have realized that earlier. I needed to pass a class
not an instance.
I have another question. I'm creating a scrolling starfield and so far
I can't manage to apply random size and velocity to the stars. They
all have the same speed and the same size. I tried to use the
lazySectorZone for the velocity, but it makes the stars to fly in any
direction. I need the stars to fly in a vertical straight line. Do you
have any suggestions on what should I need to do to apply random
velocity and size to each particle (in this case, each star)?

Thank you Allen.

Allen Chou

unread,
Sep 3, 2010, 10:42:56 AM9/3/10
to stardust-par...@googlegroups.com
Just keep the LazySectorZone.directionVar property to zero, and you'll
have all the particles moving in the same direction. Alternatively,
you may extend the Zone class and override the calculateMotionData2D()
method, making it return MotionData2D located on the same straight
line segment.

Martin

unread,
Sep 3, 2010, 11:11:32 AM9/3/10
to Stardust Particle Engine
Thanks, that solves the velocity problem, now how do I make each
particle with random size?

Thank you Allen for your time.

Allen Chou

unread,
Sep 3, 2010, 11:13:35 AM9/3/10
to stardust-par...@googlegroups.com
You can use the Scale initializer to randomize the particle scale. The
DisplayObjectRenderer adjusts the display objects' scale based on the
particles' scale.

Martin

unread,
Sep 3, 2010, 11:19:23 AM9/3/10
to Stardust Particle Engine
Oh, yeah from the common library. I was searching in the twoD and
didn't find anything. Thank you, I think thats all for the moment.
Reply all
Reply to author
Forward
0 new messages