charlie_says Posted February 22, 2017 Share Posted February 22, 2017 Following this example: https://phaser.io/examples/v2/sprites/extending-sprite-demo-1 I've been trying to extend MonsterBunny. I've added this: // here is an extension of a custom game object SuperMonsterBunny = function (game, x, y, rotateSpeed, vertSpeed) { MonsterBunny.call(this, game, x, y, rotateSpeed); this.vertSpeed = vertSpeed; this.ydir = 1; }; SuperMonsterBunny.prototype = Object.create(MonsterBunny.prototype); SuperMonsterBunny.prototype.constructor = SuperMonsterBunny; SuperMonsterBunny.prototype.update = function() { /* this.y += this.vertSpeed * this.ydir; if (this.y < 0 || this.y > h) { this.ydir *= -1; } */ }; This works fine, and the commented out code works as expected. (when it's uncommented!) The extended class's update overrides the parent class, which is expected, but, I can't find a way of calling super.update() or equivalent. Can anyone enlighten me? Link to comment Share on other sites More sharing options...
charlie_says Posted February 22, 2017 Author Share Posted February 22, 2017 I did think that this.__proto__.update(); would do it, but, apparently not. <edit> A little further investigation shows that this does call the parent update, but for some reason doesn't action the rotation... Link to comment Share on other sites More sharing options...
samme Posted February 22, 2017 Share Posted February 22, 2017 [wrong code removed] Link to comment Share on other sites More sharing options...
charlie_says Posted February 22, 2017 Author Share Posted February 22, 2017 I couldn't get either approach to work I liked the second one, which seems a little more 'correct' to me, but if I checked what this.super was, it was Sprite rather than MonsterBunny. Link to comment Share on other sites More sharing options...
charlie_says Posted February 22, 2017 Author Share Posted February 22, 2017 So it's clear, this is the code: // Here is a custom game object MonsterBunny = function (game, x, y, rotateSpeed) { Phaser.Sprite.call(this, game, x, y, 'item1'); this.angle = 90; this.rotateSpeed = rotateSpeed; }; MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype); MonsterBunny.prototype.constructor = MonsterBunny; /** * Automatically called by World.update */ MonsterBunny.prototype.update = function() { this.angle += this.rotateSpeed; console.log(this.angle, this.rotateSpeed); }; // here is an extension of a custom game object SuperMonsterBunny = function (game, x, y, rotateSpeed, vertSpeed) { MonsterBunny.call(this, game, x, y, rotateSpeed); this.vertSpeed = vertSpeed; this.ydir = 1; }; SuperMonsterBunny.prototype = Object.create(MonsterBunny.prototype); SuperMonsterBunny.prototype.constructor = SuperMonsterBunny; SuperMonsterBunny.prototype.super = MonsterBunny.prototype; SuperMonsterBunny.prototype.update = function() { this.super.update(); }; As you can see I've tried checking the values of this.angle, this.rotateSpeed which are NaN and undefined. Link to comment Share on other sites More sharing options...
charlie_says Posted February 22, 2017 Author Share Posted February 22, 2017 Ahh, got it: this.super.update.call(this); this.super.update.call(this); does what I expected. Thanks @samme for pointing me in the right direction! Link to comment Share on other sites More sharing options...
samme Posted February 22, 2017 Share Posted February 22, 2017 Argh, yes, that was supposed to be // […] SuperMonsterBunny.prototype.super = MonsterBunny.prototype; SuperMonsterBunny.prototype.update = function() { this.super.update.call(this, arguments); }; // OR: SuperMonsterBunny.prototype.update = function() { MonsterBunny.prototype.update.call(this, arguments); }; I'd left out `update`. Link to comment Share on other sites More sharing options...
Recommended Posts