lifesuxtr Posted November 16, 2017 Share Posted November 16, 2017 I am trying to damage player or enemy when they collide.(enemy will be damaged when collided with bullet and player will be damaged when collided with monster) this.game.physics.arcade.overlap(this.laser.bullets, this.zombies, this.hitEnemy, null, this); this.game.physics.arcade.overlap(this.player, this.zombies, this.playerdmg, null, this); } playerdmg(player,zombies){ player.damage(1); //oneshots } hitEnemy(bullets,zombies){ zombies.damage(1); // one shots console.log(zombies.health) // this is -1 console.log("hit"); bullets.kill(); } Collision detection works.but i cant damage player or zombies. this is how i set health after creating player: this.player.health=10; how do i set zombies health correctly ? because i create them with for loop like this: this.zombies = this.game.add.group(); this.zombies.enableBody=true; this.zombies.health = 5; for (var i = 0; i < 30; i++) { var zombie = this.zombies.create(this.game.world.randomX, this.game.world.randomY, 'zombie'); let zombieHealthBar = new HealthBar(this.game, zombie, 25, 10, -20, 0, '#FF0000', '#00FF00'); } edit: I figured out the problem completely.The external library im using for healtbars is breaking the player health or zombie health. import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import * as Phaser from "phaser-ce"; import { AngularFireAuth } from 'angularfire2/auth'; import { AngularFireDatabase } from 'angularfire2/database'; import { Observable } from 'rxjs/Observable'; import { HealthBar } from 'rxjs/healthBar'; /** * Generated class for the ZombiePage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @IonicPage() @Component({ selector: 'page-zombie', templateUrl: 'zombie.html', }) export class ZombiePage { game:any; spacefield:any; map:any; layer:any; cursors:any; player:any; zombies:any; laser:any; profileData: Observable<any[]> | any; avatar:any; constructor(private fire: AngularFireAuth, private db: AngularFireDatabase,public navCtrl: NavController, public navParams: NavParams) { } ionViewDidLoad() { console.log('ionViewDidLoad ZombiePage'); this.game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, 'game', { preload: this.preload.bind(this), create: this.create.bind(this),update:this.update.bind(this),render:this.render.bind(this) }); console.log(window.innerHeight); this.profileData = this.fire.authState.switchMap(auth => this.db.object(`profile/${auth.uid}`).valueChanges()); this.profileData.subscribe(profile =>{ this.avatar = profile.avatar; console.log(this.avatar); }); } preload(){ console.log(this.avatar) this.game.load.tilemap('map', 'assets/zombie.csv', null, Phaser.Tilemap.CSV); this.game.load.image('player', 'assets/petyr.png'); this.game.load.image('tiles', 'assets/zombie.png'); this.game.load.image('zombie','assets/actualzombie.png'); this.game.load.image('laser', 'assets/laser.png'); } create(){ this.map = this.game.add.tilemap('map', 64, 64); // Now add in the tileset this.map.addTilesetImage('tiles'); // Create our layer this.layer = this.map.createLayer(0); // Resize the world this.layer.resizeWorld(); // Allow cursors to scroll around the map this.cursors = this.game.input.keyboard.createCursorKeys(); this.player = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY,'player'); this.game.physics.enable(this.player,Phaser.Physics.ARCADE); this.player.scale.setTo(0.20,0.20) this.player.health=500; //new HealthBar(this.game, this.player, 75, 10, 25, -10, '#FF0000', '#00FF00'); this.game.camera.follow(this.player); // Creates 30 bullets, using the 'bullet' graphic this.laser = this.game.add.weapon(100, 'laser'); // The bullet will be automatically killed when it leaves the world bounds this.laser.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; // The speed at which the bullet is fired this.laser.bulletSpeed = 300; // Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms this.laser.fireRate = 400; this.laser.trackSprite(this.player, 20, 20, false); this.zombies = this.game.add.group(); this.zombies.enableBody=true; for (var i = 0; i < 30; i++) { var zombie = this.zombies.create(this.game.world.randomX, this.game.world.randomY, 'zombie'); zombie.health=5; let zombieHealthBar = new HealthBar(this.game, zombie, 25, 10, -20, 0, '#FF0000', '#00FF00'); } } update(){ console.log(this.player.health); this.player.body.velocity.x = 0 this.player.body.velocity.y = 0 if (this.cursors.left.isDown) { this.player.body.velocity.x =-150; } else if (this.cursors.right.isDown) { this.player.body.velocity.x =150; } if (this.cursors.up.isDown) { this.player.body.velocity.y =-150; } else if (this.cursors.down.isDown) { this.player.body.velocity.y=150; } this.zombies.forEach(function(enemy){ enemy.rotation = this.game.physics.arcade.angleBetween(enemy, this.player); this.game.physics.arcade.velocityFromAngle(enemy.angle, 150, enemy.body.velocity); //this.game.physics.arcade.moveToObject(enemy,this.player,250); },this); if (this.game.input.activePointer.isDown) { this.laser.fireAtPointer(this.game.input.activePointer); } this.game.physics.arcade.overlap(this.laser.bullets, this.zombies, this.hitEnemy, null, this); this.game.physics.arcade.overlap(this.player, this.zombies, this.playerdmg, null, this); } playerdmg(player,zombies){ player.damage(1); // not working. } hitEnemy(bullets,zombies){ zombies.damage(1); // this is not working console.log(zombies.health); bullets.kill(); } render(){ } } This one is working.If i uncomment HealthBar codes it doesnt work.it just resets player and zombie health to 0.I couldnt figure out why. this is the plugin im using:(healhBar.js) "use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var HealthBar = (function (_super) { __extends(HealthBar, _super); function HealthBar(game, spriteToAttach, width, height, _xOffset, _yOffset, bgColor, fgColor) { if (_xOffset === void 0) { _xOffset = 0; } if (_yOffset === void 0) { _yOffset = 0; } if (bgColor === void 0) { bgColor = "#DD1111"; } if (fgColor === void 0) { fgColor = "#11DD11"; } _super.call(this, game, spriteToAttach.body.position.x + _xOffset, spriteToAttach.body.position.y + _yOffset); this.spriteToAttach = spriteToAttach; this.width = width; this.height = height; this._xOffset = _xOffset; this._yOffset = _yOffset; this.bgColor = bgColor; this.fgColor = fgColor; //Draw bar background this.bmdBg = game.add.bitmapData(width, height); this.bmdBg.ctx.fillStyle = this.bgColor; this.bmdBg.ctx.fillRect(0, 0, width, height); this.bgSprite = game.add.sprite(this.x, this.y, this.bmdBg); this.bgSprite.anchor.set(0.5); //Draw bar foreground this.bmdFg = game.add.bitmapData(width, height); this.bmdFg.ctx.fillStyle = this.fgColor; this.bmdFg.ctx.fillRect(0, 0, width, height); this.fgSprite = game.add.sprite(this.x, this.y, this.bmdFg); this.fgSprite.anchor.set(0.5); this.game.world.add(this); // triggers the update function } HealthBar.prototype.update = function () { //Redraw health this.bmdFg.clear(); this.bmdFg.ctx.fillRect(0, 0, (this.width / 100) * this.spriteToAttach.health, this.height); //Health bar follow sprite this.bgSprite.position.x = this.spriteToAttach.position.x + this.xOffset; this.bgSprite.position.y = this.spriteToAttach.position.y + this.yOffset; this.fgSprite.position.x = this.spriteToAttach.position.x + this.xOffset; this.fgSprite.position.y = this.spriteToAttach.position.y + this.yOffset; //Delete on destroy if (this.spriteToAttach.health = 0) { this.bmdFg.destroy(); this.bmdBg.destroy(); this.bgSprite.destroy(); this.fgSprite.destroy(); this.destroy(); } }; Object.defineProperty(HealthBar.prototype, "xOffset", { get: function () { return this._xOffset; }, set: function (xOff) { this._xOffset = xOff; }, enumerable: true, configurable: true }); Object.defineProperty(HealthBar.prototype, "yOffset", { get: function () { return this._yOffset; }, set: function (yOff) { this._yOffset = yOff; }, enumerable: true, configurable: true }); return HealthBar; }(Phaser.Sprite)); exports.HealthBar = HealthBar; Link to comment Share on other sites More sharing options...
Skeptron Posted November 16, 2017 Share Posted November 16, 2017 I guess it oneshots because a lot of collisions happen per second when objects overlap (and thus, you take the damage a LOT of time). You should put a safe-delay mechanism that protects a player against damage for a few millis after taking some. Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 16, 2017 Author Share Posted November 16, 2017 8 minutes ago, Skeptron said: I guess it oneshots because a lot of collisions happen per second when objects overlap (and thus, you take the damage a LOT of time). You should put a safe-delay mechanism that protects a player against damage for a few millis after taking some. What about zombies ? am i setting their health correct because after collision i check their health with console.log and its -1 Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 16, 2017 Author Share Posted November 16, 2017 16 minutes ago, Skeptron said: I guess it oneshots because a lot of collisions happen per second when objects overlap (and thus, you take the damage a LOT of time). You should put a safe-delay mechanism that protects a player against damage for a few millis after taking some. I cant even set player health.it returns zero when i check it in update method. this is in create method this.player = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY,'player'); this.game.physics.enable(this.player,Phaser.Physics.ARCADE); this.player.scale.setTo(0.20,0.20) this.player.health=100; let playerHealthBar = new HealthBar(this.game, this.player, 75, 10, 25, -10, '#FF0000', '#00FF00'); Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 16, 2017 Author Share Posted November 16, 2017 I figured out the problem completely.The external library im using for healtbars is breaking the player health or zombie health. import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import * as Phaser from "phaser-ce"; import { AngularFireAuth } from 'angularfire2/auth'; import { AngularFireDatabase } from 'angularfire2/database'; import { Observable } from 'rxjs/Observable'; import { HealthBar } from 'rxjs/healthBar'; /** * Generated class for the ZombiePage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @IonicPage() @Component({ selector: 'page-zombie', templateUrl: 'zombie.html', }) export class ZombiePage { game:any; spacefield:any; map:any; layer:any; cursors:any; player:any; zombies:any; laser:any; profileData: Observable<any[]> | any; avatar:any; constructor(private fire: AngularFireAuth, private db: AngularFireDatabase,public navCtrl: NavController, public navParams: NavParams) { } ionViewDidLoad() { console.log('ionViewDidLoad ZombiePage'); this.game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, 'game', { preload: this.preload.bind(this), create: this.create.bind(this),update:this.update.bind(this),render:this.render.bind(this) }); console.log(window.innerHeight); this.profileData = this.fire.authState.switchMap(auth => this.db.object(`profile/${auth.uid}`).valueChanges()); this.profileData.subscribe(profile =>{ this.avatar = profile.avatar; console.log(this.avatar); }); } preload(){ console.log(this.avatar) this.game.load.tilemap('map', 'assets/zombie.csv', null, Phaser.Tilemap.CSV); this.game.load.image('player', 'assets/petyr.png'); this.game.load.image('tiles', 'assets/zombie.png'); this.game.load.image('zombie','assets/actualzombie.png'); this.game.load.image('laser', 'assets/laser.png'); } create(){ this.map = this.game.add.tilemap('map', 64, 64); // Now add in the tileset this.map.addTilesetImage('tiles'); // Create our layer this.layer = this.map.createLayer(0); // Resize the world this.layer.resizeWorld(); // Allow cursors to scroll around the map this.cursors = this.game.input.keyboard.createCursorKeys(); this.player = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY,'player'); this.game.physics.enable(this.player,Phaser.Physics.ARCADE); this.player.scale.setTo(0.20,0.20) this.player.health=500; //new HealthBar(this.game, this.player, 75, 10, 25, -10, '#FF0000', '#00FF00'); this.game.camera.follow(this.player); // Creates 30 bullets, using the 'bullet' graphic this.laser = this.game.add.weapon(100, 'laser'); // The bullet will be automatically killed when it leaves the world bounds this.laser.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; // The speed at which the bullet is fired this.laser.bulletSpeed = 300; // Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms this.laser.fireRate = 400; this.laser.trackSprite(this.player, 20, 20, false); this.zombies = this.game.add.group(); this.zombies.enableBody=true; for (var i = 0; i < 30; i++) { var zombie = this.zombies.create(this.game.world.randomX, this.game.world.randomY, 'zombie'); zombie.health=5; let zombieHealthBar = new HealthBar(this.game, zombie, 25, 10, -20, 0, '#FF0000', '#00FF00'); } } update(){ console.log(this.player.health); this.player.body.velocity.x = 0 this.player.body.velocity.y = 0 if (this.cursors.left.isDown) { this.player.body.velocity.x =-150; } else if (this.cursors.right.isDown) { this.player.body.velocity.x =150; } if (this.cursors.up.isDown) { this.player.body.velocity.y =-150; } else if (this.cursors.down.isDown) { this.player.body.velocity.y=150; } this.zombies.forEach(function(enemy){ enemy.rotation = this.game.physics.arcade.angleBetween(enemy, this.player); this.game.physics.arcade.velocityFromAngle(enemy.angle, 150, enemy.body.velocity); //this.game.physics.arcade.moveToObject(enemy,this.player,250); },this); if (this.game.input.activePointer.isDown) { this.laser.fireAtPointer(this.game.input.activePointer); } this.game.physics.arcade.overlap(this.laser.bullets, this.zombies, this.hitEnemy, null, this); this.game.physics.arcade.overlap(this.player, this.zombies, this.playerdmg, null, this); } playerdmg(player,zombies){ player.damage(1); // not working. } hitEnemy(bullets,zombies){ zombies.damage(1); // this is not working console.log(zombies.health); bullets.kill(); } render(){ } } This one is working.If i uncomment HealthBar codes it doesnt work.it just resets player and zombie health to 0.I couldnt figure out why. this is the plugin im using:(healhBar.js) "use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var HealthBar = (function (_super) { __extends(HealthBar, _super); function HealthBar(game, spriteToAttach, width, height, _xOffset, _yOffset, bgColor, fgColor) { if (_xOffset === void 0) { _xOffset = 0; } if (_yOffset === void 0) { _yOffset = 0; } if (bgColor === void 0) { bgColor = "#DD1111"; } if (fgColor === void 0) { fgColor = "#11DD11"; } _super.call(this, game, spriteToAttach.body.position.x + _xOffset, spriteToAttach.body.position.y + _yOffset); this.spriteToAttach = spriteToAttach; this.width = width; this.height = height; this._xOffset = _xOffset; this._yOffset = _yOffset; this.bgColor = bgColor; this.fgColor = fgColor; //Draw bar background this.bmdBg = game.add.bitmapData(width, height); this.bmdBg.ctx.fillStyle = this.bgColor; this.bmdBg.ctx.fillRect(0, 0, width, height); this.bgSprite = game.add.sprite(this.x, this.y, this.bmdBg); this.bgSprite.anchor.set(0.5); //Draw bar foreground this.bmdFg = game.add.bitmapData(width, height); this.bmdFg.ctx.fillStyle = this.fgColor; this.bmdFg.ctx.fillRect(0, 0, width, height); this.fgSprite = game.add.sprite(this.x, this.y, this.bmdFg); this.fgSprite.anchor.set(0.5); this.game.world.add(this); // triggers the update function } HealthBar.prototype.update = function () { //Redraw health this.bmdFg.clear(); this.bmdFg.ctx.fillRect(0, 0, (this.width / 100) * this.spriteToAttach.health, this.height); //Health bar follow sprite this.bgSprite.position.x = this.spriteToAttach.position.x + this.xOffset; this.bgSprite.position.y = this.spriteToAttach.position.y + this.yOffset; this.fgSprite.position.x = this.spriteToAttach.position.x + this.xOffset; this.fgSprite.position.y = this.spriteToAttach.position.y + this.yOffset; //Delete on destroy if (this.spriteToAttach.health = 0) { this.bmdFg.destroy(); this.bmdBg.destroy(); this.bgSprite.destroy(); this.fgSprite.destroy(); this.destroy(); } }; Object.defineProperty(HealthBar.prototype, "xOffset", { get: function () { return this._xOffset; }, set: function (xOff) { this._xOffset = xOff; }, enumerable: true, configurable: true }); Object.defineProperty(HealthBar.prototype, "yOffset", { get: function () { return this._yOffset; }, set: function (yOff) { this._yOffset = yOff; }, enumerable: true, configurable: true }); return HealthBar; }(Phaser.Sprite)); exports.HealthBar = HealthBar; Link to comment Share on other sites More sharing options...
samme Posted November 17, 2017 Share Posted November 17, 2017 Remember each argument to the collide/overlap callbacks is always one sprite, e.g., one bullet, player, zombie. Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 17, 2017 Author Share Posted November 17, 2017 6 hours ago, samme said: Remember each argument to the collide/overlap callbacks is always one sprite, e.g., one bullet, player, zombie. I know its missleaing must be bullet and zombie Link to comment Share on other sites More sharing options...
n7pankake Posted January 12, 2018 Share Posted January 12, 2018 On 16/11/2017 at 6:20 PM, Skeptron said: I guess it oneshots because a lot of collisions happen per second when objects overlap (and thus, you take the damage a LOT of time). You should put a safe-delay mechanism that protects a player against damage for a few millis after taking some. I did try to do that but it didn't work maybe I'm not doing it properly? Link to comment Share on other sites More sharing options...
Recommended Posts