Jump to content

Sprite Inheritance Question


Recommended Posts

I have been getting this error:



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

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