Jump to content

Help extending from extension to Phaser Class.


charlie_says
 Share

Recommended Posts

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

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

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

 Share

  • Recently Browsing   0 members

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