gpt4 book ai didi

actionscript-3 - 摇晃效果 - Flash CS6 ActionScript 3.0

转载 作者:行者123 更新时间:2023-12-04 06:42:40 26 4
gpt4 key购买 nike

此问题与 ActionScript 3.0 和 Flash CS6 有关

我试图让一个物体在几秒钟内稍微晃动一下。我把它变成了一个“电影剪辑”并制作了这个代码:

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

此外,物体不会晃动,但它会在每个滴答声中稳定地向左移动一点。

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

最佳答案

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 代码,请参阅上面的链接••

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;
}
}
}

}

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

关于actionscript-3 - 摇晃效果 - Flash CS6 ActionScript 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13685003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com