AS3:在sigle帧时间轴中重复功能

时间:2015-04-29 10:19:56

标签: actionscript-3 flash

我是一个非常新的动作脚本。我有单帧时间轴,并且有垂直移动影片剪辑的功能。我想重复这三次。 代码有效,我不确定这是否正确,或者它是否过于复杂。

var pocet:Number = 0;

pruh.addEventListener(Event.ENTER_FRAME, fl_AnimateVertically);

function fl_AnimateVertically(event:Event)
{
if (pruh.y >= stage.stageHeight) {
    pocet++;
}
if (pruh.y < stage.stageHeight) {
pruh.y += 3;
}
else {
    pruh.y = 0 - pruh.y;
}
if (pocet == 3) {
    pruh.removeEventListener(Event.ENTER_FRAME, fl_AnimateVertically);
}
}

感谢名单

2 个答案:

答案 0 :(得分:1)

祝贺你实现目标。

您的代码在可读性方面可以得到改进。您有fl_AnimateVertically作为描述性名称,但除此之外,很难弄清楚到底发生了什么。我的意思是它确定它会增加3到y,这可能会导致运动,但理解确切的行为并非易事。

这就是为什么你想要使用抽象或更多自上而下的方法,因为它经常被称为.. 您目前正在做的是为坐标添加一个值,从而创建一个动画。你真正想要的是创建一个动画,而不是详细说明实际意义。

当然,人们之前使用代码创建了动画。这就是为什么你可以在抽象意义上创建动画:动画是一个对象的属性随时间的变化。 In the realm of flash an animation is called a tween and there's a class doing exactly that.

我们来看一下示例代码:

var myTween:Tween = new Tween(myObject, "x", Elastic.easeOut, 0, 300, 3, true);

并将其应用于您的情况。

var verticalAnimation:Tween = new Tween(pruh, "y", Elastic.easeOut, pruh.y, stage.stageHeight, 3, true);

您必须根据自己的喜好调整持续时间。我希望你看到这更容易阅读和维护,因为你指定动画的属性,如持续时间。您还可以指定缓动,这会使运动更有趣。

好的,这只是一个动画,但你想要3,对吗? 更准确地说,当你完成时,你想再次做同样的动画。 你可以做到这一点:

var animationCount:uint = 0;
var verticalAnimation:Tween = new Tween(pruh, "y", Elastic.easeOut, pruh.y, stage.stageHeight, 3, true);

verticalAnimation.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish); // wait for the animation to be finished

function onMotionFinish(e:TweenEvent):void
{
    animationCount++; // add 1 to the counter

    if(animationCount >= 3)  // check how many times the animation finished so far
    {
        // if it was the last one, remove the listener
        verticalAnimation.removeEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
    }
    else
    {
        // otherwise rewind and start again
        verticalAnimation.rewind();
        verticalAnimation.start();
    }
}

除了Tween类内置的其他库之外,还有更强大的库。 The one from greensock is very popular and easy to use您可以find the documentation for the flash version here

答案 1 :(得分:0)

试试这个

var pocet:Number = 0;

pruh.addEventListener(Event.ENTER_FRAME, fl_AnimateVertically);
var startY:int=pruh.y;
function fl_AnimateVertically(event:Event)
{
if (pruh.y >= stage.stageHeight) {
    pocet++;
	pruh.y=startY;
}
if (pruh.y < stage.stageHeight) {
pruh.y += 3;
}
else {
    pruh.y = 0 - pruh.y;
}
if (pocet ==3) {
    pruh.removeEventListener(Event.ENTER_FRAME, fl_AnimateVertically);
	
trace("done");
}
}

相关问题