PDA

View Full Version : Preloader - When should I use one, how and why?



_sluimers_
December 2nd, 2009, 09:35 AM
I think I might need a preloader as I load in tilesets. My problem is that I have no idea how to do this. The examples I have seen so far preload swf files.
I don't think that that is what I need as I don't see how that preloads all png files that my game uses, for example this class:



package nl.obg.www {
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.URLRequest;

public class Flag extends Sprite {
private var tileSize:uint;

public var tx:int;
public var ty:int;

private var _loader:Loader;

public function Flag(nwX:int, nwY:int, nwZ:int){
x = nwX;
y = nwY;
z = nwZ;

_loader = new Loader();
addChild(_loader);
_loader.load(new URLRequest("../images/flag.png"));
}

public function init():void {
tileSize = (parent as Map).getTileSize();

tx = x;
ty = y;

x = x*tileSize;
y = y*tileSize;
}
}
}
How would one preload this png and others?

therobot
December 2nd, 2009, 01:19 PM
It's been awhile, but off the top of my head, I'm pretty sure the Loader class should dispatch a Progress event that should have info about how many bytes have loaded, regardless of whether you're loading a swf or an image.

When should you display loading progress? I'd say whenever there could be a noticeable delay where nothing appears to be happening, I.E. every time you load ANYTHING. You don't always need the progress bar/percentage indicator, but just some indicator that your game is busy trying to do something.

TOdorus
December 2nd, 2009, 04:35 PM
The example you give doesn't preload, it loads in png's when the swf has already started and needs them (note the pre part of the word). Jeff from 8bitrocket has a good tutorial on preloading.

http://www.8bitrocket.com/newsdisplay.aspx?newspage=10807

therobot
December 2nd, 2009, 06:59 PM
The example you give doesn't preload, it loads in png's when the swf has already started and needs them (note the pre part of the word). Jeff from 8bitrocket has a good tutorial on preloading.

http://www.8bitrocket.com/newsdisplay.aspx?newspage=10807

Well I think by 'preloader' sluimers meant some sort of loading indicator as he's loading in each tileset. I could be wrong, though.

_sluimers_
December 3rd, 2009, 08:15 AM
I know the specific code doesn't preload. It was something thing I want to preload. I had no idea what a preloader meant so I was imagining some magical piece of code that loads in every image from a directory, but it turns out preloading simply means loading the file in the main function and adding a progress bar, otherwise the user would see a big white screen for a while and think flash crashed or something.

That said, I would also like to preload every file from a directory.

I know that this thread tries to tackle it, which uses php, but it doesn't give me any output.
http://www.kirupa.com/forum/showthread.php?t=291047


In fact, I'm not even succeeding into reading even the simplest php file.



public function Main() {
var request:URLRequest = new URLRequest("readfromdir.php");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);//send the request with URLLoader()
}

public function handleComplete(event:Event):void {
var vars:URLVariables = new URLVariables(event.target.data);

trace("firstName:" + vars.firstName);
trace("lastName:" + vars.lastName);
trace("age:" + vars.age);
}


<?php
$numVariables = 0;

function writeVariable( $name, $value )
{
if( $numVariables > 0 )
{
echo "&";
}

echo $name . "=" . urlencode($value);

$numVariables ++;
}

writeVariable( "firstName", "foo" );
writeVariable( "lastName", "bob" );
writeVariable( "age", "99" );

?>
My output:


firstName:undefined
lastName:undefined
age:undefined
How come even this doesn't work?

therobot
December 3rd, 2009, 08:50 AM
Am I not understanding this? You want a loading indicator for each image you load into flash so you can tell when the game is busy loading crap, right?

If so, just look at the example at the bottom of this page: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Loader.html

This is where the magic happens:



private function progressHandler(event:ProgressEvent):void {
trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}


The loader class dispatches progress events every single frame that data is transferred. You can use the information of the progress event to easily whip together a loading bar when you load your tilesets.

_sluimers_
December 3rd, 2009, 09:11 AM
You're understanding it, but I've just solved it and run into a new problem, namely getting all my tilesets/chraracters/other images from a directory in case I put in a new tileset so I don't have to edit the main function to write in new filenames.

I'm lazy.

therobot
December 3rd, 2009, 09:21 AM
i can't help you writing the php file to do that, but a common gotcha i've found when dealing with php and flash is that it doesn't seem to work when testing the movie IN flash. i always had to move the swf to some server in order for it to access the php correctly. Others here are probably more knowledgeable in integrating php and flash.

Good luck!

_sluimers_
December 3rd, 2009, 09:28 AM
Thanks!

That makes everything clear.