There are several ways you could deal with this. You could start loading the images you need, and start your game with an incomplete set of images and wait for the rest to arrive as they do. Not very neat - but - possible.
The other way would be to wait for all the images to load. The "please wait, loading..." approach.
For the purposes of our game, we assume we're loading a fixed set of images, and we load them 'up-front', before the game starts that is. We will create a little library for doing just that.
We create an instance of the ImageLibrary class show below. We call 'setup()' on the library which starts it loading the images in parallel. Once all the images have loaded, the library calls us back.
Whats this callback stuff I keep talking about? Well, in Computer-Science there are two ways of communicating with systems (with that I mean entities other than your own pogram). Synchronous and Asynchronous. Synchronous should be most familiar to you. Suppose I want to load a file from disc, and there is a function provided by the Operating System called "loadFile( filename )".
In synchronous mode, I'd call / invoke loadFile("my file") and the program would what we call "block" until the loading of the file had completed. In most cases, that block would be really short, almost unnoticeable. The "blocking" means my program will be stopped until the loading (or whatever the remote entity needs to do) has completed. We call it a "block" because we're passing control to another part of the system and effectively go to sleep ourselves until it is all done. The trick is, you never really aware that you've been put to sleep. As far as you're concerned, its just another step in the program.
In asynchronous mode you'd have to give "loadFile" one additional piece of information. You'd have to tell "loadFile" who to notify of the completion of the task (in a lot of cases the system callback could also include information on what happened (e.g. successfully loaded the file, could not find the file, etc). It is asynchronous because it doesn't block our program. But at the same time, our program doesn't get what it needs immediately either. The advantage of asynchronous is that we can start doing other things while we wait. Sometimes that isn't an option. It could be that we need whatever it is right here and now. You can use "asynchronous mode" in a synchronous fashion (Don't fret. Most of the time, computer programs behave in a synchronous fashion).
Whether something is synchronous or asynchronous is not up to us. It is part of whatever library / function you're using at the time.
You could do the following for instance to turn something inherently asynchronous into a synchronous step.
There is always a danger with asynchronous callbacks that you never get the callback. This can happen if something goes terribly wrong. Worse, the thing you're waiting for takes a long time to complete due to unforeseen circumstances. I've seen web-servers return a byte every few seconds. Technically that isn't considered a time-out. It however results in pages taking hours to complete. I'm sure you've experienced it yourself in your own web-browser. The only thing you can do is to 'stop' the page loading, and restart ('refresh') the page starting from scratch.
You could change the while loop above to include a time limit.
This is the same while loop we had before, but it will only ever wait for five seconds (unless the callback completes before then).
In general - I wouldn't recommend you use the "while () { sleep }" approach in java-script. It works well in other languages like Java or C# where you have more control over the process itself, but isn't that elegant in java-script.
Lets look at our image library inside an html page. You might have guessed by now that java-scripts Image() load function is asynchronous (Setting an image's .src to a url starts it loading an image). The library is designed to call us back when its done. Don't forget our new friend jQuery, loaded right at the beginning.
The library just illustrates a point. But its powerful enough for you to use in your own games. We'll take a look later at how to use this library exactly in the game.
No comments:
Post a Comment