Jump to content

Having trouble with Groups


jagd
 Share

Recommended Posts

So in my game the player object is made up of several different sprites, so I made a Group called "player," and tried to add an object to it, "rover_proto."  I am also trying to use forEach() to change attributes of each item belonging to the group.

 

Here is my code:

function createGameScene() {    // format: (game, name, addToStage, enableBody, physicsBodyType)    player = game.add.group(game.world, 'player', true, true, Phaser.Physics.P2);    Rover_proto = player.create(0, 0, 'Rover_proto');    //player.setAll('body', true, true, false);	    //Group collision    player.forEachExists(function (item){	item.body.setCollisionGroup(playerCollision);	item.body.collides(wallCollisionGroup);	item.body.createGroupCallback(wallCollisionGroup, hurt_rover, player);    });}

So when I comment out the setAll() is says that "item.body is null."  I thought that using game.add.group allows you to enableBody for each item in the group, at least I think it worked that way in Arcade (I am using P2 now which is what is causing me this trouble).

 

So that's when I added in player.setAll(), to hopefully give everything a body.  However, if I uncomment it, then the program gives two errors:  "TypeError: item.body.setCollisionGroup is not a function" and "TypeError:  This.body.preUpdate is not a function."

 

I don't understand why these problems are being caused.  I remember using arcade the only way to fix the problem of objects in a group not having bodies was to use the game.add.group() method which I did use in this code.  I've also used forEach before, but here it's not working.

 

Is there something I'm missing?  Am I using these functions wrong?  Can forEach only be called during Update() and not Create() or something?  How can I create a body and physics and set collisions, etc. for each object in a group if game.add.group() isn't working (and why isn't it working)?

 

Thank you for your time,

--jagd

Link to comment
Share on other sites

  • 1 year later...
 Share

  • Recently Browsing   0 members

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