Jump to content

To extend or not to extend Phaser.Sprite

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 post
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.



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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...