Add object several times
I'd like to add object img
in 4 different containers cont1
,cont2
...
But img
was added only in the last cont4
container. How can I add that object in all containers?
var img:Image = ImageBuffer.imageStorage.shift();
var ti:TargetImage = new TargetImage();
for (var i:int = 1; i <= 4; i++)
{
MovieClip(ti.getChildByName("cont"+i)).addChild(img);
}
Solved. This function solved that problem
public static function duplicateImageAsSprite(original:DisplayObject):Sprite {
var bitmapData:BitmapData = new BitmapData(original.width, original.height, true, 0x000000);
bitmapData.draw(original as IBitmapDrawable);
var bitmap:Bitmap = new Bitmap(bitmapData);
var returnSprite:Sprite = new Sprite();
returnSprite.addChild(bi开发者_运维问答tmap as DisplayObject);
return returnSprite;
}
Put the new TargetImage() inside the for-loop like this:
var img:Image = ImageBuffer.imageStorage.shift();
for (var i:int = 1; i <= 4; i++)
{
var ti:TargetImage = new TargetImage();
MovieClip(ti.getChildByName("cont"+i)).addChild(img);
}
This will produce four different objects.. if you want to adress them you can put them in an array dynamically, also inside the for loop.
精彩评论