I found this tutorial using TweenMax to create a preloader: http://tutorials.flashmymind.com/2009/0 ... nscript-3/
When I use the code I can successfully load an image but I've been having difficulties loading a .swf. I'm trying to make a preloader for my flash project so it doesn't necessarily have to load the swf externally - it can be a preloader that loads to the first frame of the animation.
Here's my code:
import com.greensock.*;
var holder:MovieClip = new MovieClip();
var circles:Array = new Array();
for (var i:uint=0; i < 5; i++) {
//Create a new preloader circle
var circle:preloaderHeart = new preloaderHeart();
//Set the x position for the circle
circle.x = i * 20;
//Set the "tweened" property to false since we haven't tweened the circle yet
circle.tweened = false;
//Add the circle to the circles array
circles.push(circle);
//Add the circle to the holder
holder.addChild(circle);
}
//Add the holder to the stage
addChild(holder);
//Position the holder to the center of the stage
var holderWidth:Number = holder.width;
var holderHeight:Number = holder.height;
holder.x = stage.stageWidth / 2 - holderWidth / 2;
holder.y = stage.stageHeight / 2 - holderHeight / 2;
//Create a loader which loads the image
var loader = new Loader();
//Let's load the image.
//You can use your own URL here...
loader.load(new URLRequest("final.swf"));
//Listen for the loading progress
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
//This function is called as the loading progresses
function progressHandler(e:ProgressEvent):void {
//Check how much has been loaded (in percentages)
var loadedPercentage:Number = (e.bytesLoaded / e.bytesTotal) * 100;
//If over 20% is loaded, tween the first circle if it hasn't been tweened yet.
if (loadedPercentage > 20 && ! circles[0].tweened) {
//Tween the circle
TweenMax.to(circles[0], 1, {tint:0xD56488, glowFilter:{color:0xD56488, alpha:1, blurX:10, blurY:10}});
}
//If over 40% is loaded, tween the second circle if it hasn't been tweened yet.
if (loadedPercentage > 40 && ! circles[1].tweened) {
//Tween the circle
TweenMax.to(circles[1], 1, {tint:0xD56488, glowFilter:{color:0xD56488, alpha:1, blurX:10, blurY:10}});
}
//If over 60% is loaded, tween the third circle if it hasn't been tweened yet.
if (loadedPercentage > 60 && ! circles[2].tweened) {
//Tween the circle
TweenMax.to(circles[2], 1, {tint:0xD56488, glowFilter:{color:0xD56488, alpha:1, blurX:10, blurY:10}});
}
//If over 80% is loaded, tween the fourth circle if it hasn't been tweened yet.
if (loadedPercentage > 80 && ! circles[3].tweened) {
//Tween the circle
TweenMax.to(circles[3], 1, {tint:0xD56488, glowFilter:{color:0xD56488, alpha:1, blurX:10, blurY:10}});
}
//If 100% is loaded, tween the fifth circle if it hasnn't been tweened yet.
if (loadedPercentage == 100 && ! circles[4].tweened) {
//Tween the circle and call the function circlesTweened() when the tween is complete (this is the last circle).
TweenMax.to(circles[4], 1, {tint:0xD56488, glowFilter:{color:0xD56488, alpha:1, blurX:10, blurY:10}, onComplete: circlesTweened});
}
}
//This function is called when the cirlces have been tweened (the image is loaded)
function circlesTweened():void {
//Loop through the circles
for (var i = 0; i < circles.length; i++) {
//Tween the circles to the left side of the stage.
//Call the function circleLeft() when the tween is finished
TweenMax.to(circles[i], 0.5, {delay: i * 0.1, x: -200, alpha: 0, onComplete: circleLeft, onCompleteParams: [circles[i]]});
}
}
//This function is called when a circle is animated to the left side.
function circleLeft(circle:preloaderHeart):void {
//Remove the circle from the stage
holder.removeChild(circle);
//Check if the circle is the last one (most right)
if (circle == circles[4]) {
//Remove the holder from the stage
removeChild(holder);
//Get the bitmap from the loader
var bm:Bitmap = (Bitmap)(loader.content);
//Add the bitmap to a movie clip holder
var bmHolder:MovieClip = new MovieClip();
bmHolder.addChild(bm);
//Add the bitmap holder to the stage
addChild(bmHolder);
//Animate the bitmap holder
TweenMax.from(bmHolder, 1, {alpha: 0});
}
}
The error I get when I compile it:
I would try and contact the creator of the tutorial but it doesn't look like the site has been updated since 2009. If anyone can clarify for me how to load a .swf with this preloader, I'd appreciate it. Thanks!