Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 1 of 1

Thread: splice method of the Array class

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    splice method of the Array class

    flash templates
    In this tutorial, we saw how important can be an Array.
    Also, I explained the basics of the Arrays .
    I think it is time to start looking at some methods of the Array Class in more details.
    A very useful one is the method "splice".

    This method adds or eliminates an element from an array without creating a copy of the Array itself.
    Let us say that we want to create a banner in rotation which would display different banners for our client.
    This banner loads an external SWF every tot seconds.
    It should be random but without reloading the same external SWF twice.
    In this case, the method splice is our man"

    I create a FLA and I save it as "main.fla".
    I create a Document Class, an AS file saved as "Main.as", implemented the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.utils.Timer;
    	import flash.events.TimerEvent;
    	
    	public class Main extends MovieClip
    	{
    		private var movies:Array;
    		
    		private var timer:Timer;
    		
    		public function Main()
    		{
    			initArray();
    			initTimer();
    		}
    		
    		private function initArray():void
    		{
    			movies=new Array('swf_1','swf_2','swf_3','swf_4','swf_5','swf_6','swf_7','swf_8','swf_9','swf_10','swf_11','swf_12','swf_13','swf_14');
    		}
    		
    		private function initTimer():void
    		{
    			timer=new Timer(50,movies.length);
    			timer.addEventListener(TimerEvent.TIMER,fishNumber);
    			timer.addEventListener(TimerEvent.TIMER_COMPLETE,finish);
    			timer.start();
    		}
    		
    		private function fishNumber(evt:TimerEvent):void
    		{
    			var r:int=Math.floor(Math.random()*movies.length);
    			trace(movies[r]);
    			movies.splice(r,1);
    		}
    		
    		private function finish(evt:TimerEvent):void
    		{
    			timer.reset();
    			trace('------------------------');
    			initArray();
    			initTimer();
    		}
    	}
    }
    This is the result of the trace:






    Let us analyse the code

    Properties


    an Array into which I insert the name of the SWF to load
    private var movies:Array;
    a timer
    private var timer:Timer;

    Constructor function
    I call the method initArray
    initArray();
    I call the method initTimer
    initTimer();

    Methods
    initArray();
    I populate the Array
    movies=new Array('swf_1','swf_2','swf_3','swf_4','swf_5','swf _6','swf_7','swf_8','swf_9','swf_10','swf_11','swf _12','swf_13','swf_14');

    initTimer();
    I create a new timer with a speed of 300 (as an example, that value can be changed as wanted) and with a repeat time equal to the length of the Array
    timer=new Timer(300,movies.length);
    I tell timer to add a listener which will call the method fishNumber every 300 cents of seconds timer.addEventListener(TimerEvent.TIMER,fishNumber );
    I add a second listener which will tell me when the timer has finished its job
    timer.addEventListener(TimerEvent.TIMER_COMPLETE,f inish);
    I start the timer
    timer.start();

    fishNumber();
    this method is called every 300 cents of seconds
    I create a numerical variable to which I assign a random value from zero to the Array"s length
    var r:int=Math.floor(Math.random()*movies.length);
    I retrieve the element from the Array placed in the index equal to the value of the variable r
    trace(movies[r]);
    Next is the splice! I tell Flash to remove from the Array the element placed in the index "r" and to remove only that one. If instead of 1, I would write 2, Flash would have removed 2 elements: the one at index "r" and the preceding one. If I would write 3, Flash would have removed the element at index "r" and the 2 preceding ones. In this example, we only want to remove the single element with an index equal to the value of "r".
    movies.splice(r,1);

    finish();
    this method is called every time the timer as finished a complete cycle (equal to the Array"s length)
    I reset the timer
    timer.reset();
    I carry out a trace so to understand when the timer has finished the complete cycle of call
    trace('------------------------');
    I recall the method initArray to populate once again the Array with the SWF names
    initArray();
    I recall the method initTimer which will start a new cycle
    initTimer();

    See you soon!

+ Reply to Thread

Similar Threads

  1. array di pulsanti che chiamano array di funzioni
    By chil8 in forum Actionscript 3.0 base
    Replies: 0
    Last Post: 12-02-10, 16:01
  2. mailTo method
    By Flep in forum Tutorials
    Replies: 11
    Last Post: 06-01-10, 18:16
  3. fileLoaded Method
    By olicorse in forum Flash English
    Replies: 3
    Last Post: 08-04-09, 02:51
  4. indexOf - method of Array class
    By Flep in forum Tutorials
    Replies: 1
    Last Post: 05-08-08, 08:24
  5. Metodo splice della classe Array
    By Flep in forum Articoli e tutorials
    Replies: 1
    Last Post: 18-06-08, 21:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts