如何阻止延长舞台的动画片段边缘进入AS3中的舞台边界?

时间:2015-05-05 18:44:42

标签: actionscript-3 flash actionscript adobe flash-cc

我有一个背景图像,可以扩展舞台,顶部和底部,这个位置由按下图像的按键控制。我想根据图像的移动方式停止舞台顶部或底部的图像边缘。我希望这适用于“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);

1 个答案:

答案 0 :(得分:0)

听起来你想要约束对象,这样你就无法将它滚动到看到它边缘的位置?

在这种情况下,您需要对您的价值进行消毒,因此它不会超出这一点。

因此,要将对象向下移动,而不是像在此处一样减去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