Results 1 to 5 of 5

Thread: How to clone a sprite?

  1. #1
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,425
    Answers
    20
    Vote Rating
    685
      -1  

    Default How to clone a sprite?

    I'd like to redraw a sprite on a different surface. What what be the easiest way to do this?

  2. #2
    Sencha User
    Join Date
    Mar 2012
    Posts
    3
    Vote Rating
    0
      0  

    Default

    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...

  3. #3
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,425
    Answers
    20
    Vote Rating
    685
      -2  

    Default

    Quote Originally Posted by chhh View Post
    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.

    Code:
         * @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() {
    ....

  4. #4
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,425
    Answers
    20
    Vote Rating
    685
      -2  

    Default

    Quote Originally Posted by chhh View Post
    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.

    Code:
    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;
        },
        ...

  5. #5
    Sencha User
    Join Date
    Mar 2012
    Posts
    3
    Vote Rating
    0
      0  

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •