jmp909 Posted September 29, 2015 Share Posted September 29, 2015 Hi, I'm having trouble with TypeScript when trying to create a custom preloader function/graphic does anybody know a solution please?thanksjclass 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 More sharing options...
jmp909 Posted September 29, 2015 Author Share Posted September 29, 2015 i was not binding "this" this makes the code above work now.. is it the correct approach? thanksjclass 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 More sharing options...
Tom Atom Posted September 29, 2015 Share Posted September 29, 2015 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 jmp909, clark, arialblack14 and 1 other 4 Link to comment Share on other sites More sharing options...
jmp909 Posted October 1, 2015 Author Share Posted October 1, 2015 thanks Tom Link to comment Share on other sites More sharing options...
Recommended Posts