PDA

View Full Version : How to clone a sprite?



LesJ
31 Mar 2012, 9:53 AM
I'd like to redraw a sprite on a different surface. What what be the easiest way to do this?

chhh
31 Mar 2012, 11:28 AM
why don't you just Ext.draw.Surface.add() it to your new surface instance?
I have just recently looked in the code, and add() method accepts not only a config object, but can also accept Ext.draw.Sprite instances (even a list of instances).
I don't really know why isn't that documented...

LesJ
31 Mar 2012, 1:08 PM
why don't you just Ext.draw.Surface.add() it to your new surface instance?
I have just recently looked in the code, and add() method accepts not only a config object, but can also accept Ext.draw.Sprite instances (even a list of instances).
I don't really know why isn't that documented...

Cool, I'll check and report if it worked. I think there's a bad typo in the doc: of should be or.


* @param {Ext.draw.Sprite[]/Ext.draw.Sprite...} args One or more Sprite objects of configs.
* @return {Ext.draw.Sprite[]/Ext.draw.Sprite} The sprites added.
*/
add: function() {
....

LesJ
1 Apr 2012, 9:43 AM
why don't you just Ext.draw.Surface.add() it to your new surface instance?
I have just recently looked in the code, and add() method accepts not only a config object, but can also accept Ext.draw.Sprite instances (even a list of instances).
I don't really know why isn't that documented...

I checked the code some more.

Yes, you can pass either a sprite or a config (var args are also accepted). I don't think you can pass a sprite that was already added to the surface and then expect it to be copied to another surface. It's OK though to pass a newly created sprite that wasn't yet added to any surface.

Looking at the code... it could be slightly simplified, see below. If we pass a sprite, then item will have the isSprite property, so there's no need to use instanceof.


Ext.define('Ext.draw.Surface', {
....
// @private
prepareItems: function(items, applyDefaults) {
items = [].concat(items);
// Make sure defaults are applied and item is initialized
var item, i, ln;
for (i = 0, ln = items.length; i < ln; i++) {
item = items[i];
// if (!(item instanceof Ext.draw.Sprite)) { // <-- Current
if (!item.isSprite) { // <-- Modified
// Temporary, just take in configs...
item.surface = this;
items[i] = this.createItem(item);
} else {
item.surface = this;
}
}
return items;
},
...

chhh
1 Apr 2012, 2:08 PM
1) oh yeah, I didn't check if the surface property of the sprite was changed. But i'm not quite sure if this property plays a crucial role in the life of a Sprite. Might be just a convenience property.. But of course your code will have to take that into account, if you're using it anywhere, or maybe ExtJS is using it itself... who knows

2) you could probably Ext.clone(your_sprite_instance) and then add the new copy of you Sprite to a new surface, then the surface property will get overwritten.
3)
// if (!(item instanceof Ext.draw.Sprite)) { // <-- Current
if (!item.isSprite) { // <-- Modified
i don't see the difference here, except checking for instanceof seems more robust.