在 AS3 中,如何阻止延伸舞台的影片剪辑的边缘进入舞台边界?

How do I stop the edge of movieclips that extend the stage from entering stage boundaries in AS3?

我有一个背景图像,它扩展了舞台的顶部和底部,它的位置由对图像进行补间的按键控制。我想根据图像的移动方式将图像的边缘停在舞台的顶部或底部。我希望这适用于 "layers_mc" 和 "bg_img".

这是我的代码

import flash.events.KeyboardEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;

stop();

var isUpPressed:Boolean = false;
var isDownPressed:Boolean = false;
var tweenDown:Tween;
var tweenUp:Tween;

stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyboardUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardDown2);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyboardUp2);

    function onKeyboardDown(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.DOWN)
        {
            isDownPressed = true;
        }

        if (e.keyCode == Keyboard.UP)
        {
            isUpPressed = true;
        }
    }

    function onKeyboardUp(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.DOWN)
        {
            isDownPressed = false;
        }

        if (e.keyCode == Keyboard.UP)
        {
            isUpPressed = false;
        }
    }

    stage.addEventListener(Event.ENTER_FRAME,loop);

    function loop(event:Event):void
    {

    var posit:Number = layers_mc.y;
    var xDown:Number = layers_mc.y - 200;
    var xUp:Number = layers_mc.y + 200;

        if (isDownPressed)
        {
            tweenDown = new Tween(layers_mc, "y", Regular.easeOut, posit, xDown, 2, true);                
            trace(layers_mc.y);
        }

        if (isUpPressed)
        {
            tweenUp = new Tween(layers_mc, "y", Regular.easeOut, posit, xUp, 2, true);
            trace(layers_mc.y);
        }
    }

     function onKeyboardDown2(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.DOWN)
        {
            isDownPressed = true;
        }

        if (e.keyCode == Keyboard.UP)
        {
            isUpPressed = true;
        }
    }

    function onKeyboardUp2(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.DOWN)
        {
            isDownPressed = false;
        }

        if (e.keyCode == Keyboard.UP)
        {
            isUpPressed = false;
        }
    }

    stage.addEventListener(Event.ENTER_FRAME,loop2);

    function loop2(event:Event):void
    {

    var posit2:Number = bg_img.y;
    var xDown2:Number = bg_img.y - 50;
    var xUp2:Number = bg_img.y + 50;

        if (isDownPressed)
        {
            tweenDown = new Tween(bg_img, "y", Regular.easeOut, posit2, xDown2, 2, true);                
            trace(bg_img.y);
        }

        if (isUpPressed)
        {
            tweenUp = new Tween(bg_img, "y", Regular.easeOut, posit2, xUp2, 2, true);
            trace(bg_img.y);
        }
    }

更新:成功了

var posit:Number = layers_mc.y;
var xDown:Number = Math.max(-618.5, layers_mc.y - 200, stage.stageHeight - layers_mc.height); 
var xUp:Number = Math.min(500, layers_mc.y + 200);

听起来你想限制对象,这样你就不能将它滚动到可以看到它边缘的点?

在这种情况下,您需要清理您的值,以免超出该点。

因此,为了向下移动对象,而不是像您在此处所做的那样仅减去 200:

var xDown:Number = layers_mc.y - 200;

使边缘可见前的-y或当前值小于200中较大的值。

var xDown:Number = Math.max(layers_mc.y - 200, stage.stageHeight -layers_mc.height);

如果您采用舞台高度(或任何您的可见边界),然后减去您的子对象的高度,这将为您提供您的对象可以达到的最高点,并且它的边缘仍然位于阶段。

因此,您需要确保所有值都遵循以下规则:(使用 if 语句或 Math.max/min 等)

最小允许 y 点: stage.stageHeight - object.height;

最大允许 y 点: 0

相反,如果您还进行左右平移:

最小允许x点: stage.stageWidth - object.width

最大允许x点: 0

因此您的代码将如下所示:

var xDown:Number = Math.max(layers_mc.y - 200, stage.stageWidth - layers_mc.height)); //make sure the value doesn't go less than stageWidth - layer height
var xUp:Number = Math.min(0, layers_mc.y + 200); //make sure the value doesn't go above 0