0

下面的代码试图分配,(现场制作bloodyParticle的属性)forceX和forceY,随机数。但问题是,所创建的所有三个实例的随机数都是相同的。

for(var k:int = 0; k<3; k++)
{
    var bloodyParticle:BloodyParticle = new BloodyParticle();
    bloodyParticle.forceX = Math.random() * 20 - 10;
    bloodyParticle.forceY = Math.random() * -20 + 10;
    bloodyParticle.addEventListener(Event.ENTER_FRAME, bloodyParticleLoop);
    bloodyParticle.x = bloodyPoint.x;
    bloodyParticle.y = bloodyPoint.y;
}

好的,根据评论,这就是我使用它们的方式

private function bloodyParticleLoop(e:Event)
    {
        bloodyBmd.fillRect(bloodyBmd.rect, 0);
        bloodyBmd.draw(MovieClip(e.target),MovieClip(e.target).transform.matrix);
        //tried e.target also
        e.target.x += e.target.forceX;
        e.target.y += e.target.forceY;
        e.target.forceY++;
    }

并且 forceX 和 forceY 的 getter/setter 较早时不存在,但我只是在现在看起来像这样的类中完成了它

包裹 {

import flash.display.MovieClip;


public class BloodyParticle extends MovieClip
{
    public var forceX:Number;
    public var forceY:Number;


    public function BloodyParticle()
    {
        forceX = Math.random() * 20 - 5; //cz i hav the random number here-
        forceY = Math.random() * -20 + 10;//-i commented the earlier 
                                                      //-initialization of forceX/Y
    }
}

}

4

0 回答 0