这个问题是有关ActionScript 3.0和Flash CS6
This question is related to ActionScript 3.0 and Flash CS6
我试图使物体抖动了一下在一定的几秒钟。我做了一个影片剪辑,并取得该code:
I am trying to make an object shake a bit in a certain for some seconds. I made it a "movieclip" and made this code:
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;
}
}
在测试的电影,我得到多个错误,完全看像这样的:
When testing the movie I get multiple errors looking exactly like this one:
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()
另外,对象犯规晃动,但它运行平稳向上向左一点每一个刻度。
Also, the object doesnt shake, but it moves steadily upwards to the left a bit every tick.
要点: 我想知道我是怎么停的脚本后,对象不在舞台/场景了,以及如何使它左右摇晃,我看不出有什么不对我的脚本,请大家帮忙,谢谢^ _ ^
To the point: I wish to know how I stop the script after the object is not in the stage/scene anymore and also how to make it shake around, as I do not see what is wrong with my script, please help, thank you ^_^
您必须记住原来的起始位置,并计算出该点的防抖效果。这是我的影片剪辑晃动的影响。它动态地增加了3个变量(指定startPosition,shakeTime,maxShakeAmount)它。如果您使用的类,您将它们添加到您的剪辑。
You have to remember the original start position and calculate the shake effect from that point. This is my shake effect for MovieClips. It dynamically adds 3 variables (startPosition, shakeTime, maxShakeAmount) to it. If you use classes, you would add them to your clips.
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);
}
}
您可以使用这样的:
// shake for 100 frames, with max distance of 15px
this.shake(myMc, 100, 15);
BTW:在Flash中,应启用的允许调试的你的发布设置的有更详细的错误。这也给后面的行号,你的code被破坏。
BTW: In Flash, you should enable 'permit debugging' in your 'publish settings' to have more detailed errors. This also gives back the line numbers where your code is breaking.
更新: code现在随着时间的推移/最大距离分开。
update: Code now with time / maximum distance separated.