Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Game

The bootstrap and entrypoint for a PIXI game. It does the following things in order:

  1. Creates a Pixi instance inside the element specified in config.gameContainerId.
  2. Register mouse and keyboard listeners to grab input.
  3. Setups loading screen.
  4. Calls a custom callback to allow you to queue all the assets you need for loading.
  5. Loads all the queued assets.
  6. Removes loading screen.
  7. Initializes the first scene (like a screen of your game: main menu, game itself, game over, leaderboards, etc)
  8. Connects to the ticker that updates the active scene.
  9. Adds all other necessary listeners - changing game dimensions, updating input etc.

Example configuration

const game = new Game({
    // `document` and `window` are required to be passed to the configuration
    document,
    window,

    // ID of the element where the PIXI's canvas should be added
    gameContainerId: 'game',

    // optional PIXI configuration, at the very least you want to provide width and height
    pixiConfig: {
        width: 640,
        height: 360,
        backgroundColor: 0,
        antialias: false,
    },

    // Creates and configures a stage which scales the game pixel-perfect, snapping to full integers of scale,
    // ie. x1, x2, x3, for a crisp pixel look. It also handles all of the magic required for Pixi's
    // interactions to work and for `MouseInput` library to report proper position
    stageFactory: (game: Game) => new ScalingStage(
        game,
        640,
        360,
        PIXI.SCALE_MODES.NEAREST,
        Config.containerUpscaleMode,
    ),

    // Queue some assets to be loaded
    onQueueAssets: (game: Game): void {
        game.assetLoader.queuePixiAutoFont('topaz_0.png', FontTopaz8Image);
        game.assetLoader.queuePixiAutoFont('font-topaz', FontTopaz8);
        game.assetLoader.queueTexture(GfxConstants.InitialTileset, InitialTileset);
        game.assetLoader.queueTileset(GfxConstants.InitialTileset, {
            tileWidth: 16,
            tileHeight: 16,
            offsetX: 0,
            offsetY: 0,
            spacingX: 0,
            spacingY: 0,
        })
    },

    // Callback to create the starting scene
    initialSceneFactory: (game) => new IntroScene(game),

    // Called once everything has finished initializing
    onStartGame: () => {
        console.log("Game started");
    },
);

// Actually starts the initialization of the game
game.start();

Hierarchy

  • Game

Index

Constructors

constructor

Properties

assetLoader

assetLoader: AssetLoader

document

document: Document

gameStage

gameStage: GameStage

keyboard

keyboard: KeyboardInput

mouse

mouse: MouseInput

pixi

pixi: Application

postProcessManager

postProcessManager: PostProcessEffects

sceneManager

sceneManager: SceneManager

textureStore

textureStore: TextureStore

Methods

createContainer

  • Cretes a new PIXI.Sprite on the specified layer. This new sprite will be attached to the GameStage, and can be used to actually display something on the screen. See GameStageLayer for more information about the types of layers.

    Parameters

    • Default value layer: GameStageLayer = GameStageLayer.Normal

      Layer on which to create the container.

    Returns Sprite

    The sprite created and attached to stage.

removeContainer

  • removeContainer(container: Sprite): void
  • Removes a container previously created by the call to createContainer from the stage. Use it when it's no longer in use, typicall when switching from a Scene.

    Parameters

    • container: Sprite

      The contianer to remove

    Returns void

start

  • start(): void
  • Initializes and runs the game.

    Returns void

Generated using TypeDoc