First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing! | Flash Jobs
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/

Unique image

For the unique image approach you'll store the value of current in the LSO so that the next time, you can increase this value, and load the next image in the array. If you've reached the last element of the array, you have to start over again.
When you have the correct value, load the corresponding image:


var current:uin = lso.data.current;

current++;

if (current >= total)
current = 0;

loadImage(current);

Here's the complete code (Main.as):


package
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	
	public class Main extends Sprite 
	{
		private var images:Array;
		private var total:uint;
		private var loader:Loader;
		
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			images = new Array("800px-Wintersports_terrain_park_S-rail_GP1393.jpeg", "snowboard2.jpg", "snowboard12.jpg", "snowboarding15_800.jpg", "xtreme-snowboarding.jpg");
			total = images.length;
			
			getImage();
		}
		
		/**
		 * find the current image that should be loaded
		 */
		private function getImage():void 
		{
			// create a LSO identified by "unique"
			var lso:SharedObject = SharedObject.getLocal("unique");
			
			var current:uint;
			
			// does "current" exist already			
			if (lso.data.current != undefined)
			{
				// grab it
				current = lso.data.current;
				// increase it
				current++;
				// reset it if required
				if (current >= total)
				current = 0;				
			}
			// if it doesn't, initialize it
			else
			{
				current = 0
			}
			
			// store current in the LSO
			lso.data.current = current;
			
			// load the appropriate image
			loadImage(current);
		}
		/**
		 * load the current image
		 * @param	current
		 */
		private function loadImage(current:uint):void 
		{
			loader = new Loader();
			var img:String = images[current];
			var request:URLRequest = new URLRequest("images/" + img);
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImgComplete);
			
			loader.load(request);
		}
		
		/**
		 * when the image finishes loading
		 * add it to the display list
		 * @param	e
		 */
		private function loadImgComplete(e:Event):void 
		{
			var bm:Bitmap = Bitmap(loader.content);
			addChild(bm);
		}
		
	}
	
}
» Level Intermediate

Added: 2011-03-17
Rating: 3 Votes: 2
(10 being the highest)
» Author
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.