Sign in to follow this  
damager

Sprite pool reusing bug(?)

Recommended Posts

Hi, i'm going to create phaser runner game, but i stuck with one problem. I create a bullets like this:

this.bullets = this.game.add.group();
this.bullets.name = 'bullets';
this.bullets.enableBody = true;
this.bullets.physicsBodyType = Phaser.Physics.P2JS;
this.bullets.createMultiple(100, 'playerBullet', 0, false);
this.bullets.forEach(function(child){
    child.body.setRectangle(25, 35, 0, 0);
    child.name = "bullet";
    child.anchor.set(0.5, 0.5);
    child.body.motionState = Phaser.Physics.P2.Body.KINEMATIC;
    //child.body.debug = true;
    child.body.onBeginContact.add(SimpleGame.prototype.onBulletBeginContact, this);
}, this);
this.bullets.setAll('outOfBoundsKill', true);
this.bullets.setAll('checkWorldBounds', true);

and creating them 5 times at second

this.shotTime.loop(200, SimpleGame.prototype.shotTheEnemies, this);
shotTheEnemies(e:Phaser.TimerEvent)
{
    let bullet: Phaser.Sprite;
    for (var i = 0; i < 2; i++)
    {
        bullet = this.bullets.getFirstExists(false);

        if (bullet)
        {
            bullet.reset(this.player.x -14+(28*i), this.player.y - this.player.height/2-20);
            bullet.body.moveUp(900);
            //console.log(bullet.body.)
        }
    }

}

But in time when bullets hit enemy and i use bullet.kill() some of pool bullets don't have speed(And i can't manipulate them till i use bullet.kill again, BUT i don't know which bullets are lag).

That's not happening when bullets hit the world bounce. Please help)

Share this post


Link to post
Share on other sites

That's not object pool bug, i've found that bug appears when i use bullet.kill() right at the moment when collision is started (OR i'm wrong, cause hard to recreate bug when i slowly move the ship that fires bullets). But i don't know how to fix this and why it appears in that way...Can i somehow clear colliding forces when i use bullet.kill() function? Does someone know? (Plz help, my brain is literally blowing up) Thanks)

Share this post


Link to post
Share on other sites

20 hours fighting with this "bug"...but fixed. Problem was in this line

this.bullet = this.bullets.getFirstExists(false);

That is correct version:

this.bullet = this.bullets.getFirstExists(false, false, this.player.x -14+(28*i), this.player.y - this.player.height/2);

i don't know why, but if i don't set position in this method, and set it up later this "bug" shows up.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.