Here we are checking out another new class of Actionscript 3.0 .
The Timer class is an embellishment of the methods setInterval and setTimeout and, actually, Adobe suggest using these instead.
When you create an instance of the Timer class, it just executes a certain piece of code at regular intervals.
You can specify the frequency of the intervals and the number of times it needs to be repeated.
For example"
I create an FLA and save it as " prova_timer.fla " .
I create the Document Class, an AS file saved as " Prova.as " , structured like so:
Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;
public class Prova extends MovieClip
{
private var numQuadrati:int=25;
private var lato:int=25;
private var distanza:int=10;
private var andata:Boolean=true;
private var clips_array:Array;
private var points_array:Array;
private var timer:Timer;
public function Prova()
{
clips_array=new Array();
points_array=new Array();
disegnaQuadrati();
posizionaQuadrati();
initTimer();
}
private function disegnaQuadrati():void
{
for(var i:int=0;i < numQuadrati;i++)
{
var sprite:MovieClip=new MovieClip();
sprite.graphics.beginFill(0x000000,1);
sprite.graphics.moveTo(-lato,-lato);
sprite.graphics.lineTo(lato,-lato);
sprite.graphics.lineTo(lato,lato);
sprite.graphics.lineTo(-lato,lato);
sprite.graphics.lineTo(-lato,-lato);
addChild(sprite);
clips_array.push(sprite);
}
}
private function posizionaQuadrati():void
{
var piccoloW:Number=Math.floor(this.parent.stage.stageWidth/5);
var piccoloH:Number=Math.floor(this.parent.stage.stageHeight/5);
for(var i:Number=0;i < 5 ;i++)
{
for(var j:Number=0;j < 5;j++)
{
var point:Point=new Point(piccoloW*j,piccoloH*i);
points_array.push(point);
}
}
for(var k:int=0;k < points_array.length;k++)
{
clips_array[k].x=lato*2+points_array[k].x;
clips_array[k].y=lato*2+points_array[k].y;
}
}
private function initTimer():void
{
timer=new Timer(100,clips_array.length);
timer.addEventListener(TimerEvent.TIMER,this.gestioneIntervallo);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,finitoEffetto);
timer.start();
}
private function gestioneIntervallo(t:TimerEvent):void
{
if(andata)
{
clips_array[timer.currentCount-1].width+=20;
clips_array[timer.currentCount-1].height+=20;
}
else
{
clips_array[timer.currentCount-1].width-=20;
clips_array[timer.currentCount-1].height-=20;
}
}
private function finitoEffetto(TimerEvent):void
{
andata=!andata;
initTimer();
}
}
}
And this is the result:
Later!
Bookmarks