Sign in to follow this  
saltemishel

How to extend state

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!

 

Share this post


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

 

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.