1

每当我将图像拖放到垃圾桶中时,我都需要从舞台上删除图像。我一直在看这个,似乎真的找不到解决方案。

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[


        import mx.controls.Image;

        //array of movies
        public var myArray:Array = ["batman.jpg","cabin.jpg","christmasVacation.jpg"];

        //init function
        public function init():void{

            var trashImage:Image = new Image();

            //set trash img
            trashImage.source = "asset/trash.jpg";

            //adds img to stage
            trashGrp.addElement(trashImage);

            //loops 4 times
            for(var i:Number = 0; i < myArray.length; i++){

                var arrayEntry:String = myArray[i];

                //makes new image
                var newImage:Image = new Image();

                //sets the image source for each image
                newImage.source = "asset/" + arrayEntry;

                //adds the image to the stage
                grpMovies.addElement(newImage);

                //set drag/drop
                newImage.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
                newImage.addEventListener(MouseEvent.MOUSE_UP, dropMe); 
            }
        }

        //dragMe function
        public function dragMe(e:MouseEvent):void
        {
            // Get the Image that saw the mouse-down.
            var img:Image = Image(e.currentTarget);
            // Use the built-in method to start dragging.
            img.startDrag();
        }

        //dropMe function
        public function dropMe(e:MouseEvent):void
        {
            // Get the Image that saw the mouse-up.
            var img:Image = Image(e.currentTarget);
            // Use the built-in method to stop dragging.
            img.stopDrag();

                            -**this is where my issue is. I cant get it to remove it when the image is hit**

            if (trashGrp.hitTestObject(img)) {
                grpMovies.removeElement(img);
            }

        }


    ]]>
</fx:Script>
<s:TileGroup id="grpMovies" columnWidth="225" rowHeight="225" requestedRowCount="1"
             paddingTop="20"/>
<s:HGroup id= "trashGrp" x="950" y="20"/>


</s:Application>
4

1 回答 1

1

查看以下代码,我认为它可以满足您的需求。我修改了事件处理程序以附加到主容器,以便在您释放垃圾桶上的鼠标按钮时正确触发它。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
           xmlns:mx="library://ns.adobe.com/flex/mx"
           creationComplete="init()">

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
<![CDATA[


    import mx.controls.Image;

    //array of movies
    public var myArray:Array = ["batman.jpg","cabin.jpg","christmasVacation.jpg"];
    private var _draggedImage:Image;

    //init function
    public function init():void{

        var trashImage:Image = new Image();

        //set trash img
        trashImage.source = "assets/trash.png";
        trashImage.mouseEnabled = false;
        trashImage.mouseChildren = false;
        //adds img to stage
        trashGrp.addElement(trashImage);

        //loops 4 times
        for(var i:Number = 0; i < myArray.length; i++){

            var arrayEntry:String = myArray[i];

            //makes new image
            var newImage:Image = new Image();

            //sets the image source for each image
            newImage.source = "assets/"+arrayEntry;

            //adds the image to the stage
            grpMovies.addElement(newImage);

            //set drag/drop
            newImage.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);

        }
    }

    //dragMe function
    public function dragMe(e:MouseEvent):void
    {
        // Get the Image that saw the mouse-down.
        var img:Image = Image(e.currentTarget);
        // Use the built-in method to start dragging.
        img.startDrag();
        _draggedImage = img;
        this.addEventListener(MouseEvent.MOUSE_UP, dropMe); 
    }

    //dropMe function
    public function dropMe(e:MouseEvent):void
    {
        _draggedImage.stopDrag();
        this.removeEventListener(MouseEvent.MOUSE_UP, dropMe);


        if (trashGrp.hitTestObject(_draggedImage)) {
            grpMovies.removeElement(_draggedImage);
            _draggedImage = null;
        }

    }


]]>
</fx:Script>
<s:TileGroup id="grpMovies" columnWidth="225" rowHeight="225" requestedRowCount="1"
            paddingTop="20"/>
<s:HGroup id= "trashGrp" x="950" y="20"/>

</s:Application>
于 2012-11-02T04:15:37.067 回答