Jump to content

TypeScript preload onFileComplete - Error: Phaser.Signal: listener is a required param of add()


jmp909
 Share

Recommended Posts

Hi,

 

I'm having trouble with TypeScript when trying to create a custom preloader function/graphic

 

does anybody know a solution please?

thanks

j

class Main {   // onFileComplete can't find this function   myLoadUpdate() {        console.log("myLoadUpdate");   }   preload () {               this.game.load.image("star", "assets/star.png");        this.game.load.onFileComplete.add(this.myLoadUpdate, this);        // => Uncaught Error: Phaser.Signal: listener is a required param of add() and should be a Function.   }}
Link to comment
Share on other sites

i was not binding "this"

 

this makes the code above work now.. is it the correct approach?

 

thanks

j

class Main {     constructor() {        this.game = new Phaser.Game(800, 600, Phaser.AUTO, 'content', { preload: this.preload.bind(this), create: this.create.bind(this) }, false, true);    }    myLoadUpdate()  { // etc }    preload () { // etc }}
Link to comment
Share on other sites

Hi, here is Typescript skeleton I use - each state has its own class (and file). Use types - this is one great thing Typescript gives you: it helps you to find errors and prevents you from doing them:

// -------------------------------------------------------------------------// -------------------------------------------------------------------------// -------------------------------------------------------------------------window.onload = () => {     new MyGame.Game();};// -------------------------------------------------------------------------// -------------------------------------------------------------------------// -------------------------------------------------------------------------module MyGame {    export class Game extends Phaser.Game {        // -------------------------------------------------------------------------        constructor() {            // init game            super(800, 480, Phaser.AUTO, "content", null /* , transparent, antialias, physicsConfig */);                        // states            this.state.add("Boot", Boot);            this.state.add("Preloader", Preloader);            this.state.add("Menu", Menu);            this.state.add("Levels", Levels);            this.state.add("Play", Play);            // start            this.state.start("Boot");        }    }}// -------------------------------------------------------------------------// -------------------------------------------------------------------------// -------------------------------------------------------------------------module MyGame {    export class Boot extends Phaser.State {        // -------------------------------------------------------------------------        constructor() {            super();                :                :        }        // -------------------------------------------------------------------------        init() {            if (this.game.device.desktop) {                :                :            }            else {                :                :            }        }        // -------------------------------------------------------------------------        preload() {                :                :        }        // -------------------------------------------------------------------------        create() {                :                :        }        // -------------------------------------------------------------------------        update() {                :                :        }    }}// same classes for other game states like "Boot" class (... usually without init() method)

Look at my small tutorial for simple Phaser Typescript game here: http://sbcgamesdev.blogspot.cz/2015/05/phaser-tutorial-dronshooter-simple-game_23.html

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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