Welcome, Guest
Want to take part in these discussions? Sign in if you have an account, or apply for one below
-
- CommentAuthorOwen Bennett
- CommentTimeJan 16th 2009
Is there currently a way to clone a particle? I've implemented my own quick and dirty solution using a helper class with a couple of static methods, but this has some speed implications, especially since most of the properties have to be accessed through getter/setters. Looking at the description of the Particle, it seems that only the image property is not a primitive type, so I have excluded that and the sortID from the mirroring. Of course, this method will not clone private or protected properties so that's another reason to have it within the Particle itself.
Here's the code I'm using:
// creates a clone of the specified particle, attaches it to the emitter and then returns the new particle
public static function cloneParticle(emitter:Emitter, particle:Particle):Particle
{
var p2:Particle = emitter.particleFactory.createParticle();
emitter.addExistingParticles([p2], true);
mirrorParticle(particle, p2);
return p2;
}
// copies all of the properties of particle1 to particle2
public static function mirrorParticle(particle1:Particle, particle2:Particle, excludedProperties:Array = null):void
{
if (excludedProperties == null) excludedProperties = ["sortID", "image"];
else excludedProperties.push("sortID", "image");
var description:XML = describeType(particle2);
var varList:XMLList = description..variable;
for each (var item:XML in varList)
{
if (excludedProperties.indexOf(String(item.@name)) != -1) continue;
var prop:Object = particle1[item.@name];
if (prop is Number || prop is String || prop is Boolean) particle2[item.@name] = prop;
}
} -
- CommentAuthorRichard
- CommentTimeJan 18th 2009
Now you mention it, it seems so obvious. So I've added a clone method to the Particle class. This clones the image property along with everything else - if particles are used with a BitmapRenderer the image class can safely be shared so it makes sense to copy it, although it will cause problems when used in a DisplayObjectRenderer unless the user creates a new image for the particle after cloning it. -
- CommentAuthorOwen Bennett
- CommentTimeJan 19th 2009
Thanks for adding this Richard. The thing with the DisplayObjectRenderer image is a bit of a problem - they way I got around it was to let the emitter create the image when adding the particle. Clearly this isn't really a "clone" as it could potentially not create an exact image copy. After a bit of thought, I realised that the only real way to do it would be to have a clone method for every DisplayObject initialiser (Dot etc.) with an interface that allowed it to be checked. Something like:
if (image is IClonable) p.image = image.clone();
else p.image = image;
Obviously this may be a lot of work to implement...
Other than that, how would it be possible to create a copy of a DO image? I can't think of a neat way to do it that doesn't require knowledge of the parameters that were put into the emitter initialiser. -
- CommentAuthorRichard
- CommentTimeJan 20th 2009 edited
Unfortunately, people use other display objects too - display objects created in flash for example - so it's not possible to expect a clone method for images, although it's theoretically possible with one's own classes. The other problem is that, when using a bitmap renderer, we don't want to clone the image, just copy the reference - cloning would be slower and require more memory.
I did some very extensive tests to try to find a way to clone a display object, but even the ByteArray method doesn't work with display objects. So it seems there isn't a way, although it may be possible with reflection and a lot of code.
1 to 4 of 4
