在计时器上的落雪 - Actionscript 3

时间:2014-07-09 18:54:15

标签: actionscript-3 timer

我有下面的雪落代码,我只想下降一段时间,然后停止下雪。有什么建议?我尝试在计时器函数中包装整个事物,然后从计时器调用该函数,但它会产生错误。

var snowflakes:Array = new Array();
var snowflakeProps:Dictionary= new Dictionary(true);
var max_snowsize:Number = .04;
// pixels
var snowflakesCnt:Number = 150;
var oheight:Number;
var owidth:Number;



init();
function init():void {

    owidth = width;
    oheight = height;
    // quantity
    for (var i:int=0; i<snowflakesCnt; i++) {

        var t:MovieClip = new SnowFlake();//
        t.name = "snowflake"+i;

        t.alpha = 20+Math.random()*60;
        t.x = -(owidth/2)+Math.random()*(1.5*owidth);
        t.y = -(oheight/2)+Math.random()*(1.5*oheight);
        t.scaleX = t.scaleY=.5+Math.random()*(max_snowsize*10);
        var o:Object = new Object();
        o.k = 1+Math.random()*2;
        o.wind = -1.5+Math.random()*(1.4*3);

        snowflakeProps[t] = o;

        addChild(t);
        snowflakes.push(t);
    }
    addEventListener(Event.ENTER_FRAME, snowFlakeMover);
}
function shakeUp():void{
    for (var i:int=0; i<snowflakes.length; i++) {
        var t:MovieClip = snowflakes[i] as MovieClip;
        t.x = -(owidth/2)+Math.random()*(1.5*owidth);
        t.y = -(oheight/2)+Math.random()*(1.5*oheight);
    }
}
function snowFlakeMover(evt:Event):void {
    var dO:MovieClip;
    var o :Object;
    if(visible && parent.visible){
    for (var i:int = 0; i < snowflakes.length; i++) {
        dO = snowflakes[i] as MovieClip;
        o = snowflakeProps[dO];
        dO.y += o.k;
        dO.x += o.wind;
        if (dO.y>oheight+10) {

            dO.y = -20;

        }
        if (dO.x>owidth+20) {

            dO.x = -(owidth/2)+Math.random()*(1.5*owidth);
            dO.y = -20;

        } else if (dO.x<-20) {

            dO.x= -(owidth/2)+Math.random()*(1.5*owidth);
            dO.y = -20;
        }

    }
    }
}

1 个答案:

答案 0 :(得分:1)

这是一种停止移动并从显示屏上移除所有雪花的简单方法:

var stopTime:Number = 7; // number of seconds to wait before stopping
var stopTimer:Timer = new Timer(stopTime * 1000, 1);
stopTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onStopTimerComplete);
stopTimer.start();
function onStopTimerComplete(e:TimerEvent):void
{
    for each(var snowflake:MovieClip in snowflakes)
    {
        TweenLite.to(snowflake, 4, {alpha:0, delay:Math.random()*3, onComplete:removeSnowflake, onCompleteParams:[snowflake]});
    }
}

function removeSnowflake(snowflake:MovieClip):void
{
    if(snowflake.parent)
    {
        // remove the snowflake from the display list
        snowflake.parent.removeChild(snowflake);
    }

    // delete the properties
    delete snowflakeProps[snowflake];

    // remove the snowflake from your array
    snowflakes.splice(snowflakes.indexOf(snowflake),1);

    // if there are no more snowflakes in the array, stop moving them
    if(!snowflakes.length)
    {
        removeEventListener(Event.ENTER_FRAME, snowFlakeMover);
    }
}