如何在AS3中动态添加图像?

时间:2012-12-02 16:01:21

标签: image actionscript-3 containers

好的,我想添加一系列图像,然后能够拖放它们中的每一个。我将所有图像嵌入到类图像中。 s0,s1,s2是图像类的实例。现在这就是我所做的

public function Main():void 
{
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE, init);

    function init(e:Event = null):void 
    {
        var a:Array = new Array(); 
        var imageContainer:Sprite = new Sprite;
        var imgClass:Images = new Images(); 

       for (var i:int = 0; i < 9; i++) {
            a[i].push(imgClass.(s+String(i)));
            imageContainer.addChild[a[i]]; 
        }
        stage.addChild(imageContainer);
        imageContainer.addEventListener(MouseEvent.MOUSE_UP, takeIt);
        imageContainer.addEventListener(MouseEvent.MOUSE_DOWN, dropIt);

        function takeIt(event:MouseEvent) {
            event.currentTarget.startDrag(); 
        }
        function dropIt(event:MouseEvent) {
            event.currentTarget.stopDrag(); 
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

我已经重写了一下你的代码,但我还是无法测试它。

我首先想到的是,init方法位于您的Main方法中。然后,您的takeItdropIt方法位于init内。我不确定这是否真的有效,但我已在下面的代码中解决了这个问题。

在我的代码中,我假设Images类中的图像实例是Bitmap的实例。这意味着在for循环内我必须将每个添加到Sprite,以便您可以访问startDragstopDrag。我在每个图像上侦听MOUSE_DOWN事件,并将图像设置为selectedImage var并执行startDrag。我也在MOUSE_UP上听stage,然后删除currentImage。

package 
{
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class  Main extends Sprite
    {
        private var selectedImage:Sprite;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var imageContainer:Sprite = new Sprite();
            var imgClass:Images = new Images(); 

            for (var i:int = 0; i < 9; i++) 
            {
                var imgInstance:Bitmap = imgClass['s' + i.toString()] as Bitmap;
                var imgSprite:Sprite = new Sprite();
                imgSprite.addChild(imgInstance); // Put image in a sprite so we can use startDrag on it.
                imageContainer.addChild(imgSprite);
                imgInstance.addEventListener(MouseEvent.MOUSE_DOWN, img_mouseDownHandler);
            }

            addChild(imageContainer);

            stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
        }

        private function dropSelectedImage():void 
        {
            if (selectedImage) 
            {
                selectedImage.stopDrag(); 
            }
        }

        private function img_mouseDownHandler(e:MouseEvent):void 
        {
            dropSelectedImage();
            selectedImage = e.currentTarget as Sprite;
            selectedImage.startDrag();
        }

        private function stage_mouseUpHandler(e:MouseEvent):void 
        {
            dropSelectedImage();
        }
    }
}

要在循环中构建图像,您也可以手动构建数组,然后循环遍历数组:

var images:Array = [imgClass.s0, imgClass.s1, imgClass.s2, imgClass.s3, imgClass.s4, imgClass.s5, imgClass.s6, imgClass.s7, imgClass.s8];
for each (var img:Bitmap in images) 
{
    var imgSprite:Sprite = new Sprite();
    imgSprite.addChild(img); // Put image in a sprite so we can use startDrag on it.
    imageContainer.addChild(imgSprite);
    imgInstance.addEventListener(MouseEvent.MOUSE_DOWN, img_mouseDownHandler);
}

希望这有帮助。请随意让我在评论中详细说明或解释其他任何内容。