如何阻止扩展舞台的动画剪辑的边缘进入 AS3 中的舞台边界?
Posted
技术标签:
【中文标题】如何阻止扩展舞台的动画剪辑的边缘进入 AS3 中的舞台边界?【英文标题】:How do I stop the edge of movieclips that extend the stage from entering stage boundaries in AS3? 【发布时间】:2015-07-15 15:39:23 【问题描述】:我有一个扩展舞台的背景图像,顶部和底部,其位置由补间图像的按键控制。我想根据图像的移动方式将图像的边缘停在舞台的顶部或底部。我希望这适用于“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】:听起来您想约束对象,使您无法将其滚动到可以看到其边缘的位置?
在这种情况下,您需要清理您的价值,使其不会超出这一点。
所以,为了向下移动对象,而不是像你在这里做的那样仅仅减去 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
【讨论】:
以上是关于如何阻止扩展舞台的动画剪辑的边缘进入 AS3 中的舞台边界?的主要内容,如果未能解决你的问题,请参考以下文章
AS3 使用 ENTER_FRAME 对动态创建的影片剪辑进行动画处理