Jump to content

TypeScript, states problem


Shana
 Share

Recommended Posts

Hi guys, it's me again \o .

 

So, I have another problem, i worked on it for 3-4 hours yesterday, and I give up...

 

So, my problem is simply : I can't add state, or simply call it.

 

So, my app.ts (Yhea, I'm following the tutorial) :

/// <reference path="phaser.d.ts" />/// <reference path="Boot.ts" />/// <reference path="Preloader.ts" />/// <reference path="MainMenu.ts" />module Castlevania {    export class MainGame {        game: Phaser.Game;        constructor() {            this.game = new Phaser.Game(800, 600, Phaser.AUTO, 'content', { preload: this.preload, create: this.create });                    }        preload() {        }        create() {            this.game.state.add("boot", Castlevania.Boot, true);            console.log(this.game.state);        }    }}window.onload = () => {    var game = new Castlevania.MainGame();};

My Boot.ts :

/// <reference path="phaser.d.ts" />module Castlevania {    export class Boot extends Phaser.State {        constructor() {            super();        }        preload() {            console.log("IT WORKS !!!");            //this.game.load.image('preloadBar', 'assets/loader.png');        }        create() {            console.log("IT WORKS!!!");        }        update() {        }    }}

(Yhea, I tried to put a constructor, but it doesn't work \o )

 

 

So, in my log of state, I have :

Phaser.StateManager - No state found with the key: boot phaser.js:9225Phaser.StateManager_created: true_pendingState: ObjectcallbackContext: Objectcurrent: "default"game: Phaser.GameonCreateCallback: function () {onInitCallback: function () {onLoadRenderCallback: nullonLoadUpdateCallback: nullonPausedCallback: nullonPreRenderCallback: nullonPreloadCallback: function () {onRenderCallback: nullonShutDownCallback: function () {onUpdateCallback: nullstates: Objectboot: undefineddefault: Object__proto__: Object__proto__: Object 

So, now I'm lost. I tried everything, but nothing works...

Can u help me ?

 

Thanks :) .

 

 

 

 

 

 

EDIT :

 

When I try this : 

console.log("TEST");var b = new Castlevania.Boot();console.log("TEST2");console.log(;console.log("TEST3");this.game.state.add("boot", b, true);console.log(this.game.state);

I have :

TEST app.ts:21Uncaught TypeError: undefined is not a function 
Link to comment
Share on other sites

I'm not sure why your code fails, but I'll show you what works for me:

 

/states/Boot.ts

module WM.States {    export class Boot extends Phaser.State {        constructor() {            super();        }        render() {        }        create() {            this.game.state.start("preload", true, false);        }        update() {        }    }}

/states/Preload.ts

module WM.States {    export class Preload extends Phaser.State {               constructor() {            super();                    }        preload() {        }        render() {                   }        update() {                  }    }}

/Game.ts

module WM {    export class Main extends Phaser.Game {               constructor() {                        super(800, 600, Phaser.CANVAS, "bla");            this.state.add("boot", States.Boot);            this.state.add("preload", States.Preload);            this.state.start("boot");        }    }}var wm:WM.Main;window.onload = () => {    wm = new WM.Main(); };

In addition to that i have a _reference.ts in the root which contains something like:

 

/_reference.ts

/// <reference path="states/boot.ts" />/// <reference path="states/preload.ts" />

But this might be specific to how i compile typescript. This allows me to make sure that everything gets referenced. Just place the files you call in the right order and if needed you place a reference like

/// <reference path="../_reference.ts" />

above the class where you use other classes.

I'll write a tutorial about my approach as soon as I have everything working as needed (waiting for Phaser 2.0 and it's typescript definitions).

Hope this helps you.

Link to comment
Share on other sites

Yhea, I'm simply following the tutorial (TypeScript & Phaser so), and I'm in the second part.

 

So I did the same as the tutorial, with the modules and the states.

Well, when I do "this.game.state.add("thing", Castlevania.Boot, false);" (So, the same line), I have this error :


  1. Phaser.StateManager - No state found with the key: boot phaser.js:9225Phaser.StateManager_created: true_pendingState: ObjectcallbackContext: Objectcurrent: "default"game: Phaser.GameonCreateCallback: function () {onInitCallback: function () {onLoadRenderCallback: nullonLoadUpdateCallback: nullonPausedCallback: nullonPreRenderCallback: nullonPreloadCallback: function () {onRenderCallback: nullonShutDownCallback: function () {onUpdateCallback: nullstates: Objectboot: undefineddefault: Object__proto__: Object__proto__: Object

So I've tried to instanciate this class :

console.log("TEST");var b = new Castlevania.Boot();console.log("TEST2");console.log(;console.log("TEST3");this.game.state.add("boot", b, true);console.log(this.game.state);

And it fails at "var b = new Castlevania.Boot();", and it gives me that error :

TEST app.ts:21Uncaught TypeError: undefined is not a function 

I tried everything, but nothing works, even if I copy and paste the same code as above.

Link to comment
Share on other sites

I cannot see where you are starting the state. 

CheshirePuss42's code looks good to me. 

It appears you are trying to create States and call them by constructor instead of his example?

this.state.start("boot");

Link to comment
Share on other sites

  • 1 month later...
  • 7 months later...
From my point of view problem is not in "Phaser".  

 

You  created a new typescript file Boot.ts. It means, you split your module across files. In this case just make sure all of the compiled code gets loaded.  

 


  1. Uncaught TypeError: undefined is not a function

 

It looks like you forget about it. <script src="Boot.js" type="text/javascript" />. 

 

You can read about it here

 

Hope it will helps somebody : )

Link to comment
Share on other sites

Not very likely, that someone who had this problem in april is still stuck on it ;)

 

But you are most likely correct.

 

It happens to me all the time when I add new files to my project.. I define a new class in the file, I add the "reference" statement to the TS-file where I want to use it.. and then wonder why the game crashes in the browser. after a bit of wondering I add the resulting .js from the new file do my debug.index.html and everything works fine.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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