duplicating objects with easeljs

chrysillo picture chrysillo · Oct 11, 2012 · Viewed 7.4k times · Source

I keep having problems trying to reuse elements using the easeljs library. Whether I use the clone() method, where I can only get one instance and then events like onPress will stop working for the new elements.

Adding the same object on more than one container will make the object to disappear everywhere. I keep having to find workarounds around this, messing up my code and wasting resources.

Thanks in advance for any help or tip.

Answer

marcosbernal picture marcosbernal · Dec 15, 2013

if you are using Shape's Graphics, you might want to consider passing true as a parameter.

var boxClone = box.clone(true);

from CreateJs documentation: http://www.createjs.com/Docs/EaselJS/classes/Shape.html#method_clone

clone ( recursive )

Returns a clone of this Shape. Some properties that are specific to this instance's current context are reverted to their defaults (for example .parent).

Parameters: recursive

If true, this Shape's Graphics instance will also be cloned. If false, the Graphics instance will be shared with the new Shape.