TwelvestoneFlash

Clone a sprite


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
mystic_juju
 
2007-10-17

Anybody know how to clone a sprite in AS3? I want a duplicate, not a reference.

pyrogen
 
2007-10-17

if the extend sprite for your class object, you can just make a new instance of said class and add it in the display list.

mystic_juju
 
2007-10-18

don't think that would really work. I need to duplicate it once its done a bunch of things. Extending it will instantiate it as though its new. Here's the deal:

The class I want to clone is an ImageLoader class which extends Sprite. I instantiate a bunch of them and use them as rotated images in the background. But if i reference one of these, add it to the display list, and then remove it, it seems to disappear and I can't add it again. But if I can clone it, then it will add the clone to the display list, and once removed, the clone will disappear.

A workaround I've used is to read the bitmapdata of the ImageLoader, cast it into a Bitmap and add that to the display list instead. That works in this case, cause I can do that repeatedly, but it wouldn't work if the sprite was more complex than just a static image.

JLM
 
2007-10-19

just a random thought does it make a difference if you use a random string on the loading path?

Sounds like your described the wrong problem and are trying to solve the wrong problem, not using as3 in anger yet, I can't really advise on the whole clone thing.

However I am slightly lost 'ImageLoader class which extends Sprite' 'instantiate a bunch of them and use them as rotated images' .... surely it would make more sense to have a single image loader (maybe composite rather than sprite extends since it is not visual ) that passes sprites to the display list?

mystic_juju
 
2007-10-19

Thanks JLM. I am not sure what i was doing before before (it was at work), but anyway, this seems to have done the trick (very simplified). An array of image filenames is passed to this Background class.

package {

import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import fl.transitions.*;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

public class Background extends Sprite {

    private var _images:Array;
    private var _timer:Timer;
    private var _oldimage:ImageLoader;
    private var _newimage:ImageLoader;
    private var _atween1:Tween;
    private var _atween2:Tween;
    private var _currentimagenum:uint;

    public function Background(imgArr:Array) {

        _images = new Array();

        for (var i:uint = 0; i < imgArr.length; i++) {

            var img:ImageLoader = new ImageLoader(imgArr[i]);
            _images.push(img);

        }

        _timer = new Timer(2000);
        _timer.addEventListener(TimerEvent.TIMER, changeImage);
        _timer.start();

    }

    private function changeImage(e:TimerEvent = null) : void {

        do {
            var newImageNum:uint = Math.floor(Math.random() * _images.length);
        } while (newImageNum == _currentimagenum);

        _newimage = _images[newImageNum];
        addChild(_newimage);

        _atween1 = new Tween(_newimage, "alpha", Regular.easeOut, 0, 1, 1, true);
        _atween1.addEventListener(TweenEvent.MOTION_FINISH, swapImages);

        _currentimagenum = newImageNum;

    }

    private function swapImages(e:TweenEvent) : void {
        if (_oldimage != null && this.contains(_oldimage)) {
            removeChild(_oldimage);
        }

        _oldimage = _newimage;
        addChild(_oldimage);
    }

}

}

And the other class:

package {

import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;

public class ImageLoader extends Sprite {

    public function ImageLoader(imageStr:String) {

        var loader:Loader = new Loader(  );
        addChild( loader );
        loader.load( new URLRequest( imageStr ) );

    }

}

}

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

Clone a sprite