I got function in Flash (Action Script 3) – that makes snowflakes falling. Now i want to make this snowflakes appear on screen only for 3 seconds. So I’m trying to use Timer class but i got problem:
var myTimer:Timer = new Timer(3000, 1);
myTimer.addEventListener(TimerEvent.TIMER, snowflakes);
myTimer.start();
function snowflakes(event:TimerEvent):void {
//snowflakes faling function
}
In this case snowflakes appear after 3 seconds and stay on the stage forver…So its kinda opposite what i wanted. I want them to appear from the very beginning then disappear after 3 second. How can I do that ?
…EDIT
Oh yes. I do apologies for that. Here is the code. I got snowFlakes class in separate file called Snowflake.as which contains that code:
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;
}
}
}
}
and first frame on the timeline on AS layer contains that code:
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();
}
}
Your timer is set just fine, the issue you’re having is what you’re doing with it.
Call snowFlakes right from the start, and bind the timer Event to removeSnowFlakes, where you will removeChild all snow flakes you added on the snowflakes function.
EDIT:
Apply the following changes to your code: