AS3 Preloader

Posted February 16th, 2010 in Actionscript 3.0, Codes, Flash Component, Source Files by Jimi Sulaiman

Another Flash Preloader created today. Click on image to try.

In this example, the preloader will appear when the method loadMovieToContent is called from the button click event. Simply set the swf file or the image to be loaded as the parameter for this method.

The source code:

package com.jimisulaiman
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.*;

	/*
	Code by jimisulaiman.com on 16 February 2010
	*/

	public class Preloader extends Sprite
	{
		//constructor
		public function Preloader()
		{
			//add listeners to the button
			load_swf_btn.addEventListener(MouseEvent.CLICK,clickHandler,false,0,true);
			load_img_btn.addEventListener(MouseEvent.CLICK,clickHandler,false,0,true);
			//on loaded, hide preloader
			myPreloader.visible = false;
		}

		//set variables
		var perLoaded:Number = 0;
		var loader:Loader;

		//initialize
		protected function initHandler(event:Event):void
		{
			//show preloader, reset back to zero
			myPreloader.visible = true;
			perLoaded = 0;
		}
		//using onEnterFrame might comes in handy, so I'm just gonna use it here
		protected function enterFrameHandler(event:Event):void
		{
			//set the percentage text%
			myPreloader.per.text = perLoaded + "%";
		}
		protected function progressHandler(event:ProgressEvent) {
			//calculate the percentage loaded
			perLoaded = Math.round((event.bytesLoaded/event.bytesTotal)*100);
			trace("progress:  "+perLoaded+"%");
		}
		private function completeHandler(event:Event):void {
			trace("completed!");
			myPreloader.visible = false;
			//place the loaded movieclip to the content area
			myContent.placer.addChild(loader);
		}

		public function loadMovieToContent(mymovie:String):void {
			trace("Loading "+mymovie);
			//new loader everytime
			loader = new Loader();
			//add those listeners
			loader.contentLoaderInfo.addEventListener(Event.OPEN,initHandler);
			loader.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			//load it!
			loader.load(new URLRequest(mymovie));
		}

		//set the buttons behaviour
		protected function clickHandler(event:MouseEvent):void
		{
			switch( event.target )
			{
				case load_swf_btn:
				    //trace("button clicked!");
				    loadMovieToContent("content/myswf.swf");
					break;
				case load_img_btn:
				    loadMovieToContent("content/image.jpg");
					break;
			}
		}

	}

}

Download full source code here:  AS3 Preloader (60)

  • Share/Bookmark

Simple AS3 Preloader

Posted November 18th, 2008 in Actionscript 3.0, Source Files by Jimi Sulaiman

A very simple flash preloader with easing bar written in Actionscript 3.0. Required Flash CS3.

Click here for demo

Download Source File: Simple AS3 preloader (170)

  • Share/Bookmark