Jump to content

To extend or not to extend Phaser.Sprite


Noid
 Share

Recommended Posts

Which of this two options(if any) do you think is better for handling a sprite?

 

Option A - Extending Phaser.Sprite:

 

Pichon.Player2 = function (game, x, y, sprite) {    Phaser.Sprite.call(this, game, x, y, sprite);    game.physics.enable(this, Phaser.Physics.ARCADE);    this.body.bounce.y = 0.2;    this.body.maxVelocity = new Phaser.Point(300,800);    this.body.collideWorldBounds = true;    this.body.setSize(30,30, 13,15);    this.frameName = 'PonPon-00';    //...    game.add.existing(this);};Pichon.Player2.prototype = Object.create(Phaser.Sprite.prototype);Pichon.Player2.constructor = Pichon.Player2;Pichon.Player2.prototype.update = function() {    //Update code here};

Option B - private var _sprite containing a Phaser.Sprite

Pichon.Player = function (game, x, y, sprite) {    var _sprite = game.add.sprite(x, y, sprite);    game.physics.enable(_sprite, Phaser.Physics.ARCADE);    _sprite.body.bounce.y = 0.2;    _sprite.body.maxVelocity = new Phaser.Point(300,800);    _sprite.body.collideWorldBounds = true;    _sprite.body.setSize(30,30, 13,15);    _sprite.frameName = 'PonPon-00';    //...     return {        get _sprite () {            return _sprite;         },        update: function() {            //update code here        }    };};

 

Link to comment
Share on other sites

Option A = inheritance, Option B = composition.  This topic has been discussed to death since the birth of OOP, so you will find plenty of resources out there.  Most of the general OOP concepts related to this topic can be applied to game programming and Phaser.

https://www.google.com/#q=favor+composition+over+inheritance

 

In this specific case, it probably won't make much difference.  When you start getting into more complex structures, then it can make a world of difference.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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