Jump to content

Phaser P2 collisions not working


robersend
 Share

Recommended Posts

Hi, i've been working with phaser p2 collision groups and I just can't get this working properly. In my game i have the player, the map, enemies, npcs and fireballs. Well... The player collides with everything, the fireballs and enemies collide with each other but not with npcs or the map. I have an object layer for the map colisions. Here's the code:

  game.physics.startSystem(Phaser.Physics.P2JS);  game.physics.p2.setImpactEvents(true);  enemies.enableBody = true;  enemies.physicsBodyType = Phaser.Physics.P2JS;  enemies2 = game.add.group();  enemies2.enableBody = true;  enemies2.physicsBodyType = Phaser.Physics.P2JS;  enemies3 = game.add.group();  enemies3.enableBody = true;  enemies3.physicsBodyType = Phaser.Physics.P2JS;  npcs = game.add.group();  npcs.enableBody = true;  npcs.physicsBodyType = Phaser.Physics.P2JS;  var playerCG = game.physics.p2.createCollisionGroup();  wallsCG =  game.physics.p2.createCollisionGroup();  var enemyCG =  game.physics.p2.createCollisionGroup();  var npcCG = game.physics.p2.createCollisionGroup();  var fireballsCG = game.physics.p2.createCollisionGroup();  game.physics.p2.updateBoundsCollisionGroup(); walls = game.physics.p2.convertCollisionObjects(map, "colisiones");               for(var wall in walls)        {            walls[wall].setCollisionGroup(wallsCG);            walls[wall].collides(playerCG);            walls[wall].collides(npcCG);            walls[wall].collides(fireballsCG);        }                var arrayPos=[2152, 523,2042, 396,1987, 483,1330,876,1453,942,1432,1043,2352, 1323,2442, 1196,2287, 1283];        for(var i=0; i<3; i++){            var c = enemies.create(arrayPos[2*i] , arrayPos[2*i +1] , 'evilf', 7);            c.name = 'evil' + i;            c.body.immovable = true;            game.physics.p2.enable(c);            c.body.static = true;            c.body.setCollisionGroup(enemyCG);            c.body.collides(fireballsCG);            c.body.collides(enemyCG);            c.body.collides(wallsCG);            c.body.collides(playerCG);            c.animations.add('Down',[6,7,8],FPSMOVEENEMY, false );            c.animations.add('Up',[42,43,44],FPSMOVEENEMY, false);            c.animations.add('Right',[30,31,32],FPSMOVEENEMY, false);            c.animations.add('Left',[18,19,20],FPSMOVEENEMY, false);                   }        for(var i=0; i<3; i++){            var c = enemies2.create(arrayPos[2*(3+i)] , arrayPos[2*(3+i) +1], 'evilf', 7);            c.name = 'evil' + i;            c.body.immovable = true;            game.physics.p2.enable(c);            c.body.static = true;            c.body.setCollisionGroup(enemyCG);            c.body.collides(enemyCG);            c.body.collides(fireballsCG);            c.body.collides(wallsCG);            c.body.collides(playerCG);            c.animations.add('Down',[6,7,8],FPSMOVEENEMY, false );            c.animations.add('Up',[42,43,44],FPSMOVEENEMY, false);            c.animations.add('Right',[30,31,32],FPSMOVEENEMY, false);            c.animations.add('Left',[18,19,20],FPSMOVEENEMY, false);        }        for(var i=0; i<3; i++){            var c = enemies3.create([2*(6+i)] , arrayPos[2*(6+i) +1], 'evilf', 7);            c.name = 'evil' + i;            c.body.immovable = true;            game.physics.p2.enable(c);            c.body.static = true;            c.body.setCollisionGroup(enemyCG);            c.body.collides(fireballsCG);            c.body.collides(enemyCG);            c.body.collides(wallsCG);            c.body.collides(playerCG);            c.animations.add('Down',[6,7,8],FPSMOVEENEMY, false );            c.animations.add('Up',[42,43,44],FPSMOVEENEMY, false);            c.animations.add('Right',[30,31,32],FPSMOVEENEMY, false);            c.animations.add('Left',[18,19,20],FPSMOVEENEMY, false);                   }        var npcsArray= [460, 650, 80,302, 300,780, 350,500, 550,350];        var npcsSizes= [1, 4, 49,52,55];        for(var i=0; i<5; i++){            var c = null;            c = npcs.create(npcsArray[2*(i)] , npcsArray[2*(i) +1], 'evilf', npcsSizes[i]);            c.name = 'evil' + i;            c.body.immovable = true;            game.physics.p2.enable(c);            c.body.static = true;            c.body.setCollisionGroup(npcCG);            c.body.collides(fireballsCG);            c.body.collides(wallsCG);            c.body.collides(playerCG);                    }        /*this.npc.body.clearCollision();        varnon.sprite.body.clearCollision();*/ //Hay que descomentarlo y usar overlap                        //npc.body.static = true;                        fireballs = game.add.group();        fireballs.enableBody = true;        fireballs.physicsBodyType = Phaser.Physics.P2JS;        for (var i = 0; i < 20; i++)        {            var f = fireballs.create(0, 0, 'fireball');            f.animations.add('fireballAnimRight', [32, 33, 34, 35, 36, 37, 38, 39], FPSMOVE, true);            f.animations.add('fireballAnimLeft', [0, 1, 2, 3, 4, 5, 6, 7], FPSMOVE, true);            f.animations.add('fireballAnimUp', [16, 17, 18, 19, 20, 21, 22, 23], FPSMOVE, true);            f.animations.add('fireballAnimDown', [48, 49, 50, 51, 52, 53, 54, 55], FPSMOVE, true);            f.name = 'fireball' + i;            f.exists = false;            f.visible = false;            f.checkWorldBounds = true;            f.events.onOutOfBounds.add(resetFireball, this);            game.physics.p2.enable(f);            f.body.static = true;            f.body.setCollisionGroup(fireballsCG);            f.body.collides(wallsCG);            f.body.collides(enemyCG);            f.body.collides(npcCG);                    }                                        //npc.body.setCollisionGroup(npcCG);        //npc.body.collides(playerCG);        // npc.body.collides(wallsCG);        //npc.body.collides(fireballsCG);               varnon.sprite.body.setCollisionGroup(playerCG);        varnon.sprite.body.collides(npcCG);        varnon.sprite.body.collides(wallsCG);        varnon.sprite.body.collides(enemyCG);        varnon.sprite.body.fixedRotation = true;        varnon.alternar(true);

Any idea on why it isn't working?

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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