lobsterhands Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
Heppell08 Posted April 25, 2014 Share Posted April 25, 2014 Make the variables global. Have the blocks in global variable and then use the update code to change the colours. lobsterhands 1 Link to comment Share on other sites More sharing options...
lobsterhands Posted April 25, 2014 Author Share Posted April 25, 2014 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 More sharing options...
Heppell08 Posted April 25, 2014 Share Posted April 25, 2014 Where ever it is you place your vars just put var block1 in with the rest of the vars. Example, you'll have var player;var block1;var block2;Do that, then when you create the group, instead of having var block1 in the group creation, just have it as block1.That's basically it. Hopefully that helps a bit more lobsterhands 1 Link to comment Share on other sites More sharing options...
blackgames Posted April 25, 2014 Share Posted April 25, 2014 If you create this once or adding in equal intervals you can (I think so - not tested) use pipes.getAt(posOfBlock1) Link to comment Share on other sites More sharing options...
lobsterhands Posted April 25, 2014 Author Share Posted April 25, 2014 @Heppell08, I didn't do this exactly, but you pointed me in a good direction. I got it working. Thank you very much. Link to comment Share on other sites More sharing options...
Heppell08 Posted April 25, 2014 Share Posted April 25, 2014 I'm glad I got you in the right direction of a working method. Link to comment Share on other sites More sharing options...
lobsterhands Posted April 26, 2014 Author Share Posted April 26, 2014 Okay, I played with simply declaring the variable in the preload() function, called it in the update() function and everything works. Much cleaner than what I came up with the first time. Thanks again. Link to comment Share on other sites More sharing options...
Heppell08 Posted April 26, 2014 Share Posted April 26, 2014 No problem, I'm not a fan of always declaring every variable as global but 9 times out of 10 its probably safer to do so because phaser can upsate any global you declare and use lobsterhands 1 Link to comment Share on other sites More sharing options...
Heppell08 Posted April 26, 2014 Share Posted April 26, 2014 Also, I have tried declaring a private var and updating it in update and failed so put it global and had success. Link to comment Share on other sites More sharing options...
Recommended Posts