Reputation: 121
Sorry about the title, I'm not exactly sure how to explain this with a few words.
I've looked through google searches and found that people find it easlier when exporting classes in frame 2 when using a preloader. I'm currently using the usual method of having frame 1 with the preloader, frame 2 with the asset holder and the bulk of the game, frame 3 has the stop(); action. This is the tutorial I followed to get a working preloader AS Gamer.
After many hours of searching I've only found mentions and revalations of people using export on frame 2 instead of the traditional asset holder, but no examples or tutorials.
Does any know where I can find a tutorial with example code I reference to get a export on frame 2 preloader?
Upvotes: 0
Views: 918
Reputation: 4244
I've been meaning to write a complete tutorial on the subject of preloaders, but I'm still in the middle of getting my new site launched... until then, here are the hightlights:
Two parts are going to be required -
one on the javascript side: sending flashvars into the preloader, and embedding the flash object into the html structure. I love swfobject for its simplicity and its bulletproofness.
html:
<html>
<head>
<script type="text/javascript" src="/javascript/swfobject/swfobject.js"></script>
<script type="text/javascript" src="/javascript/logic.js"></script>
</head>
<body>
<div id="flashContainer">content for when swfobject fails</div>
</body>
</html>
logic.js:
var _flashvars =
{
filePath : "/flash/swf/yourcontent.swf",
FR : "60",
verbose : "false",
SW : "760",
SH : "550"
};
var _params =
{
quality : 'high',
menu : 'false',
base : '/flash/swf/'
};
var _attr =
{
id : "flashContainer"
};
function embedSWF()
{
swfobject.embedSWF(
"/flash/preLoader.swf",
"flashContainer",
"760",
"550",
"9.0.0",
"/javascript/swfobject/expressInstall.swf",
_flashvars,
_params,
_attr
);
return;
}
//onload event
embedSWF()
On the flash side, we have the preloader fla (or however you're building it). Its purposefully "content-agnostic" because I reuse it for all my preloading. You could save some time by hardcoding in the flash vars, i spose. Anyway, I've got two classes -
Main.as - which pulls in the flashvars at runtime, initiates the load, and then responds to the loading progress, finally adding the loaded content into the display list.
Preloader.as - which does all the heavy lifting.
Note these are partial classes pulled directly from my project, and I've included only the bare essentials (you'll have to finish out the code and tailor as needed).
Note also that this is one of many ways to do this ;)
Main.as :
package {
public class Main extends MovieClip
{
private var _loadParams :Object;
private var _filePath :String;
private var _frameRate :uint;
private var _stageH :Number;
private var _stageW :Number;
private var _verbose :Boolean;
public function Main():void
{
super();
if (stage)
{
init();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
}
/**
* once preloader is onstage, create param object.
* assign vars from param object
*
* stage height / stage width:
* some files may have SH and SW defined, if they are, used them as
* stage dimensions. If they are not, default to current stageHeight,Width
*
* @param e
*/
private function init(e:Event = null):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_loadParams = new Object();
_loadParams = LoaderInfo(stage.loaderInfo).parameters;
_filePath = String( parseParam( "filePath", "undefined.swf" ) );
_frameRate = uint( parseParam( "FR", "24" ) );
_stageH = Number( parseParam( "SH", String(stage.stageHeight) ) );
_stageW = Number( parseParam( "SW", String(stage.stageWidth) ) );
( String( parseParam( "verbose", "true" )) == "true")?_verbose = true : _verbose = false;
initiateLoader();
}
private function parseParam(name:String, defaultValue:String):String
{
if (_loadParams.hasOwnProperty(name) && _loadParams[name] != "" && _loadParams[name] != "undefined")
{
return _loadParams[name];
}
else
{
return defaultValue;
}
}
private function initiateLoader():void
{
_loader = new PreLoader(_filePath, _verbose);
_loader.addEventListener("displayObjectLoaded", onLoadComplete, false, 0, true);
this.addEventListener(Event.ENTER_FRAME, setLoadPercentage, false, 0, true);
}
/**
* respond to load graphically
*/
private function setLoadPercentage(e:Event):void
{
_loadPercentage = Math.round((_loader.progressNumberArray[0] /_loader.progressNumberArray[1])*100);
if (_verbose)
{
_txt.text = String(_loadPercentage) + "% Loaded";
}
}
/**
* finally, add the loader object into the display list.
*/
private function onLoadComplete(evt:Event):void
{
_loader.removeEventListener("displayObjectLoaded", onLoadComplete);
this.removeEventListener(Event.ENTER_FRAME, setLoadPercentage);
this.addChild(_loader);
}
Preloader.as
package {
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.text.TextField;
public class PreLoader extends Sprite {
private var _loader : Loader;
private var _loaderInfo : LoaderInfo;
private var _verbose : Boolean = false;
private var _loadProgressString : String = "";
private var _bytesLoaded : Number = 0;
private var _bytesTotal : Number = 0;
public function PreLoader(path:String, verbose:Boolean)
{
_verbose = verbose;
_loader = new Loader();
_loaderInfo = _loader.contentLoaderInfo;
// there are several other events to listen for and respond to
_loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress, false, 0, true);
_loaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
try
{
_loader.load(new URLRequest(path));
}
catch (evt:Error)
{
_loader.unload();
var errMsg:TextField = new TextField();
with (errMsg) {
width = 300;
text = "Unable to load content:\n" + evt.toString();
}
addChild(errMsg);
dispatchEvent(new Event("displayObjectLoaded"));
}
}
private function onProgress(evt:ProgressEvent):void
{
var loadPercent:int = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
_bytesLoaded = evt.bytesLoaded;
_bytesTotal = evt.bytesTotal;
_loadProgressString = (loadPercent + " % loaded: " + _bytesLoaded + " bytes of " + _bytesTotal + " total bytes");
if (_verbose)
{
trace(_loadProgressString);
}
}
private function onComplete(evt:Event):void
{
_loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
_loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
addChild(_loader);
dispatchEvent(new Event("displayObjectLoaded"));
}
/**
* getter for load details
*/
public function get progressNumberArray():Array
{
return [_bytesLoaded,_bytesTotal];
}
Hopefully that helps get you started. Let me know if you have any questions.
cheers!
Upvotes: 2