Starling ios拖动对象大于屏幕并设置边界

时间:2014-04-21 20:50:54

标签: drag-and-drop starling-framework

在Starling中我有一个非常大的MovieClip。动画片段的宽度比屏幕宽800%。如果我使用as3我可以设置一个边界,我可以拖动大MC。这样MC就不能被拖出屏幕了(所以显示空的背景)。你知道在starling中是否可以这样做吗?

下面的伪代码

    1. Add the mc to the stage
    2. Add eventlistener for touchEvent on MC
    3. Only drag the mc so its 0,0 coordinates are smaller than stage 0,0 coordinates
    4. Only drag the mc so its widthX,widthY are within stageWidthX, stageWidthY

希望这有道理吗?我只想拖动大型MC,因此它始终位于屏幕区域内。

干杯。

Ps:抱歉不包括一个例子,但我不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以通过检查TouchEvent的TouchPhase来查看手指拖动到的位置,然后仅在新位置落在舞台边界内时才更新MovieClip的X位置。

这是一个例子,使用四边形,但对于MovieClip也是如此: -

package  
{
    import starling.core.Starling;
    import starling.display.DisplayObject;
    import starling.display.Quad;
    import flash.geom.Point;
    import starling.display.Sprite;
    import starling.events.TouchEvent;
    import starling.events.Touch
    import starling.events.TouchPhase;

    public class QuadDrag extends Sprite
    {
        private var _startXPos:int = 0; // start of each drag

        public function QuadDrag() 
        {
            super();

            // create quad 8x stage size
            var quad:Quad = new Quad(Starling.current.stage.stageWidth*8, Starling.current.stage.stageHeight, 0xffffff);
            quad.setVertexColor(0, 0xff0000);
            quad.setVertexColor(1, 0x0000ff);
            quad.setVertexColor(2, 0xff0000);
            quad.setVertexColor(3, 0x0000ff);
            addChild(quad);

            // center quad on stage
            quad.x = Math.round(Starling.current.stage.stageWidth/2 - quad.width/2);
            quad.addEventListener(TouchEvent.TOUCH, onQuadTouch);
        }

        private function onQuadTouch(e:TouchEvent):void
        {
            var currentXPos:int = 0;
            var newXPos:int = 0;
            var touch:Touch = e.getTouch(stage);
            var target:DisplayObject = e.currentTarget as DisplayObject;
            if (touch == null)
            {
                return;
            }

            var position:Point = touch.getLocation(stage);

            if (touch.phase == TouchPhase.BEGAN )
            {
                // store start of drag x pos
                _startXPos = target.globalToLocal(new Point(touch.globalX, touch.globalY)).x;
            }
            else
            if (touch.phase == TouchPhase.MOVED )
            {
                // set limits for target x
                var minX:int = -Math.round(target.width - Starling.current.stage.stageWidth);
                var maxX:int = 0; 

                // calculate new x based on touch's global coordinates
                currentXPos = target.globalToLocal(new Point(touch.globalX, touch.globalY)).x;
                newXPos = target.x + currentXPos - _startXPos;

                if (newXPos <= maxX && newXPos>=minX) // set target's x if it falls within limits
                    target.x=newXPos;
            }
            else
            if (touch.phase == TouchPhase.ENDED )
            {
                // touch released
            }


            return;
        }
    }

}