Here is the example of easing, reusable preloader that I am using in some of my projects.
The general idea is to tween the loader bar to the next point instead of just jumping to it.
The easiest way to achieve that effect is using one of the tweening libraries. My favorite one is Tweenlite, which you can get from http://blog.greensock.com/tweenliteas3/
(I'm still using the old library for this example).
Another point is that in most cases it is nice to add and remove preloaders with some effect, for example fade in/out. And for this reason loading actually starts when preloader fades in, and loaded content should be visible when preloader is fade out.
Note that preloader itself is not monitoring loading process, it is just contains method to set progress.
Example:
Preloader.as
-
package com.abrahamyan.util
-
{
-
-
-
/**
-
* ...
-
* @author Armen Abrahamyan | http://www.abrahamyan.com
-
*/
-
-
import flash.display.MovieClip;
-
import flash.events.Event;
-
import gs.TweenLite;
-
import gs.OverwriteManager;
-
import gs.easing.*;
-
/**
-
*
-
*/
-
public class Preloader extends MovieClip
-
{
-
-
public static const ON_SHOW:String = "onShow";
-
public static const ON_HIDE:String = "onHide";
-
public static const ON_PROGRESS_FINISH:String = "onProgressFinish";
-
public static var fadeTime:Number = .6;
-
public static var progressIntTime:Number = .3;
-
/**
-
*
-
* @param pLabel
-
* @param pXpos
-
* @param pYpos
-
*/
-
public function Preloader(pLabel:String = "Loading...", pXpos:Number = 0, pYpos:Number = 0):void
-
{
-
alpha = 0;
-
visible = false;
-
bar_mc.scaleX = 0;
-
label = pLabel;
-
move(pXpos, pYpos);
-
-
}
-
/**
-
*
-
* @param pXpos
-
* @param pYpos
-
*/
-
public function move(pXpos:Number, pYpos:Number):void
-
{
-
this.x = pXpos;
-
this.y = pYpos;
-
}
-
/**
-
*
-
*/
-
public function set label(pLabel:String):void
-
{
-
label_txt.text = pLabel
-
}
-
/**
-
*
-
*/
-
public function get label():String
-
{
-
return label_txt.text;
-
}
-
/**
-
*
-
*/
-
public function show():void
-
{
-
TweenLite.killTweensOf(this);
-
TweenLite.to(this, fadeTime, { autoAlpha:1, onComplete:onShowComplete});
-
}
-
/**
-
*
-
*/
-
private function onShowComplete():void
-
{
-
dispatchEvent(new Event(ON_SHOW));
-
}
-
/**
-
*
-
*/
-
public function hide():void
-
{
-
TweenLite.killTweensOf(this);
-
TweenLite.to(this, fadeTime, { autoAlpha:0, onComplete:onHideComplete});
-
}
-
/**
-
*
-
*/
-
private function onHideComplete():void
-
{
-
MovieClip(this.parent).removeChild(this);
-
dispatchEvent(new Event(ON_HIDE));
-
}
-
/**
-
*
-
*/
-
public function setProgress(pProgress:Number):void
-
{
-
if (pProgress<0 || pProgress>1)
-
{
-
throw new Error("Progress range should be in [0-1] interval");
-
}
-
TweenLite.killTweensOf(bar_mc);
-
TweenLite.to(bar_mc, progressIntTime, {scaleX:pProgress, onComplete:onProgressComplete} );
-
}
-
/**
-
*
-
*/
-
private function onProgressComplete():void
-
{
-
if (bar_mc.scaleX == 1)
-
{
-
dispatchEvent(new Event(ON_PROGRESS_FINISH));
-
}
-
}
-
/**
-
*
-
*/
-
public function destroy():void
-
{
-
TweenLite.killTweensOf(this);
-
}
-
}
-
-
}
Hi, great Example!
I tried to download, but could not ope the fla is it in CS 4? If so since I only have CS 3 that would explain why.
Anyway, I think that I can get it working with my own graphics, get work and blog`-`
Hi John, thx
FLA is CS4
U have to create MovieClip in library with progress bar(bar_mc) and textfield(label_txt) that refers to Preloader.as
thanks, I thought that it might be CS 4. Yeah I should have no problem with the graphics. the text field is easy, and the process bar will just be with rectangle tool. I will post tomorrow and let you know how it work out. This is a great example`-`
John
Hello again, I think that I might done it wrong:(
I set the doc class to point to Preloader.as, but I get an error:
Cannot create property bar_mc on com.abrahamyan.util.Preloader.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at com.abrahamyan.util::Preloader()
I was wondering if you can look at:
http://blog.meleana.info/?p=279
To see if I did something really wrong?
Thanks`-`
Johnny
Hi John. You have to create your preloader as MovieClip, export in first frame and in linkage->class use com.abrahamyan.util.Preloader
Hi,
Thanks for your help`-` I was able to get rid of those errors, but it seems when I try to access the as file in the fla
var bar_mc = new bar_mc();
var label_txt = new label_text();
I get:
A conflict exists with definition bar_mc in namespace internal
A conflict exists with definition label_txt in namespace internal
Without this in the fla I get a undefined variable.
Yikes, I think this might be a little above my knowledge:(
Thanks so much for all your help, your work is an inspiration to an aspiring flasher like me`-`
CS3 fla file is included in ZIP now.
thx