Jump to content

Collision Detection on Single Item in Group


lobsterhands
 Share

Recommended Posts

GameState.prototype.create = function() {this.pipe = game.add.group();    this.game.physics.enable(this.pipe, Phaser.Physics.ARCADE);    this.pipe.createMultiple(1, 'pipe');    for (var i = 0; i < 1; i++) {      var block1 = this.game.add.sprite(x, y, 'pipe')      var block2 = this.game.add.sprite(x, y, 'pipe')      this.pipe.add(block1);      this.pipe.add(block2);    }}GameState.prototype.update = function() {  game.physics.arcade.collide(this.player, this.pipe, collisionHandler, null, this);}function collisionHandler (obj1, obj2) {    game.stage.backgroundColor = '#992d2d';}

 

The above code works properly to change the background color when the player touches a block. Yay! However, I'm trying to get the update (in this case, background color change) to occur only when the player touches block1. I've tried:

GameState.prototype.update = function() {  game.physics.arcade.collide(this.player, this.block1, collisionHandler, null, this);}

as well as 

GameState.prototype.update = function() {  game.physics.arcade.collide(this.player, this.pipe.block1, collisionHandler, null, this);}

The game runs, but no update occurs. Any ideas?

 

 

 

 

Link to comment
Share on other sites

Heppell, thanks for the response. I understand what you mean when you say make them global, but I don't know how to achieve that with my current set up. Do I need to change the code completely (as in create the blocks outside of a 'for loop') or can I simply share the information inside that loop with the rest of the code easily? 

 

If this seems like a question coming from a noob, it totally is.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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