Noid Posted May 16, 2014 Share Posted May 16, 2014 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.SpritePichon.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 More sharing options...
gabehollombe Posted May 16, 2014 Share Posted May 16, 2014 I've opted for having a private `sprite` var on my own game model objects. I like this approach because it's easy for me to separate Phaser logic and settings from my own object's concerns. plicatibu 1 Link to comment Share on other sites More sharing options...
sleekdigital Posted May 16, 2014 Share Posted May 16, 2014 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 More sharing options...
rich Posted May 16, 2014 Share Posted May 16, 2014 Yup, I personally always use inheritance. Link to comment Share on other sites More sharing options...
Recommended Posts