darcys22 Posted November 7, 2014 Share Posted November 7, 2014 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 referencevar 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 More sharing options...
eguneys Posted November 7, 2014 Share Posted November 7, 2014 You can call it anywhere, but I have no idea why we have to call it and how this all works, the code looks fine, are you still getting the error? Link to comment Share on other sites More sharing options...
saibotlive Posted November 8, 2014 Share Posted November 8, 2014 You probably need to pass the game object instance in first like: var Shift = function(game, ctx, hour) Link to comment Share on other sites More sharing options...
lewster32 Posted November 8, 2014 Share Posted November 8, 2014 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. darcys22 1 Link to comment Share on other sites More sharing options...
darcys22 Posted November 10, 2014 Author Share Posted November 10, 2014 Ah figured it out. Because I was trying to set this.height which trigged a calculation by the Sprite class it was upset that I hadn't made the call yet. Thanks all Link to comment Share on other sites More sharing options...
Recommended Posts