抖动效果 - Flash CS6 ActionScript3.0

The*_*ody 7 actionscript-3 flash-cs6

此问题与ActionScript 3.0和Flash CS6有关

我试图让一个物体在某种程度上震动一段时间.我做了一个"movieclip"并制作了这段代码:

import flash.events.TimerEvent;

var Machine_mc:Array = new Array();

var fl_machineshaking:Timer = new Timer(1000, 10);
fl_machineshaking.addEventListener (TimerEvent.TIMER, fl_shakemachine);
fl_machineshaking.start ();


function fl_shakemachine (event:TimerEvent):void {


 for (var i = 0; i < 20; i++) {

  Machine.x += Math.random() * 6 - 4;
  Machine.y += Math.random() * 6 - 4;
 }

}
Run Code Online (Sandbox Code Playgroud)

在测试电影时,我发现了多个与此完全相同的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Historieoppgave_fla::MainTimeline/fl_shakemachine()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
Run Code Online (Sandbox Code Playgroud)

此外,物体不会摇晃,但每次嘀嗒声都会向上稳定向上移动一点.

要点:我想知道如何在对象不在舞台/场景之后停止脚本以及如何让它摇晃,因为我没有看到我的脚本有什么问题,请帮忙,谢谢^ _ ^

Mar*_*nol 3

您必须记住原始的起始位置并从该点计算抖动效果。这是我的 MovieClips 抖动效果。它动态地向其中添加 3 个变量(startPosition、shakeTime、maxShakeAmount)。如果您使用类,您可以将它们添加到您的剪辑中。

import flash.display.MovieClip;
import flash.geom.Point;

function shake(mc:MovieClip, frames:int = 10, maxShakeAmount:int = 30) : void 
{
    if (!mc._shakeTime || mc._shakeTime <= 0)
    {
        mc.startPosition = new Point(mc.x, mc.y);
        mc._shakeTime = frames;
        mc._maxShakeAmount = maxShakeAmount;
        mc.addEventListener(Event.ENTER_FRAME, handleShakeEnterFrame);
    }
    else
    {
        mc.startPosition = new Point(mc.x, mc.y);
        mc._shakeTime += frames;
        mc._maxShakeAmount = maxShakeAmount;
    }
}

function handleShakeEnterFrame(event:Event):void
{
    var mc:MovieClip = MovieClip(event.currentTarget);
    var shakeAmount:Number = Math.min(mc._maxShakeAmount, mc._shakeTime);
    mc.x = mc.startPosition.x + (-shakeAmount / 2 + Math.random() * shakeAmount);
    mc.y = mc.startPosition.y + (-shakeAmount / 2 + Math.random() * shakeAmount);

    mc._shakeTime--;

    if (mc._shakeTime <= 0)
    {
        mc._shakeTime = 0;
        mc.removeEventListener(Event.ENTER_FRAME, handleShakeEnterFrame);
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以这样使用它:

// shake for 100 frames, with max distance of 15px
this.shake(myMc, 100, 15);
Run Code Online (Sandbox Code Playgroud)

顺便说一句:在 Flash 中,您应该在“发布设置”中启用“允许调试”以获得更详细的错误。这也会返回代码中断的行号。


更新:
现在代码与时间/最大距离分开。