如何在enter_frame监听器"animatesmoke"中获得"smokeobject"的速度属性,并参考movieclip"whitesmoke",它也是"smokeobject"的属性
这是我的代码
public function createRocketSmoke()
{
var smokeObject:Object = new Object();
smokeObject.whiteSmoke = new Bitmap(new WhiteSmoke(0,0));
smokeObject.whiteSmoke.x=targetX + Math.random()*4-8;
smokeObject.whiteSmoke.y=targetY + Math.random()*4-8;
smokeContainer=new MovieClip();
smokeContainer.addChild(smokeObject.whiteSmoke);
addChild(smokeContainer);
var randomScale = Math.random();
if(randomScale<.5)
randomScale = randomScale+.5;
smokeObject.whiteSmoke.scaleX= randomScale;
smokeObject.whiteSmoke.scaleY= randomScale;
smokeObject.speed = Math.random();
smokeObject.whiteSmoke.rotation = Math.random()*360;
smokeObject.whiteSmoke.addEventListener(Event.ENTER_FRAME,animateSmoke);
}
public function animateSmoke(event:Event):void
{
//here i want the speed property of the 'smokeObject'
}
您要做的事情很快就会变得非常混乱。
我建议为Smoke创建一个类,并给它一个update()
函数:
public class Smoke
{
// Properties.
private var _whiteSmoke:Bitmap;
private var _speed:Number = 0;
// Constructor.
public function Smoke(targetX:Number = 0, targetY:Number = 0)
{
// Prepare graphics.
_whiteSmoke = new Bitmap(new WhiteSmoke());
_whiteSmoke.x = targetX + Math.random()*4-8;
_whiteSmoke. y = targetY + Math.random()*4-8;
_whiteSmoke.rotation = Math.random()*360;
_whiteSmoke.scaleX = whiteSmoke.scaleY = 0.5 Math.random() * 0.5;
// Apply random speed.
_speed = Math.random();
}
// Update this smoke.
public function update():void
{
//
// Update this smoke here.
//
}
// The graphics for this smoke.
public function get whiteSmoke():Bitmap
{
return _whiteSmoke;
}
// The speed for this smoke.
public function get speed():Number
{
return _speed;
}
}
这将允许你更容易地更新你的烟雾对象与一个阵列和一个循环来更新每一个:
// This array will hold all your Smoke objects.
var allSmoke:Array = [];
// Create some Smoke.
var smoke1:Smoke = new Smoke(10, 10);
var smoke2:Smoke = new Smoke(10, 10);
// Add it to the array.
allSmoke.push(smoke1);
allSmoke.push(smoke2);
// Prepare game loop.
addEventListener(Event.ENTER_FRAME, update);
function update(e:Event):void
{
// Loop through smoke objects and update them.
for each(var i:Smoke in allSmoke)
{
i.update();
}
}