Jump to content

phaser.js - p2 body of group is undefined


skywalker
 Share

Recommended Posts

im a beginner in Phaser.js game development and i want to create static body to group , with p2 physic system i created the group and then started the physic system.

var block_group = game.add.group();
var Y = 20;
for (var i = 0; i < numberOfBlocks; i++) {
block_group.create(400,Y,'block');
Y = Y +15;
};
game.physics.startSystem(Phaser.Physics.P2JS);
block_group.enableBody = true;
block_group.physicsBodyType = Phaser.Physics.P2JS;

when i print block_group.body it gives me undefined so i tried using forEachAlive and it stile the same however it works fine for single sprite , but with a group it is not working .

Link to comment
Share on other sites

Groups don't have a body so you can't enable it. Groups hold sprites and each of these can have a body so enable it for each one.

One way to do this would be to modify this line.

block_group.create(400,Y,'block');

to

block = block_group.create(400,Y,'block');

The create function returns the created object so now you can enable body on block within the loop before it is reused on the next create call. Obviously you should create block in an appropriate place with var.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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