Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

splice method of the Array class

This is a discussion on splice method of the Array class within the Tutorials forums, part of the Flash English category; In this tutorial , we saw how important can be an Array. Also, I explained the basics of the Arrays . I ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 19-11-07, 06:55
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
splice method of the Array class

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!
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:48..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
mailTo method Flep Tutorials 8 20-08-08 01:38
indexOf - method of Array class Flep Tutorials 1 05-08-08 09:24
Metodo splice della classe Array Flep Articoli e tutorials 1 18-06-08 22:51
getBounds method Flep Tutorials 0 20-12-07 06:38
How to call another class from the Document Class with Flash CS3 Flep Tutorials 0 09-10-07 19:50


All times are GMT. The time now is 12:02.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap