Jump to content

Sprite Inheritance Question


darcys22
 Share

Recommended Posts

I have been getting this error:

http://www.html5gamedevs.com/topic/10129-213-problem-with-texture-displayobjecttexture-is-undefined/

 

and I narrowed it down to a class that inherits from Sprite.

 

In the examples http://examples.phaser.io/_site/view_full.html?d=sprites&f=extending+sprite+demo+2.js&t=extending%20sprite%20demo%202

 

The first thing done in the constructor is Phaser.Sprite.call(). Is it important that this is done straight away?

I was putting it in a function and calling it at the end once I had made a few calculations.

 

Here is the code for reference

var Shift = function(ctx, hour) {    this.position = hour;    this.length = 8;    this.ctx = ctx;    this.height = this.calculateHeight();    this.createSprite();  };Shift.prototype = Object.create(Phaser.Sprite.prototype);Shift.prototype.constructor = Shift;Shift.prototype.createSprite = function() {  var bmd = this.ctx.game.add.bitmapData(this.position * Shift.SHIFT_SIZE, Shift.SHIFT_HEIGHT);  bmd.context.fillStyle = 'rgba(255, 0, 0, 0.3)';  Helpers.RoundRect(bmd.ctx, 0, 0, bmd.width, bmd.height, 5, true);  Phaser.Sprite.call(this, this.ctx.game, this.xpos,this.ypos,bmd);};
Link to comment
Share on other sites

The Phaser.Sprite.call line calls the 'constructor' of the original Sprite object (the Phaser.Sprite function itself) which allows your extended object to pass the required initialisation parameters back to the original object. You typically want this in the constructor of your extended object, but you can calculate things beforehand so long as they don't rely on things set up by the inherited object's constructor. You should look and see what Sprite's constructor sets up to determine what's getting initialised where. Some knowledge of how JavaScript's prototype chain comes in handy here.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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