j0hnskot Posted May 21, 2014 Share Posted May 21, 2014 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 More sharing options...
j0hnskot Posted May 21, 2014 Author Share Posted May 21, 2014 Just found out that the damage() method checks first if the sprite.alive is true. I fixed the problem by this.alive=true. But my question now is why this property does not exists at all? What am i missing? Link to comment Share on other sites More sharing options...
lewster32 Posted May 21, 2014 Share Posted May 21, 2014 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 More sharing options...
j0hnskot Posted May 21, 2014 Author Share Posted May 21, 2014 Thanks, that clears it up! Link to comment Share on other sites More sharing options...
rich Posted May 21, 2014 Share Posted May 21, 2014 Yup I just came to a similar conclusion. I've checked the property list and it's the only one missing that I can see, everything is set properly (visible, exists, etc). Now fixed in dev branch Link to comment Share on other sites More sharing options...
j0hnskot Posted May 21, 2014 Author Share Posted May 21, 2014 Thank you rich! Link to comment Share on other sites More sharing options...
honorthrawn Posted October 26, 2014 Share Posted October 26, 2014 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 More sharing options...
j0hnskot Posted October 26, 2014 Author Share Posted October 26, 2014 Can you give us some relevant code? Link to comment Share on other sites More sharing options...
honorthrawn Posted October 26, 2014 Share Posted October 26, 2014 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 More sharing options...
j0hnskot Posted October 26, 2014 Author Share Posted October 26, 2014 could you try swaping function playerHit( bullet, player ) with function playerHit( player, bullet ) and see if it works? Link to comment Share on other sites More sharing options...
honorthrawn Posted October 26, 2014 Share Posted October 26, 2014 Wow. Thanks. I thought I had already tried that and it didn't work. But it worked great this morning. Link to comment Share on other sites More sharing options...
j0hnskot Posted October 26, 2014 Author Share Posted October 26, 2014 What version of Phaser are you using? Link to comment Share on other sites More sharing options...
honorthrawn Posted October 26, 2014 Share Posted October 26, 2014 Phaser 2.1.1 Link to comment Share on other sites More sharing options...
Recommended Posts