JackAttack Posted April 7, 2017 Share Posted April 7, 2017 I'm making a tower defence game and it uses bullets generated through Phaser's weapon/bullet plugin. I want to iterate over them within the game state's update(), however when I do either of the following: this.game.children.forEach(function(bullet) { console.log(bullet); }, this); this.game.bullets.children.forEach(function(bullet) { console.log(bullet); }, this); The bullet object is the group of bullets, not the bullet sprite itself. Inspecting bullet, I can see it has a children property, which is an array of the bullet sprites I want. Alas, trying this.game.bullets.children.forEach() still doesn't get them. The equivalent code on a different group, such as: this.game.towers.forEach(function(tower) { console.log(tower); }, this); behaves as expected, tower is a tower sprite, and not it's containing group. Is there something special about the bullets group that is causing this? If so, is there a way of getting all bullets? Link to comment Share on other sites More sharing options...
samid737 Posted April 7, 2017 Share Posted April 7, 2017 Hi, did you include the name of your weapon object in order to retrieve it from the actual weapon itself: myWeapon= game.add.weapon(30, 'phaz0r'); myWeapon.bullets.forEach(function(bullet){console.log(bullet)}); This works in my case? bullets are a property (a group) of the weapon object. http://phaser.io/docs/2.6.2/Phaser.Weapon.html#bullets Link to comment Share on other sites More sharing options...
JackAttack Posted April 7, 2017 Author Share Posted April 7, 2017 Thanks Samid, using the weapon object got me there. In my case, each weapon is found at weapon1 property in a tower object, which is in the towers group, so my code looks like: this.towers.forEach(function(tower) { if (tower.weapon1) { tower.weapon1.bullets.forEach(function (bullet) { console.log(bullet) }); } }, this); samid737 1 Link to comment Share on other sites More sharing options...
msickle Posted April 7, 2017 Share Posted April 7, 2017 You can check one pool of objects against another very simply: this.physics.arcade.overlap( g.bulletPool, this.jetPool, this.enemyHit, null, this ); Link to comment Share on other sites More sharing options...
JackAttack Posted April 12, 2017 Author Share Posted April 12, 2017 Thanks Bobomite, I'm actually doing something a bit different here, which is redirecting each bullet towards a target I've given it (which is a sprite). If anyone is interested, the code I'm using is: // Begin bullet heat-seeking this.towers.forEach(function(tower) { if (tower.weapon1) { tower.weapon1.bullets.forEach(function (bullet) { if (bullet.target && bullet.target.alive) { var midPoint = mainState.getMidPointBetweenSprites(bullet, bullet.target); var moveX = Math.cos(this.game.math.degToRad(midPoint.angle)) * bullet.speed; var moveY = Math.sin(this.game.math.degToRad(midPoint.angle)) * bullet.speed; bullet.body.velocity.set(moveX, moveY); } }); } }, this); // End bullet heat-seeking The getMidPointBetweenSprites function I use here provides the angle: mainState.getMidPointBetweenSprites = function(spriteA, spriteB) { return { x: Math.round((spriteA.x + spriteB.x) / 2), y: Math.round((spriteA.y + spriteB.y) / 2), angle: Math.atan2(spriteB.y - spriteA.y, spriteB.x - spriteA.x ) * (180/Math.PI) } }; Link to comment Share on other sites More sharing options...
Recommended Posts