How to Make a Efficient Flash AS3 SWF content Loader[Preloader]


Very effi­cient Loader Cod­ing Tutorial

the code using here is very clean , sim­ple and error less . you can load exter­nal swf file and call func­tion in that swf file


step 1 : Cre­ate Loader.fla and LoaderClass.as

open Loader.fla go to prop­er­ties type “Load­er­Class” class : filed under PUBLISH tab

image1

step 2 : Make a pre­loader movie

click on rec­tan­gle tool or ®

image2

then draw a reac­t­an­gle width : 300px & height :10px , color :#63F262

image3

after that right click on the rec­tan­gle and con­vert to sym­bol, when con­vert to sym­bol win­dow popup type the (1)Name : mc , (2)Type : Movie Clip , (3)Registeration : Left-center

image6

then name the Movi­Clip “mc” on prop­er­ties tab image7

again right click on the rec­tan­gle and con­vert to sym­bol, when con­vert to sym­bol win­dow popup type the (1)Name : preloader_mc , (2)Type : Movie Clip , (3)Registeration : cen­ter , (4)tick Export for Action­Script under Link­age tab

image5

dou­ble click on the preloader_mc and cre­ate a dynamic text field and name “percent_txt”

image8

image9

then click on Char­ac­ter Embed­ding and in the include these char­ac­ters: type “0123456789%”

image10

it will look like this

image10.5

step 3: Make Pre­loader Animation

dou­ble click on the preloader_mc and insert a frame at 100

image11

then cre­ate a motion tween on “mc” rec­tan­gle

image12insert a scale KeyFrame at 100th frame

image13

set width = 1 at first Frame

image14

come to root of the Scene delete the preloader_mc Movie Clip

and save your file

step 4: Write Pre­loader Class

open LoaderClass.as

package {

	//import needed classes

	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;

	dynamic public class LoaderClass extends MovieClip // main class
	{

		//declare private variables

		private var PRELOADER:preloader_mc= new preloader_mc();
		private var URL_:URLRequest;
		private var OBJLoader:Loader;
		private var isLoaded:Boolean;
		private var TOTAL_:Number;
		private var LOADED_:Number;
		private var isLoadedInited:Boolean;

		public function LoaderClass() {//main function
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			init();
		}

		public function init():void {
			PRELOADER.x = stage.stageWidth / 2;
			PRELOADER.y = stage.stageHeight / 2;
			addChild(PRELOADER);
			LoadContent(null);
			stage.addEventListener(Event.RESIZE,onResize);
		}

		public function LoadContent(URLReq:URLRequest) {
			if (URLReq == null) {
				URL_ = new URLRequest("swfContent.swf");
			} else {
				URL_ = URLReq;
			}
			OBJLoader = new Loader();
			OBJLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
			OBJLoader.load(URL_);
			addChild(OBJLoader);
			addEventListener(Event.ENTER_FRAME, onLoading);
		}
		public function loaded(param1:Event):void {
			isLoaded = true;
			return;
		}
		public function onLoading(e:Event) {
			TOTAL_ = OBJLoader.contentLoaderInfo.bytesTotal;
			LOADED_ = OBJLoader.contentLoaderInfo.bytesLoaded;
			var percent:Number = int(LOADED_ / TOTAL_ * 100);
			PRELOADER.gotoAndStop(percent);
			PRELOADER.percent_txt.text = percent + "%";
			if (! isLoadedInited && isLoaded) {
				isLoadedInited = true;
				var LoaderData:* = OBJLoader.content;
				LoaderData["loadData"](this);
			}
			if (TOTAL_ / LOADED_ == 1) {
				removeEventListener(Event.ENTER_FRAME,onLoading);
				removeChild(PRELOADER);
				stage.removeEventListener(Event.RESIZE,onResize);
				trace("content Loaded");
			}

			return;
		}

		public function onResize(e:Event) {

			PRELOADER.x = stage.stageWidth / 2;
			PRELOADER.y = stage.stageHeight / 2;

		}

	}

}

Source File :AS3 SWF Content Loading method-Source (110)

Bookmark and Share
VN:F [1.9.3_1094]
Rat­ing: 5.0/5 (1 vote cast)
VN:F [1.9.3_1094]
Rat­ing: +1 (from 1 vote)
How to Make a Effi­cient Flash AS3 SWF con­tent Loader[Preloader], 5.0 out of 5 based on 1 rating

, , , , , , ,

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

  1. #1 by vijay on September 15, 2009 - 4:25 AM

    hi.….….….….….!

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  2. #2 by did_ro on July 6, 2010 - 6:03 AM

    Hi, this com­po­nent looks amaz­ing, but can I use it with flash CS4 in an AIR project ? Actu­ally I didn’t man­aged to use it, but this could be because I don’t have enough skills to do so.
    So is this pos­si­ble ? How ?
    Thanks in advance for any reply.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  3. #3 by did_ro on July 6, 2010 - 6:14 AM

    Hi, this Date Picker com­po­nent looks amaz­ing, but can I use it with flash CS4 in an AIR project ? Actu­ally I didn’t man­aged to use it, but this could be because I don’t have enough skills to do so. Thanks in advance for any help.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
    • #4 by NiDiN on July 11, 2010 - 3:10 AM

      check this post for setup swc files in flash ide http://www.infogroupindia.com/blog/?p=513

      and down­load swc file from down­load section

      VN:F [1.9.3_1094]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.3_1094]
      Rating: 0 (from 0 votes)
  4. #5 by NiDiN on July 6, 2010 - 10:05 AM

    yes you can, use swc in your project

    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  5. #6 by poruntong on July 10, 2010 - 10:03 AM

    how can I use th ecom­po­nent with CS3 IDE

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
    • #7 by NiDiN on July 11, 2010 - 3:06 AM

      HI,

      check this post for setup swc files in flash ide http://www.infogroupindia.com/blog/?p=513

      and down­load swc file from down­load section

      VN:F [1.9.3_1094]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.3_1094]
      Rating: 0 (from 0 votes)
(will not be published)