Anybody know how to clone a sprite in AS3? I want a duplicate, not a reference.
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.
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.
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?
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 ) );
}
}
}