1

我正在尝试将 5 张卡相互重叠 30%。并尝试使用鼠标事件对其进行移动。它应该只落在 5 张牌之内,而不是在那之外。我已经学习了拖放事件并执行了它,但我无法将卡片放在 5 张卡片中。请有人帮助我。请检查以下代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"     creationComplete="carcan();">
<mx:Script>
<![CDATA[

        import mx.containers.Canvas;
        import mx.controls.Image;

        private var images:Array;
        private var images1:Array;
        private const IMAGE_COUNT:uint = 5;
        private var img:Image;
        private var img1:Image;


        private var points:flash.geom.Point;
        private var offset_x:int;
        private var offset_y:int;
        private var canal:Canvas;
        private var doDrag:Boolean;

        [Embed(source='cards/2C.png')]
        private var Image0:Class;

        [Embed(source='cards/2D.png')]
        private var Image1:Class;

        [Embed(source='cards/2H.png')]
        private var Image2:Class;

        [Embed(source='cards/2S.png')]
        private var Image3:Class;

        [Embed(source='cards/3C.png')]
         private var Image4:Class;

         public function carcan():void
         {
            canal = new Canvas();
            canal.setStyle("backgroundColor","blue");
            canal.x=100;
            canal.y=50;
            canal.width=500;
            canal.height=400;
            this.addChild(canal);
            init();
         }

         public function init():void
         {
          images = new Array(IMAGE_COUNT);
             for (var i:int = 0; i < IMAGE_COUNT; i++)
                {
                     img= new Image();
                     img1= new Image();
                        images[i] = this["Image" + i];
                        trace(images[i]);

                img.x=(i*30)+50;
                img.source=images[i];
                img.id="Image"+i;
                canal.addChild(img);


                 img.addEventListener(MouseEvent.MOUSE_DOWN, md);
                img.addEventListener(MouseEvent.MOUSE_MOVE, mm);
                canal.addEventListener(MouseEvent.MOUSE_OUT,smu);
                img.addEventListener(MouseEvent.MOUSE_UP, mu);

                                        }

         }
         public function smu(event:MouseEvent):void
         {
            img.alpha=1;
            img.stopDrag();
            doDrag=false;
            setCards(); 

         }

         public function mo(event:MouseEvent):void
         {

            if(doDrag==true)
            {

            img.addEventListener(MouseEvent.MOUSE_DOWN, md);
            img.addEventListener(MouseEvent.MOUSE_UP, mu);
            img.stopDrag();
            img.alpha=1;
            img.removeEventListener(MouseEvent.MOUSE_MOVE, mm);
            }
            else
            {
            img.addEventListener(MouseEvent.MOUSE_MOVE, mm);
            }




         }                 

         public function md(event:MouseEvent):void
        {

        img = new Image();
            doDrag=true;

              canal.setChildIndex(Image(event.target),images.length-1);
             img.addEventListener(MouseEvent.MOUSE_MOVE, mm);



        }
        public function mm(event:MouseEvent):void
        {


             if(doDrag==true)
             {
                points = new Point();
            images = new Array(IMAGE_COUNT);


            img = new Image();
            img = Image(event.target);

            points.x=event.target.x;
            points.y=event.target.y;
            points = localToGlobal(points);
            img.x=points.x;
            img.y=points.y;
             img.alpha=0.7;
            img.addEventListener(MouseEvent.MOUSE_UP, mu);                  
            var boundar:flash.geom.Rectangle = new Rectangle(this.x, this.y, 250, 100);

             }
        }


        public function mu(event:MouseEvent):void
        {

            img.alpha=1;
            canal.stopDrag();
            doDrag=false;

            canal.stopDrag();
            doDrag=false;
            var current:Image = event.currentTarget as Image;
            var num1:int = current.x;                   

    if(num1 < 50){

         canal.setChildIndex(current, images.length-5);
         current.y=0;
         setCards();                
        }
    if(num1 > 50 && num1 < 80){

         canal.setChildIndex(current, images.length-4);
         current.y=0;
         setCards();                
        }
    if(num1 > 80 && num1 < 110){

    canal.setChildIndex(current, images.length-3);
    current.y=0;
        setCards(); 
        }
    if(num1 > 110 && num1 < 140){
        canal.setChildIndex(current, images.length-2);

         current.y=0;
        setCards(); 
        }
    if(num1 > 140 && num1 < 170){
        canal.setChildIndex(current, images.length-2);

         current.y=0;
        setCards(); 
        }
    if(num1 > 170){

        canal.setChildIndex(current, images.length-1);
        current.y=0;
        setCards(); 
        }



        }
        private function setCards():void{
        var b:int = 0;
        var a:int;
        var cardsArray:Array = canal.getChildren();
        for(a = 0;a < cardsArray.length; a++)
        {
            canal.getChildAt(a).x = 50+b;
            b=b+30;
            canal.getChildAt(a).y=0;
        }
         }

    ]]>
</mx:Script>        
</mx:Application>

PS:我正在尝试用鼠标事件替换拖放事件,并使用鼠标事件获得相同的功能。请有人帮助我。

4

1 回答 1

0

希望下面的代码可以帮助您: - 您可以按照下面的代码使用拖放并在 AS 中创建它。我已经在 MXML 中创建了它,它可以让您了解您正在寻找的内容:-

<?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" minWidth="955" minHeight="600" xmlns:local="*">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.core.DragSource;
            import mx.core.UIComponent;
            import mx.events.DragEvent;
            import mx.managers.DragManager;

            private function doDragEnter(event:DragEvent):void
            {
                DragManager.acceptDragDrop(UIComponent(event.target));
            }

            private function doDragDrop(event:DragEvent):void
            {
                var img:RummyItemRenderer;
                if (event.dragInitiator.parent == mainCanvas)
                {
                    img = event.dragInitiator as RummyItemRenderer;

                    //mainCanvas.swapChildren(img, event.currentTarget as RummyItemRenderer);

                    var index:Number = mainCanvas.getChildIndex(event.currentTarget as RummyItemRenderer);


                    mainCanvas.removeChild(img);
                    mainCanvas.addChildAt(img,index);

                    setCardsPosition();
                }
            }

            private function setCardsPosition():void{
                var b:int = 0;
                var a:int;
                var cardsArray:Array = mainCanvas.getChildren();
                for(a = 0;a < cardsArray.length; a++)
                {
                    mainCanvas.getChildAt(a).x = 50+b;
                    b = b+30;
                    mainCanvas.getChildAt(a).y=20;
                }
            }

            private function doDragStart(event:MouseEvent):void
            {
                var dragInitiator:RummyItemRenderer = event.currentTarget as RummyItemRenderer;
                var dragSource:DragSource = new DragSource();
                DragManager.doDrag(dragInitiator, dragSource, event);
            }

        ]]>
    </fx:Script>

    <mx:Canvas id="mainCanvas" backgroundColor="#DDDDDD" width="400" height="200" x="50" y="50">

        <local:RummyItemRenderer id="firstID" x="{mainCanvas.x}" y="20" width="200" height="80%" backgroundColor="#FF0000"
                                  mouseDown="doDragStart(event)" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)"
                                  setImageSource="myImageURL"/>
        <local:RummyItemRenderer id="secondID" x="{mainCanvas.x + 30}" y="20" width="200" height="80%" backgroundColor="#00FF00"
                                 mouseDown="doDragStart(event)" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)"
                                 setImageSource="myImageURL"/>
        <local:RummyItemRenderer id="thirdID" x="{mainCanvas.x + 60}" y="20" width="200" height="80%" backgroundColor="#0000FF"
                                 mouseDown="doDragStart(event)" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)"
                                 setImageSource="myImageURL"/>
    </mx:Canvas>

</s:Application>

RummyItemRenderer.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

            [Bindable]
            private var _setImageSource:String;

            public function get setImageSource():String
            {
                return _setImageSource;
            }

            public function set setImageSource(sourceURL:String):void
            {
                _setImageSource = sourceURL;
            }


        ]]>
    </fx:Script>

    <s:Image id="imageID" source="{_setImageSource}"/>
</mx:Canvas>
于 2012-09-24T08:27:45.547 回答