Jump to content

Object Inherintance in Phaser?


notalentgeek
 Share

Recommended Posts

Inheritance is a feature of Javascript. So its hard to make anything that doesn't support it (probably impossible). Without knowing too much about how Phaser is suppose to work I can tell you that it uses Inheritance all over the place. You can inherit from it as well. 

 

If you wanted to create a new type of Sprite for example you would do something like this :
var MySpriteType = function(attr){    this.x = 500;    this.y = 200;}MySpriteType.prototype = Object.create(Phaser.Sprite.prototype);MySpriteType.constructor = MySpriteType;

I based my pattern on what Phaser is doing, since it inherits a lot from Pixi.js

 

This is how Phaser extends/inherits Pixi's Sprite type/class:

Phaser.Sprite = function (game, x, y, key, frame) {    x = x || 0;    y = y || 0;    //... a bunch of other code ...}Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);Phaser.Sprite.prototype.constructor = Phaser.Sprite;
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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