Jump to content

Collision Detection on Single Item in Group


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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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