saltemishel Posted March 17, 2018 Share Posted March 17, 2018 Hi all, I would like to extend the game state like this method to extend the entity so I don't have to keep writing the same function on each state. Phaser.Sprite.prototype.something=function(t){} I tried Phaser.Game.prototype and Phaser.State.prototype but it's not working, what's the right word? Thanks! Link to comment Share on other sites More sharing options...
Adel Posted March 17, 2018 Share Posted March 17, 2018 Phaser.State State = function () { Phaser.State.call(this); }; State.prototype = Object.create(Phaser.State.prototype); /** @type {State} */ State.prototype.constructor = State; State.prototype.init = function () { }; State.prototype.preload = function () { }; State.prototype.create = function () { }; /****************************************************************/ window.onload = function () { const game = new Phaser.Game(360, 640, Phaser.AUTO); game.state.add('State', new State()); game.state.start('State', true, false); }; /*********************************************************************/ // es2015 class State extends Phaser.State { constructor() { super(); } init() { } preload() { } create() { } } export { State }; Link to comment Share on other sites More sharing options...
samme Posted March 17, 2018 Share Posted March 17, 2018 Link to comment Share on other sites More sharing options...
saltemishel Posted March 18, 2018 Author Share Posted March 18, 2018 Thanks both, I combined both of your methods and it works now. There sure are many ways to write this. I'm wondering if there's a way to write this State = function () { Phaser.State.call(this); }; State.prototype = Object.create(Phaser.State.prototype); /** @type {State} */ State.prototype.constructor = State; State.prototype.init = function () { }; Using this way below so I don't need to keep writing the prototype.something=function? BasicGame.Game.prototype = { create: function() { this.createDude(); }, render: function() { var debug = this.game.debug; debug.phaser(10, 580); } }; Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts