Jump to content

Phaser creating state in same file typescript error


lifesuxtr
 Share

Recommended Posts

I am trying to create a gamestate in phaser.
The problem is typescript is not allowing me to do it by this
 Iam triying to put all functions into 1 state then call that state.

    let gameState = {
    //preload update create functions here
      
    };

After initializing game

    this.game.state.add('game', gameState);

Then call state in subscribe 

this.game.state.start('gameState');

The problem is typescript doesnt let me to create a state with above let gameState structure.It says Unexpected token. A constructor, method, accessor, or property was expected.

Link to comment
Share on other sites

  • 2 weeks later...

You are using Typescript; Why not make use of the awesome things it adds to JS? :)

Take a look at https://github.com/gaurav-dixitv/PhaserPostProcessing/tree/master/src for a complete project template that uses Typescript and Phaser.

You could create a state (boot.ts) like so:

export class Boot extends Phaser.State {
 
    public preload(): void {
    }
 
    /**
     * Use this to set global properties such as resolution, aspect ratio, et cetera.
     * 
     * 
     * @memberOf Boot
     */
    public create(): void {
 
        this.input.maxPointers = 1//multi-touch needed?
        this.stage.disableVisibilityChange = false;
 
        this.game.scale.pageAlignHorizontally = true;
        this.game.scale.pageAlignVertically = true;
 
        if (this.game.device.desktop) {
            //Desktop settings
        }
        else {
            this.game.scale.forceOrientation(truefalse);
            this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL
        }
        /*if (DEBUG) {
            console.log("Done booting up!");
            console.log("Revision : " + GIT_REVISION);
        }*/
    }
}

 

Then,

 
import { Boot } from './states/boot'
 
 
export class Game extends Phaser.Game {
 
    /**
     * Creates an instance of Game.
     * @param {Phaser.IGameConfig} config 
     * 
     * @memberOf Game
     */
    constructor(config: Phaser.IGameConfig) {
        super(config);
 
        //Add states
        this.state.add('Boot'new Boot(), false);
    }
 
    /**
     * Boots game by starting the boot state.
     * 
     * @memberOf Game
     */
    public bootGame(): void {
        //Boot
        this.state.start('Boot');
    }
}
 
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...