7

这个问题与 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;
 }

}

在测试这部电影时,我遇到了多个错误,看起来与此一模一样:

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()

此外,该对象不会摇晃,但它会在每个滴答声中稳定地向上向左移动一点。

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

4

4 回答 4

4

AStupidNube 提出了关于原始立场的重要观点。因此,将其添加到摇晃应该是一个来回运动,所以不要依赖可能会或可能不会得到你想要的东西的随机值。随着时间的推移,摇晃也会产生抑制作用,所以试试这样的方法:

链接到工作代码

http://wonderfl.net/c/eB1E - 基于 Event.ENTER_FRAME

http://wonderfl.net/c/hJJl - 基于定时器

http://wonderfl.net/c/chYC - Event.ENTER_FRAME 基于额外的随机性

**1 到 20 个摇晃项目基于计时器的代码 - ENTER_FRAME 代码请参见上面的链接••</p>

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.utils.Timer;

public class testing extends Sprite {

    private var shakeButton:Sprite;
    private var graphic:Sprite;
    private var shakerPos:Array;
    private var shakers:Array;
    private var numShakers:int = 20;
    private var dir:int = 1;
    private var displacement:Number = 10;
    private var shakeTimer:Timer;

    public function testing() {
        this.shakers = new Array();
        this.shakerPos = new Array();
        this.addEventListener(Event.ADDED_TO_STAGE, this.init);
    }
    private function init(e:Event):void {
        this.stage.frameRate = 30;
        this.shakeTimer = new Timer(33, 20);
        this.shakeTimer.addEventListener(TimerEvent.TIMER, this.shake);
        this.graphics.beginFill(0x333333);
        this.graphics.drawRect(0,0,this.stage.stageWidth, this.stage.stageHeight);
        this.graphics.endFill();

        this.createShakers();

        this.shakeButton = this.createSpriteButton("Shake ");
        this.addChild(this.shakeButton);
        this.shakeButton.x = 10;
        this.shakeButton.y = 10;
        this.shakeButton.addEventListener(MouseEvent.CLICK, this.shakeCallback);
    }
    private function createSpriteButton(btnName:String):Sprite {
        var sBtn:Sprite = new Sprite();
        sBtn.name = btnName;
        sBtn.graphics.beginFill(0xFFFFFF);
        sBtn.graphics.drawRoundRect(0,0,80,20,5);
        var sBtnTF:TextField = new TextField();
        sBtn.addChild(sBtnTF);
        sBtnTF.text = btnName;
        sBtnTF.x = 5;
        sBtnTF.y = 3;
        sBtnTF.selectable = false;
        sBtn.alpha = .5;
        sBtn.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void { sBtn.alpha = 1 });
        sBtn.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void { sBtn.alpha = .5 });
        return sBtn;
    }
    private function createShakers():void {
        var graphic:Sprite;

        for(var i:int = 0;i < this.numShakers;i++) {
            graphic = new Sprite();
            this.addChild(graphic);
            graphic.graphics.beginFill(0xFFFFFF);
            graphic.graphics.drawRect(0,0,10,10);
            graphic.graphics.endFill();
            // add a 30 pixel margin for the graphic
            graphic.x = (this.stage.stageWidth-60)*Math.random()+30;
            graphic.y = (this.stage.stageWidth-60)*Math.random()+30;
            this.shakers[i] = graphic;
            this.shakerPos[i] = new Point(graphic.x, graphic.y);
        }
    }
    private function shakeCallback(e:Event):void {
        this.shakeTimer.reset();
        this.shakeTimer.start();
    }
    private function shake(e:TimerEvent):void {
        this.dir *= -1;
        var dampening:Number = (20 - e.target.currentCount)/20;
        for(var i:int = 0;i < this.numShakers;i++) {
            this.shakers[i].x = this.shakerPos[i].x + Math.random()*10*dir*dampening;
            this.shakers[i].y = this.shakerPos[i].y + Math.random()*10*dir*dampening;
        }
    }
}

}

现在这是一个线性阻尼,您可以通过平方或立方值来调整您认为合适的值。

于 2012-12-03T14:37:54.573 回答
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);
    }
}

你可以像这样使用它:

// shake for 100 frames, with max distance of 15px
this.shake(myMc, 100, 15);

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


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

于 2012-12-03T14:29:49.097 回答
3

这是所选答案的分叉版本,但更灵活一点,因为它还允许您设置频率。它还基于时间而不是帧,因此您可以在设置持续时间和间隔时考虑时间(毫秒)而不是帧。

用法类似于选择的答案:

摇(clipToShake,durationInMilliseconds,frequencyInMilliseconds,maxShakeRange);

这只是我使用 TimerEvent 而不是 ENTER_FRAME 的意思的一个例子。它也不需要向您正在摇动的 MovieClip 添加动态变量来跟踪时间、摇动量和起始位置。

    public function shake(shakeClip:MovieClip, duration:Number = 3000, frequency:Number = 30, distance:Number = 30):void
    {
        var shakes:int = duration / frequency;
        var shakeTimer:Timer = new Timer(frequency, shakes);
        var startX:Number = shakeClip.x;
        var startY:Number = shakeClip.y;

        var shakeUpdate:Function = function(e:TimerEvent):void
            {
                shakeClip.x = startX + ( -distance / 2 + Math.random() * distance);
                shakeClip.y = startY + ( -distance / 2 + Math.random() * distance); 
            }

        var shakeComplete:Function = function(e:TimerEvent):void
            {
                shakeClip.x = startX;
                shakeClip.y = startY;
                e.target.removeEventListener(TimerEvent.TIMER, shakeUpdate);
                e.target.removeEventListener(TimerEvent.TIMER_COMPLETE, shakeComplete);
            }

        shakeTimer.addEventListener(TimerEvent.TIMER, shakeUpdate);
        shakeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, shakeComplete);

        shakeTimer.start();
    }
于 2012-12-05T00:26:05.497 回答
0

-4 <= Math.random() * 6 - 4< 2

您将此偏移量添加到 Machine.x 20 次,因此向左移动的机会大于向右移动的机会。

看来您正在寻找这样的东西:

for each (var currentMachine:MovieClip in Machine_mc)
{
     currentMachine.x += Math.random() * 6 - 3;
     currentMachine.y += Math.random() * 6 - 3;
}
于 2012-12-03T14:27:43.637 回答