Jump to content

Issue with P2


ulixm
 Share

Recommended Posts

Hello,
I am developing a space shooter game with Phaser + P2 physics. During the game play, the player chooses one of the available weapons at a time to kill its enemies and, for all projectile based weapons, I have created a pool of shots. Everytime the player shoots a weapon, one of the "dead" projectiles is revived via Sprite.reset() method, then re-positioned, rotated and so on. Whenever a projectile hits a target or leaves the world it is killed via Sprite.kill() or Sprite.outOfBoundsKill flag and gets back to the pool. So far so good as it works almost 100% of the time. The problem is that sometimes (about 1 in every 100 shots), the projectile is revived but remains stuck on its new position until the game is restarted and the pool remains short of one projectile (or more if it happens more than once in the level). A debug analysis revealed me that although the projectile in this state doesn't interact with the world anymore (no collisions), it still exists (Sprite.exists === true), is alive, and has a physics body that is not sleeping. It seems however, that for some weird reason, it's body is not reinserted in the list of P2 bodies that should be updated by the main P2 loop.

One possible but awful solution would be to monitor every revived projectile, for one frame, to detect the ones that are not moving. Then kill and revive those again. But that would be my last resort...

Does anyone have any clue on why this is happening and how to fix it?

This is the most relevant code:

    // Called when player fires a weapon
    reviveProjectile: function(group, xOfst, yOfst, ...) {
        var target = group.getFirstExists(false);

        if (target) {
            var playerShip = this.game.playerShip;
            playerShip.rotation = playerShip.body.rotation;
            this.tempPoint.setTo(xOfst, yOfst);
            var p = playerShip.toGlobal(this.tempPoint);
            target.reset(p.x, p.y);            // Revive the projectile
            // ...
    },

    // Called when the projectile hits something
    shotHit: function(shotBody, contactBody, baseDamage, ...) {
        if (contactBody.sprite) {
            shotBody.sprite.fireSoundFx.stop();         // Stop projectile launch sound
            shotBody.sprite.kill();                     // Remove shot sprite
            // ...
    },

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...