Sign in to follow this  
jmp909

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

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.   }}

Share this post


Link to post
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 }}

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.