Jump to content

Extending an extended class


pWEN
 Share

Recommended Posts

Hello! I'm having a hell of a time trying to create different bullet types in my game (and this problem extends to different weapon types, enemy types, etc.). I know how to extend Sprite, and I have done so to make a base bullet class. However, I can't seem to figure out how to extend that class to make bullets that behave differently. For example: I have a weapon of type Pistol that will shoot bullets of type BulletPistol, and a weapon of RocketLauncher that will shoot bullets of type BulletRocket. The form is like a typical bullet, while the latter will start moving slowly, then accelerate. My thinking is that for the base Bullet class, I can put in all of the default behavior. For BulletPistol, and any other weapons that use this behavior, I merely pass in the sprite I want to use. For BulletRocket, I will need to override the update function (at least) to change how the rocket moves.

Here is the code in my Bullet.js file:

var Bullet = function(game, key)
{
    console.log("Bullet.prototype");
    Phaser.Sprite.call(this, game, 0, 0, key);

    this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;

    this.anchor.set(0.5);

    this.checkWorldBounds = true;
    this.outOfBoundsKill = true;
    this.exists = false;

    this.tracking = false;
    this.scaleSpeed = 0;
};

Bullet.prototype = Object.create(Phaser.Sprite.prototype);
Bullet.prototype.constructor = Bullet;

Bullet.prototype.fire = function(x, y, angle, speed, gx, gy)
{
    gx = gx || 0;
    gy = gy || 0;

    this.reset(x, y);
    this.scale.set(1);

    this.game.physics.arcade.velocityFromAngle(angle, speed, this.body.velocity);

    this.angle = angle;
};

Bullet.prototype.update = function()
{
    console.log("Bullet.prototype.update");
    if (this.tracking)
    {
        this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x);
    }

    if (this.scaleSpeed > 0)
    {
        this.scale.x += this.scaleSpeed;
        this.scale.y += this.scaleSpeed;
    }
};

////////////////////////////////////////////////////////////
//    PISTOL
////////////////////////////////////////////////////////////

var BulletPistol = function(game)
{
    console.log("Bullet.Pistol.prototype");
    Bullet.call(game, 'img_BulletPistol');
};

BulletPistol.prototype = Object.create(Bullet.prototype);
BulletPistol.prototype.constructor = BulletPistol;

BulletPistol.prototype.fire = function(x, y, angle, speed, gx, gy)
{
    //
};

BulletPistol.prototype.update = function()
{
    console.log("Bullet.Pistol.prototype.update");
};

Here are the error messages and console logs I get when I load my game:

Quote

Weapon.Pistol constructor 

Weapon.Pistol.prototype.addBullets() 

Bullet.Pistol.prototype

Bullet.prototype 

Uncaught TypeError: this.onTextureUpdate is not a function

Uncaught TypeError: this.scale.preUpdate is not a function

Uncaught TypeError: Cannot read property 'x' of undefined

Uncaught TypeError: Cannot read property 'disableVisibilityChange' of null

I'm not sure what I'm doing wrong, exactly. I went through 11 pages on this forum and several more on Google, but the closest things I could find to what I'm attempting were written in other languages. Any insight on how to extend an extended class is greatly appreciated!

 

Link to comment
Share on other sites

You do it the way you're doing it now, but remember to have 'this' as the first argument in call, even in sub-classes:

var BulletPistol = function(game)
{
    console.log("Bullet.Pistol.prototype");
    Bullet.call(this, game, 'img_BulletPistol');
};

Also remember that if you have a function called 'update' in your BulletPistol class, it will replace the Bullet.update function on that instance. So if you don't need it, don't include it. If you do need it, you need to call the parent update function in the over-ridden one.

Link to comment
Share on other sites

Ah, so simple! Thank you, it works now! Here's my updated code, including calls to parent functions.

var Bullet = function(game, key)
{
    console.log("Bullet.prototype");
    Phaser.Sprite.call(this, game, 0, 0, key);

    this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;

    this.anchor.set(0.5);

    this.checkWorldBounds = true;
    this.outOfBoundsKill = true;
    this.exists = false;

    this.tracking = false;
    this.scaleSpeed = 0;
};

Bullet.prototype = Object.create(Phaser.Sprite.prototype);
Bullet.prototype.constructor = Bullet;

Bullet.prototype.fire = function(x, y, angle, speed, gx, gy)
{
    console.log("Bullet.prototype.fire() - x: "+x+", y: "+y+", angle: "+angle+", speed: "+speed+", gx: "+gx+", gy: "+gy);
    gx = gx || 0;
    gy = gy || 0;

    this.reset(x, y);
    this.scale.set(1);

    this.game.physics.arcade.velocityFromAngle(angle, speed, this.body.velocity);

    this.angle = angle;
};

Bullet.prototype.update = function()
{
    //console.log("Bullet.prototype.update");
    if (this.tracking)
    {
        this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x);
    }

    if (this.scaleSpeed > 0)
    {
        this.scale.x += this.scaleSpeed;
        this.scale.y += this.scaleSpeed;
    }
};

////////////////////////////////////////////////////////////
//    PISTOL
////////////////////////////////////////////////////////////

var BulletPistol = function(game)
{
    console.log("Bullet.Pistol.prototype");
    Bullet.call(this, game, 'img_BulletPistol');
};

BulletPistol.prototype = Object.create(Bullet.prototype);
BulletPistol.prototype.constructor = BulletPistol;

BulletPistol.prototype.fire = function(x, y, angle, speed, gx, gy)
{
    Bullet.prototype.fire.call(this, x, y, angle, speed, gx, gy);
};

BulletPistol.prototype.update = function()
{
    Bullet.prototype.update.call(this);
};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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