动作脚本3(闪存)定时器 - 我无法正确设置定时器

时间:2011-11-24 01:37:41

标签: flash actionscript-3 timer using

我在Flash中获得了功能(动作脚本3) - 这使得雪花飘落。现在我想让这个雪花只在屏幕上显示3秒钟。所以我试图使用Timer类,但我遇到了问题:

var myTimer:Timer = new Timer(3000, 1); 
myTimer.addEventListener(TimerEvent.TIMER, snowflakes);
myTimer.start();

function snowflakes(event:TimerEvent):void {
 //snowflakes faling function
}

在这种情况下,雪花在3秒后出现 并永远留在舞台上......所以它与我想要的相反。我希望它们从一开始就出现,然后在3秒后消失。我怎么能这样做?

...编辑

哦,是的。我为此道歉。这是代码。我在单独的文件Snowflake.as中得到了snowFlakes类,其中包含该代码:

package 
{
    import flash.display.*;
    import flash.events.*;

    public class Snowflake extends MovieClip
    {
        private var xPos:Number = 0;
        private var yPos:Number = 0;

        private var xSpeed:Number = 0;
        private var ySpeed:Number = 0;

        private var radius:Number = 0;

        private var scale:Number = 0;
        private var alphaValue:Number = 0;

        private var maxHeight:Number = 0;
        private var maxWidth:Number = 0;

        public function Snowflake()
        {
            //SetInitialProperties();
        }

        public function SetInitialProperties()
        {
            //Setting the various parameters that need tweaking 
            xSpeed = .05 + Math.random()*.1;
            ySpeed = .1 + Math.random()*6;
            radius = .1 + Math.random()*2;
            scale = .01 + Math.random();
            alphaValue = .1 + Math.random();

            var stageObject:Stage = this.stage as Stage;
            maxWidth = stageObject.stageWidth;
            maxHeight = stageObject.stageHeight;

            this.x = Math.random()*maxWidth;
            this.y = Math.random()*maxHeight;

            xPos = this.x;
            yPos = this.y;

            this.scaleX = this.scaleY = scale;
            this.alpha = alphaValue;

            this.addEventListener(Event.ENTER_FRAME, MoveSnowFlake);
        }

        function MoveSnowFlake(e:Event)
        {
            xPos += xSpeed;
            yPos += ySpeed;

            this.x += radius*Math.cos(xPos);
            this.y += ySpeed;

            if (this.y - this.height > maxHeight)
            {
                this.y = -10 - this.height;
                this.x = Math.random()*maxWidth;
            }
        }
    }
} 

并且AS层上时间轴上的第一帧包含该代码:

import flash.utils.Timer;
import flash.events.TimerEvent;

var myTimer:Timer = new Timer(3000, 1);
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();

function runOnce(event:TimerEvent):void {
 for (var i:int = 0; i < 50; i++)
    {
        var newSnowFlake:Snowflake = new Snowflake();
        this.addChild(newSnowFlake);

        newSnowFlake.SetInitialProperties();
    }
}

2 个答案:

答案 0 :(得分:2)

你的计时器设置得很好,你所遇到的问题就是你正在做的事情 从一开始就调用snowFlakes,并将定时器Event绑定到removeSnowFlakes,在那里你将删除你在雪花功能上添加的所有雪花。

编辑:

对您的代码应用以下更改:

import flash.utils.Timer;
import flash.events.TimerEvent;

var snowFlakeContainer : MovieClip;
var myTimer:Timer = new Timer(3000, 1);
myTimer.addEventListener(TimerEvent.TIMER, removeSnowFlakes);
myTimer.start();

addSnowFlakes();

function addSnowFlakes():void {
    snowFlakeContainer = new MovieClip();
    for (var i:int = 0; i < 50; i++)
    {
        var newSnowFlake:Snowflake = new Snowflake();
        snowFlakeContainer.addChild(newSnowFlake);

        newSnowFlake.SetInitialProperties();
    }
}

function removeSnowFlakes(event:TimerEvent):void {
    removeChild(snowFlakeContainer);
    snowFlakeContainer = null;
}

答案 1 :(得分:0)

在我发布答案之前,这个问题已经接受了答案,但无论如何我都会回答。

您可以向SnowFlake对象添加一个内部Timer对象,完成后从父显示对象容器中删除SnowFlake。这是一个例子:

package 
{
    import flash.display.Sprite;
    import flash.display.TriangleCulling;
    import flash.events.Event;
    import flash.utils.Timer;

    public class Main extends Sprite 
    {
        private var _spawnTime:int = 5;
        private var _spawnTimeCounter:int;

        private var _triangles:Vector.<Triangle>;
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            _triangles = new Vector.<Triangle>();
            addEventListener(Event.ENTER_FRAME, onEnterFrame);

        }
        private function onEnterFrame(e:Event):void
        {
            if (_spawnTimeCounter > _spawnTime)
            {
                var triangle:Triangle = new Triangle(random(1, 5), 3000)
                triangle.x = random(0, stage.stageWidth);
                addChild(triangle);
                triangle.drop();

                _spawnTimeCounter = 0;
            }

            _spawnTimeCounter++;
        }

        private function random(min:Number, max:Number):Number
        {
            return Math.floor((Math.random() * ((1 + max) - min)) + min);

        }
    }

}

import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;

internal class Triangle extends Sprite
{
    public var speed:Number;

    private var _delay:Number;
    private var _timer:Timer;

    public function Triangle(speed:Number = 1, delay:Number = 0) 
    { 
        this.speed = speed;
        _delay = delay;

        init(); 

    }// end function

    public function init():void
    {
        graphics.beginFill(0xFFFFFF);
        graphics.lineStyle(2, 0x000000);
        graphics.drawTriangles(Vector.<Number>([0, 20, 15, 0, 30, 20]));
        graphics.endFill();

    }// end function

    public function drop():void
    {
        if (parent && !hasEventListener(Event.ENTER_FRAME))
        {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);

            if (_delay > 0)
            {
                _timer = new Timer(_delay, 1);
                _timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
                _timer.start();

            }// end if

        }// end if

    }// end function

    public function remove():void
    {
        if (parent) parent.removeChild(this);

    }// end function

    private function onEnterFrame(e:Event):void
    {
        y += speed;

    }// end function

    private function onTimerComplete(e:TimerEvent):void
    {
        removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
        removeEventListener(Event.ENTER_FRAME, onEnterFrame);

        remove();

    }// end function

}// end class

如果运行代码,您可以看到连续下降的雪花片(三角形)以不同的速度下降,但每个雪花片在3秒后消失。

enter image description here