Preloader madness

(Hariel Cid) #1

Hi there!

I’m coding my first full fledged game, and I’m trying to add a small preloader. I tried implementing some classes I found online, but I don’t seem to grasp the concept behind it, so it has never worked so far.

Is there a simple way to implement a basic one? I am looking for the most basic of examples, so I can start building it from there.

Thanks a lot!

Preloader Not Appearing Until 100% Loaded [SOLVED]
(Jacob Albano) #2

Here is the preloader I use for all my projects. It should be pretty simple to make it work for yours. I probably don’t entirely understand all of what makes it work, but I’d be happy to help troubleshoot if it gives you trouble. :slight_smile:

(Zachary Lewis) #3

@jacobalbano I’ve stripped your preloader to the bare minimum to preload. This should be much easier to digest (not that yours wasn’t).

public class Preloader extends MovieClip 
	public function Preloader()
		stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

	public function onEnterFrame(e:Event):void
		if (loaderInfo.bytesLoaded >= loaderInfo.bytesTotal)
			// If all bytes are loaded, start the game.
			// Update your screen to display whatever you'd like.
	private function startup():void
		stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		var mainClass:Class = getDefinitionByName("YourGameClass") as Class;
		parent.addChild(new mainClass as DisplayObject);

(Hariel Cid) #4

Thanks a lot, I’ll start implementing this right away :smiley:

(Andrew) #5

Hi there, I tried Zach’s code and it seemed fine at first, but while it worked in Firefox and other browsers, it didn’t work in Chrome! It just sat at an empty bar… FOREVER! The game loaded fine in Chrome without a preloader though, it just took a while.

Does anyone know why this happened or have a fix? A preloader is pretty much all my game needs!

(Ultima2876) #6

Chrome has problems accessing this variable most of the time and reports it as either 0 or infinity. The best thing is to encode the size of the swf in bytes directly into your preloader class and use that instead:

private var TOTAL_BYTES: int = 4000000; //4mb swf

The way I do it is to right click the release build swf file to get the size in bytes, then take a little bit off (this means it will fill the bar a little before the swf is fully loaded but this isn’t usually an issue and is much less annoying than the bar never quite getting full - just remember not to overdraw your bar!).

Other people have mentioned getting the value from ‘flashvars’ etc but that is something I don’t know a lot about.

Make sure you remember to update BYTES_TOTAL for every different build you do! (when you change art assets etc - it’s only really worth setting it when you’re getting ready to upload the game!)

(Andrew) #7

This seemed to work perfectly! Thanks a bunch!

(lordmagnet) #9

The code found here was immensely helpful. I’m just curious how to add a splash screen to this. Everything I’ve tried has failed. Nothing fancy, just a .jpg in the background would be great.

(Bartłomiej Kalemba) #10

Sorry for rearchieve, but I want to load swf in browser. After loading whole swf, there is no time for loading bar, because everything is loaded.

Any idea how to preload game in browser?

I try to load small swf and then inside large swf. But i don’t have idea how to run loaded swf in FlashPunk?

I tested code from @zachwlewis and @jacobalbano

(Jacob Albano) #11

Are you running the swf locally? If so, the loading will be so fast you won’t see the bar at all. You’ll have to upload it to your website or a Flash portal like Newgrounds or Kongregate if you want to test the loading time.

As for the second part, are you trying to load the swf as a movieclip asset? If so, that should go in a new topic.

(Bartłomiej Kalemba) #12

No, I run it from external server… Try this options You mentioned… Thanks! :smile: I try my game at FGL, and I must wait bout 15sek (with white screen) and then suddenly game appear…

New topic: How can I get my preloader to stop looping?

(Nate ) #13

Sorry for digging up an old thread! I have implemented this, both with Zach’s code as well as Jacob’s! I used Zach’s code to understand it and Jacobs code to add the fancy loading bar. My question is, although I am stating in the Preloader class 640x480 the preloader takes up the entire browser window, then once the game loads the game is 640x480. Any thoughts guys?


(Ultima2876) #14

Make sure you use a clipping mask to stop it drawing outside of the desired area. FlashPunk does this by default, whereas regular flash does not.

(Nate ) #15

Well it isn’t making the bar leave the window, the overall window is just massive it isn’t 640x480

(Nate ) #16


I fixed it, in the Preloader class I made the following changes:

Used to be:

sw = stage.stageWidth;
sh = stage.stageHeight;
w = stage.stageWidth * 0.8;

Changed these three lines to:

sw = 640;
sh = 480;
w = sw * 0.8;

All is fine and good now! :smiley:

(Frazer Bennett Wilford) #17

I’m probably missing something simple here.


ReferenceError: Error #1065: Variable Main is not defined. at global/flash.utils::getDefinitionByName() is my extends Engine class which starts the world.

(Ultima2876) #18

Is it in a package? You have to include the package name (e.g. mine are com.kickbackgames.Main or com.lunaticpandora.Main).

(Frazer Bennett Wilford) #19

The preloader class is in the same location as the main class.

(Jacob Albano) #20

Regardless, if the main class is in a package at all, you’ll need to specify it.

(Mike Evmm) #21

I have the same problem, when calling getDefinitionByName("Worlds.Levels."+String(level.@levelClass)) unless I write out all the classes I might call before (so:

public function LevelSelect() 

I get the same error.