如何阻止粒子移出舞台,Actionscript随机粒子生成器

时间:2016-03-15 18:34:18

标签: actionscript-3 flash

下面是随机粒子生成器的一些代码。我想改变它,以便粒子在停止之前只移动一小段距离而不会消失。就像现在一样,他们继续前进,直到他们离开舞台。我是Actionscript的新手,所以任何帮助都会受到赞赏,谢谢。

var particleArray:Array = new Array();
var maxParticles:Number = 50000;

function addParticle(e:Event)
{
    var Symbol:Symbol3 = new Symbol3();
    Symbol.alpha = Math.random() * .8 + .2;
    Symbol.scaleX = Symbol.scaleY = Math.random() * .8 + .2;
    Symbol.xMovement = Math.random() * 10 - 5;
    Symbol.yMovement = Math.random() * 10 - 5;
    Symbol.x = mouseX;
    Symbol.y = mouseY;
    particleArray.push(Symbol);
    addChild(Symbol);
    Symbol.cacheAsBitmap = true;

    if (particleArray.length >= maxParticles)
    {
        removeChild(particleArray.shift());
    }

    Symbol.addEventListener(Event.ENTER_FRAME,moveParticle);
}

function moveParticle(e:Event)
{
    e.currentTarget.x += e.currentTarget.xMovement;
    e.currentTarget.y += e.currentTarget.yMovement;
}

var myTimer:Timer = new Timer(50);
myTimer.addEventListener(TimerEvent.TIMER, addParticle);
myTimer.start();

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是在执行数字后删除事件侦听器:

function addParticle(e:Event)
{
    var Symbol:Symbol3 = new Symbol3();
    Symbol.alpha = Math.random() * .8 + .2;
    Symbol.scaleX = Symbol.scaleY = Math.random() * .8 + .2;
    Symbol.xMovement = Math.random() * 10 - 5;
    Symbol.yMovement = Math.random() * 10 - 5;

    // set the number of times this instance can move. 
    // Using a randomly generated number here
    Symbol.movementCount = int(Math.random() * 10); 
    Symbol.x = mouseX;
    Symbol.y = mouseY;
    particleArray.push(Symbol);
    addChild(Symbol);
    Symbol.cacheAsBitmap = true;

    if (particleArray.length >= maxParticles)
    {
        removeChild(particleArray.shift());
    }

    Symbol.addEventListener(Event.ENTER_FRAME,moveParticle);
}

function moveParticle(e:Event)
{
    e.currentTarget.x += e.currentTarget.xMovement;
    e.currentTarget.y += e.currentTarget.yMovement;

    // decrement your move count, and then check to see if it's hit zero
    e.currentTarget.movementCount--;
    if(e.currentTarget.movementCount <=0)
    {
        // if so, remove the event listener
        e.currentTarget.removeEventListener(Event.ENTER_FRAME, moveParticle);
    }
}