Thursday, February 25, 2010

Random Timers Tutorial

Click here to get the .fla file or paste all the information below the symbol "/*" into the first frame of action script in flash for a step-by-step tutorial. I think it is annoying when you can't see exactly what is happening. The comments will reveal your path!

/*
First off, welcome! This tutorial will teach you how to make an object that will travel in
a certain direction over a random distance at random intervals and continue to do so forever,
forever, amen. I would encourage you to imput this data and tweak the values, tweens, and directions
because the use of timers can apply to anything when you want to trigger an event.
*/
/*To start, make at least one symbol, here we named it bloc_mc, and place it in the first frame of
the stage*/

//import the classes we will be using
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.utils.Timer;
import flash.events.TimerEvent;

/*define a variable for the timer, the first numeral is the time in miliseconds
between each time the timer goes off, the second number is the number of times
the timer will repeat. We want it to repeat forever so we set it to 0, and will
make it repeat later on.*/
var timer:Timer = new Timer(1000,0);
/*Now we set the timer to listen for each time it goes off, and when it goes
off to perform a function, we will call it "doNextTween*/
timer.addEventListener(TimerEvent.TIMER, doNextTween);
//Since we want the timer to go indefinately we need the timer to repeat itself
timer.reset();
//And of course the timer has to start
timer.start();

//this var, lastCall, will be used to trace each time the timer fires
var lastCall:uint = 0;
//this variable will be the y coordinate for the object
var newY:Number = bloc_mc.y;

//Time to set up the function that "plays" each time the timer fires, as a timer event
function doNextTween(e:TimerEvent):void{

/*The object, bloc_mc, needs to move. So we set it to tween between y positions. Object,
direction of the tween, the type of ease, the start point of the animation, the distance, duration
*/
var myTweenBloc:Tween = new Tween(bloc_mc, "y", Bounce.easeOut, bloc_mc.y, newY, 1, true);
//we want the new y position to be different each time, increasing the number we multiply by
//will make the y value higher or lower, I think. you mess with it
newY = (Math.random() * 100);
//just for our sake, to know that the timer is working, this tracks how long each timer cycle took
trace("Time since last call: " + String(getTimer() - lastCall));
//If we don't want the timer to go off at even intervals we set a timer delay, here we make it random
//increasing and decreasing the value we multiply by will speed up or slow down the timer fire
timer.delay = Math.random()*10000;
//this sets up the trace so that AS3 knows that lastCall is to get the timer
lastCall = getTimer();

//Here we added just another object with the same attributes to work at the same time, to form eye brows
var myTweenSquare:Tween = new Tween(square_mc, "y", Bounce.easeOut, square_mc.y, newY, 1, true);
newY = (Math.random() * 100);
timer.delay = Math.random()*10000;
lastCall = getTimer();
}

//THE END