Jump to content

Using for loop to add existing objects


chunkmighty
 Share

Recommended Posts

What I am trying to do is spawn 3 of these objects and give them some sort of collision independently. I tried doing collision on the group like this but it did not work. It seemed to work spawn the 3 objects but the collision only works with the last object in the row

 
        this.boneGroup = this.game.add.group();        for (var i = 0; i < 3; i++) {            this.bone = new Bone(this.game, 1200, 200, 0);            this.game.add.existing(this.bone);            this.boneGroup.add(this.bone);        }In my updatethis.game.physics.arcade.overlap(this.player, this.boneGroup, this.boneCollide);
 
So my next thought was to try and give each bone object a unique name so I could assign collision independently but I get errors:
 
Uncaught TypeError: Cannot set property '0' of undefinedgame.js:293 Play.addBonephaser.js:37163 Phaser.Timer.updatephaser.js:36670 Phaser.Time.updatephaser.js:18663 Phaser.Game.updatephaser.js:32700 Phaser.RequestAnimationFrame.updateRAFphaser.js:32686 Phaser.RequestAnimationFrame.start._onLoop

 

 

        this.boneGroup = this.game.add.group();        for (var i = 0; i < 3; i++) {            this.bone[i] = new Bone(this.game, 1200, 200, 0);            this.game.add.existing(this.bone[i]);            this.boneGroup.add(this.bone[i]);        }        this.boneGroup.forEach(function(item) {            this.game.physics.arcade.overlap(this.player, item, this.boneCollide);            this.boneCount += 1;        }, this);

Any thoughts? I apologize in advance for my less than great js skills as I am really just an artist trying to make his own game :)

 

Thanks!

Link to comment
Share on other sites

Could you explain a little more what exactly you're trying to make happen? Do you want to do something different with each one when they collide? If so, you should add your bones to a single group, check collision against that group and put your logic to determine what happens to each one individually inside the this.boneCollide callback, something like this:

create: function() {  this.boneGroup = this.game.add.group();  // Use a temporary var rather than a property, as we'll not be using it after this function  var tmpBone;   for (var i = 0; i < 3; i++) {    tmpBone = new Bone(this.game, 1200, 200, 0);    // Give each bone a number we can check for later    tmpBone.id = i;    // Add the bone straight to the group, no need to add it to the game    this.boneGroup.add(this.bone);  }},update: function () {  // Check for overlaps against all bones  this.game.physics.arcade.overlap(this.player, this.boneGroup, this.boneCollide);},boneCollide: function(player, bone) {  if (bone.id === 0) {    // This is bone 0, do something specific to this bone  }  else if (bone.id === 1) {    // This is bone 1, do something else  }  else if (bone.id === 2) {    // Bone 3 checking in!  }  else {    // This shouldn't be called, but let's keep it here anyway just in case!  }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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