0

好的,我想添加一系列图像,然后能够拖放每个图像。我将所有图像都嵌入到类图像中。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(); 
        }
    }
} 
4

1 回答 1

0

我已经稍微改写了你的代码,但我无法测试它抱歉。

我注意到的第一个想法是您的init方法在您的Main方法中。然后你的takeItdropIt方法在里面init。我不确定这是否真的有效,但我已经在下面的代码中修复了它。

在我的代码中,我假设您在Images类中拥有的图像实例是Bitmap. 这意味着在for循环中我必须将每个都添加到 aSprite以便您可以访问startDragand stopDrag。我在每个图像上监听MOUSE_DOWN事件,并将图像设置为selectedImagevar 并执行 startDrag。我还听MOUSE_UPon the 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);
}

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

于 2012-12-02T23:05:11.427 回答