如何将starling图像对象置于滚动区域中

时间:2013-11-07 22:26:24

标签: scroll starling-framework

连续第二天我无法解决Starling中滚动区域的问题。这是我的代码:

    protected function onTouch(event:TouchEvent):void
    {
        var touches:Vector.<Touch> = event.getTouches(this, TouchPhase.MOVED);

        if (touches.length == 1)
        {
            // one finger touching -> move
            var delta:Point = touches[0].getMovement(parent);
            x += delta.x;
            y += delta.y;
        }            
        else if (touches.length == 2)
        {
            // two fingers touching -> rotate and scale
            var touchA:Touch = touches[0];
            var touchB:Touch = touches[1];

            var currentPosA:Point  = touchA.getLocation(parent);
            var previousPosA:Point = touchA.getPreviousLocation(parent);
            var currentPosB:Point  = touchB.getLocation(parent);
            var previousPosB:Point = touchB.getPreviousLocation(parent);

            var currentVector:Point  = currentPosA.subtract(currentPosB);
            var previousVector:Point = previousPosA.subtract(previousPosB);

            if (_enableRotation)
            {
                var currentAngle:Number  = Math.atan2(currentVector.y, currentVector.x);
                var previousAngle:Number = Math.atan2(previousVector.y, previousVector.x);
                var deltaAngle:Number = currentAngle - previousAngle;               
            }

            // update pivot point based on previous center
            var previousLocalA:Point  = touchA.getPreviousLocation(this);
            var previousLocalB:Point  = touchB.getPreviousLocation(this);
            pivotX = (previousLocalA.x + previousLocalB.x) * 0.5;               
            pivotY = (previousLocalA.y + previousLocalB.y) * 0.5;

            // update location based on the current center
            x = (currentPosA.x + currentPosB.x) * 0.5;
            y = (currentPosA.y + currentPosB.y) * 0.5;


            if (_enableRotation)
            {
                // rotate
                rotation += deltaAngle;
            }
            // scale
            var sizeDiff:Number = currentVector.length / previousVector.length;
            var diffSize:Number = scaleX * sizeDiff;  
            if(( diffSize>= _minScale) && (diffSize <= _maxScale))
            {
                scaleX *= sizeDiff;
                scaleY *= sizeDiff;
            }
        }

        var touch:Touch = event.getTouch(this, TouchPhase.ENDED);

        if (touch)
        {
            //here is my problem!
        }

    }

最后,如果我想检查我的子对象(图像)在哪里,如果这个对象在屏幕外,我想在屏幕中心移动滚动区域(精灵)。但我不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以使用ContainerLayout中的Gazman SDK来完成这项工作。它适用于每个starling displayObject。

有些人喜欢这样:

var layout = new ContainerLayout();
layout.horizontalCenter = 0;
layout.applyLayoutOn(myScroller);