使用数组

时间:2017-04-04 21:48:23

标签: arrays actionscript-3 drag-and-drop snapping

制作一个涉及用彩色瓷砖覆盖网格的简单游戏时遇到问题。我已经使用Arrays来建立拖放功能,但无法找到一种方法来轻松地将它们捕捉到网格上的位置。我已经将网格上的位置设置为名称为snapA1到snapJ10的影片剪辑,所有这些剪辑都以10个字母组合并且实例名称匹配,但这些剪辑已从阵列中删除,因为它们不需要移动。我一直存在的问题是我使用的任何功能只能用于捕捉到单个位置。基本上我只是寻找一个简短的手,允许两组或更多组对象相互捕捉,最好不需要暴力破解代码,我嘲笑你,相同代码的20,000个实例。 下面是我的代码,到目前为止只允许一个捕捉目标位置

import flash.events.Event;
import flash.events.MouseEvent;

var greenArr:Array = new Array 
(greenTablet1, greenTablet2, greenTablet3, greenTablet4, greenTablet5,
greenTablet6, greenTablet7, greenTablet8, greenTablet9, greenTablet10,
greenTablet11, greenTablet12, greenTablet13, greenTablet14, greenTablet15, 
greenTablet16, greenTablet17, greenTablet18, greenTablet19, greenTablet20);


for (var i1:uint =0; i1 < greenArr.length; i1++) {
 greenArr[i1].addEventListener(MouseEvent.MOUSE_DOWN, dragG);
 greenArr[i1].addEventListener(MouseEvent.MOUSE_UP, dropG);
}


function dragG(event:MouseEvent):void {
 event.currentTarget.startDrag();
}


function dropG(event:MouseEvent):void {
 event.currentTarget.stopDrag();
}




stage.addEventListener(MouseEvent.MOUSE_UP, stopMotionG1);

function stopMotionG1(evt:MouseEvent):void {
snapInPlaceG1();
}
function snapInPlaceG1():void {
if (snapA1.hitTestObject(greenTablet1)) {
greenTablet1.x = snapA1.x;
greenTablet1.y = snapA1.y;


}}

1 个答案:

答案 0 :(得分:0)

易。您需要的是停止使用标准拖动并设计自定义拖动。然后,您可以使用相同大小的脚本拥有尽可能多的可拖动对象和多个捕捉点:

package
{
    import flash.display.Sprite;

    import flash.geom.Point;

    import flash.events.Event;
    import flash.events.MouseEvent;

    // Main class.
    public class Snap extends Sprite
    {
        private var Circus:Vector.<Sprite>;
        private var Dots:Vector.<Point>;

        public function Snap() 
        {
            Dots = new Vector.<Point>;
            Circus = new Vector.<Sprite>;

            // Create snap points, put them on stage and keep their coordinates.
            for (var i:int = 1; i * 100 < stage.stageWidth; i++)
            {
                for (var j:int = 1; j * 100 < stage.stageHeight; j++)
                {
                    var aDot:Sprite = new Sprite;
                    var aPos:Point = new Point(i * 100, j * 100);

                    aDot.graphics.beginFill(0xFF0000, 0.3);
                    aDot.graphics.drawCircle(0, 0, 4);
                    aDot.x = aPos.x;
                    aDot.y = aPos.y;

                    addChild(aDot);
                    Dots.push(aPos);
                }
            }

            var aCircas:int = 2 + 3 * Math.random();

            // Add random number of draggable objects.
            while (Circus.length < aCircas)
            {
                Circus.push(addCirca());
            }
        }

        // This method adds a draggable circle of random radius, color, position.
        private function addCirca():Sprite
        {
            var result:Sprite = new Sprite;

            result.graphics.lineStyle(0, 0);
            result.graphics.beginFill(0x1000000 * Math.random(), 0.5);
            result.graphics.drawCircle(0, 0, 20 + 20 * Math.random());

            result.x = stage.stageWidth * Math.random();
            result.y = stage.stageHeight * Math.random();

            addChild(result);

            result.buttonMode = true;
            result.useHandCursor = true;
            result.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);

            return result;
        }

        // *** DRAGGING AN ITEM ** //

        // Snap radius.
        private var toleRance:int = 20

        // Mouse offset.
        private var offx:Number = 0;
        private var offy:Number = 0;

        // Dragged object.
        private var TheDrag:Sprite;

        private function startDragging(e:MouseEvent):void
        {
            // Get the dragged object reference.
            TheDrag = e.currentTarget as Sprite;

            // Bring it to front.
            setChildIndex(TheDrag, numChildren - 1);

            // Record the mouse offset. If you comment the next 2 lines
            // the object will just snap to mouse by its center.
            offx = TheDrag.x - mouseX;
            offy = TheDrag.y - mouseY;

            // Watch the object position every frame.
            addEventListener(Event.ENTER_FRAME, onFrame);

            // Subscribe for certain events that mean the end of dragging.
            stage.addEventListener(Event.MOUSE_LEAVE, stopDragging);
            stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
        }

        private function onFrame(e:Event):void
        {
            // Get new object (x,y) according to the mouse position.
            var aPos:Point = new Point;

            aPos.x = mouseX + offx;
            aPos.y = mouseY + offy;

            // Browse through the list of snap points.
            for (var i:int = 0; i < Dots.length; i++)
            {
                // Snap to a point within tolerance pixels radius.
                if (Point.distance(aPos, Dots[i]) < toleRance)
                {
                    aPos = Dots[i];
                    break;
                }

                // If there are no points withing the given radius,
                // then the object will use mouse positioning. 
            }

            TheDrag.x = aPos.x;
            TheDrag.y = aPos.y;
        }

        private function stopDragging(e:Event):void
        {
            TheDrag = null;

            // Stop monitoring.
            removeEventListener(Event.ENTER_FRAME, onFrame);

            // Stop waiting for events to cancel dragging.
            stage.removeEventListener(Event.MOUSE_LEAVE, stopDragging);
            stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
        }
    }
}