The Game type is (as might be obvious) the primary game wrapper in Foundation. Game instances are simply OrderedUpdateDictionarys of Actor components (with some additional properties and methods). This structure allows for a fully customizable game loop as the "game loop" is simply a function which iterates over all actors in the collection and calls their
For convenience, a factory method is available (see below) which will create a game instance with a common set of components (time tracker, key, sound and touch controllers, a layer list, a renderer, etc.)
// Create a prefab game object var game = Foundation.Game.createDefaultPrefab(800, 400, document.body); // Load assets game.textures.push(new Foundation.Texture('someImage.jpg', 100, 100)); // Create objects etc. game['layers']['default'].add(new Foundation.Tile( new Foundation.Rectangle(0, 0, 100, 100), game.textures.getRegion('all') )); // Start the game when all textures have finished loading game.autoStart();
This type extends:
No base methods are overridden.
This type does not implement any interfaces (except those in the base).
- A game singleton reference to the last created game instance. This is used by debugging types for logging and profiling, but is otherwise ignored for production code.
- Defines the maximum width of the game world. Drawing outside of world bounds is not restricted (the world is technically infinite), but some optional inbuilt functionality will make use of this number (the camera, for example, can be restricted to these bounds).
- Defines the maximum height of the game world. Drawing outside of world bounds is not restricted (the world is technically infinite), but some optional inbuilt functionality will make use of this number (the camera, for example, can be restricted to these bounds).
- The HTML element that is used as a container for the game (rendering canvases, screens, etc.) For full screen games,
document.bodyis a good container. For non full screen games, a div is preferable. This div can be sized as needed via CSS, the game's view port will be scaled to fit.
- A convenience array to hold loaded textures. Textures can, of course, be stored in other places, but the
autoStart()method (see below) will only take textures stored here into consideration. If storing textures elsewhere, a reference to each texture should also be added to this collection if using the
- Used to hold a reference to a
Loggerinstance. If non-null, game logging will be available through the game singleton.
- Used to hold a reference to a
TimeSlotProfilerinstance. If non-null, game time profiling will happen automatically on the game singleton.
- Returns true if the game is not currently running
- Note: not the same as the game being inactive.
active === falsemay mean the game is paused, this
=== falsemeans the game loop is NOT running.
(worldWidth: number = 800, worldHeight: number = 480, HTMLContainer: HTMLElement = document.body, log: boolean = false, profile: boolean = false)
- Creates a new game instance and sets it as the game singleton.
worldWidthSee property description. Defaults to
worldHeightSee property description. Defaults to
HTMLContainerSee property description. Defaults to
trueto add a
Loggerinstance to the game. Defaults to
trueto add a
TimeSlotProfilerinstance to the game. Defaults to
- Starts the game loop running.
- A callback is not strictly necessary here as the game will be immediately (synchronously) started. However, a callback here will be called with it's
thiscontext being the game, which may be of some use.
autoStart(callback?: Function): void
start()when, and only when, all textures stored in
readystate. I.e. will start the game when all textures are ready to draw.
- Pauses update calls in the game loop. The game loop will continue to run (game life time will accumulate), but no actors will be updated.
- The game can be resumed with a
- Like pause, but fully stops the game loop.
Game Prefab Factory
createDefaultPrefab(worldWidth: number = 800, worldHeight: number = 480, HTMLContainer: HTMLElement = document.body, log: boolean = false,profile: boolean = false, load: Function = null)
- Creates a game instance with a predefined set of components. The following components are available on the returned instance, in order:
Layerinstances. Only 1 layer is added with the key
active === falseby default
loadparameter should now be considered deprecated (>= 0.8.1). This function will create and return a game instance synchronously. The object returned is a
DefaultGamePrefabinstance. This is simply a
Gamesubclass with typed getters for the above components.