Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

How to extend state


saltemishel
 Share

Recommended Posts

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

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

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

 Share

  • Recently Browsing   0 members

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