Jump to content

Extending Sprite and damage();


j0hnskot
 Share

Recommended Posts

Hello there! I extended the sprite class, everything seems to work normal. 

But when i call "damage(1)" its health doesn't get reduced. 

If i use .health-=1; it reduces its health but i need the functionality of damage().

 

Am i doing something wrong?

That's the code i'm using to extend the sprite class : 

Enemy = function (game){		var x=((Math.random() * 400)+1); ;	var y=-120;		Phaser.Sprite.call(this, game, x, y, 'enemy_ship');	game.physics.arcade.enable(this);		this.rateOfFire=250	this.lastTimeFired=0;	this.health=3;	this.body.velocity.y=40;	this.checkWorldBounds=true;    this.outOfBoundsKill= true;}Enemy.prototype = Object.create(Phaser.Sprite.prototype);Enemy.prototype.constructor = Enemy;

Thanks in advance!

Link to comment
Share on other sites

It looks like the 'alive' property on Phaser.Sprite is only set if the sprite is revived (true), killed (false) or reset (true) within the object itself, and only when Group.create is used does the property get set outside. This means when extending the Sprite object, because you instantiate it manually, the property is never created. Looks like it may have been a small oversight.

Link to comment
Share on other sites

  • 5 months later...

Hi,

 

I'm not trying to extend the Sprite class, I just used it as is.   I create a Sprite and assign health to be 100.   Later, in my collision handler I call damage(25).   Instead of of having 75 health, the sprite at this point dies.   I used Firebug to debug and my sprite's health is set to 100 but in the collision handler health is just 1, even before I call damage().   It looks like Phaser itself is setting health to 1.   Is this a bug or is there something about how health and damage works in Phaser that I'm not understanding?

Link to comment
Share on other sites

Sure.  

function createPlayer()
{
    player = game.add.sprite( playerStartX, playerStartY, 'playerFighter' );
    player.health = 100;
    game.physics.enable( player, Phaser.Physics.ARCADE );
    player.animations.add( 'fly' );
    //  30 is the frame rate (30fps)
    //  true means it will loop when it finishes
    player.animations.play( 'fly', 30, true );
    player.body.collideWorldBounds=true;
    //Create Player fire while we're at it
    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    game.physics.enable( bullets, Phaser.Physics.ARCADE );
    bullets.createMultiple(30, 'blueBullet' );
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);
}
 

 

function update()
{
    enemyActions();
        
    if( player.alive )
    {
        //text.setText( "Hull Strength: " + player.health + " - Score: " + score );
        text.setText( "Score: " + score );
        keyboardHandler();
        game.physics.arcade.overlap( enemyBullets, player, playerHit, null, this );
        game.physics.arcade.overlap( enemyGroup, player, playerHit, null, this );
    } else {       
        text.setText( "You're DEAD Final- Score: " + score + " Click RESTART to start over" );      
    }
    game.physics.arcade.overlap( enemyBullets, bullets, bulletsMeet, null, this );
    
    if( enemyGroup.countLiving() == 0 )
    {
        enemiesinWave++;
        createEnemies();
    }
}
 

 

function playerHit( bullet, player )
{
    bullet.kill();
    player.damage( 25 );
    explodeAt( player.x, player.y, false );
    playerExplodeSound.play();
}

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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