Game States Using Wade Scenes
Shri

Waders,

 

I put a little demo of how to implement game states in Wade using the built in scene system.

It is up in the projects section titled - Game States Using Scenes.

 

It is a rework of my previous game states demo, but instead of rolling my own, I have used wade's built in scene system.

It consists of a couple of .wsc (wade scene same as json) files and their associated behavior .js files.

If you look at the .js files, it should be pretty easy to understand.

 

Basically there are two type of states

  • States which cause the previous state to be wiped
  • Overlays which leaves the previous state in place and lay the new state over it. On closing, the overlay scene objects are removed and the previous state resumes.

 

This is just one way of accomplishing game state management with Wade.

If you dig into the scene methods in the api, there is a lot more functionality than I am using.

Hopefully a tutorial on the scene system will be forthcoming.

 

However, this example should get you up and running in a short time and keep you main code free of

conditional statements for state management.

 

The bird animations and background images are courtesy of Bevoulin at opengameart.org under the CC-0 license.

 

cheers,

Shri

All 4 Comments
Gio

Hey Shri

 

Thanks, that's going to be really useful for people who want to know how the scene system works. We'll try to write a tutorial about it at some point too, there is a video tutorial that's only slightly related, it doesn't really cover the coding side of it.

 

One thing I would suggest (though take it with a pinch of salt because I haven't looked at the code, so I don't really know what it's doing). It seems to be a bit slow the first time you switch scenes... is it because it's loading the assets?

 

If so, it may be worth preloading them first, to avoid that little pause that occurs the first time. For example you could load the scene json file (.wsc), then modify the addToScene value of its scene objects so it's always false, then import that scene with wade.importScene()- this will trigger loading all the assets used by the scene objects, without adding anything to the scene. After that, you'd run your game normally and keep doing what you're doing now. Just one of many ways of doing it really.

Shri

Gio,

 

I lliked the idea of putting all the image information in the .wsc file and not having it clutter up the load function.

But I modified the code so all the assets are loaded at startup.

I used load and not preload because of our previously discussed apple problem.

 

See if it runs any smoother ? I didn't profile anything so I'm not sure if it is faster.

 

Also, just an fyi, when I run in chrome, I get the following warning:

'CanvasRenderingContext2D.webkitImageSmoothingEnabled' is deprecated. Please use 'CanvasRenderingContext2D.imageSmoothingEnabled' instead.

wade_2.1.js: 107

 

cheers,

Shri

 

OkieDokie

Nice. It would be even nicer if there was a way to preload the assets without have them in the load function, as you say.

Gio

That is currently possible though. To make it easier, I think we can add a couple of new scene-related functions in the next version of WADE, to load all the assets from a scene file. Currently you'd do this:

// load the JSON file of the scenewade.loadJson('myScene.wsc', null, function(sceneData){    // change the scene data structure to make sure that objects are not added to the scene    if (sceneData.sceneObjects)    {        for (var i=0; i<sceneData.sceneObjects.length; i++)        {            sceneObjects[i].addToScene = false;        }    }        // call importScene - same as loadScene, but we can pass it a data object instead of the name of a JSON file    wade.importScene(sceneData, true, function()    {         // all assets loaded, do something here    });});

The main problem with the code above, is that you have these nested callbacks, which some people don't like. As a result, it cannot really be easily used in the load function.

 

We'll think of something that's more user-friendly for the next release


 

Post a reply
Add Attachment
Submit Reply
Login to Reply