JackAttack

Iterate over bullets

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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);

 

Share this post


Link to post
Share on other sites

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)
    }
};

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.