zelcard Posted August 7, 2017 Share Posted August 7, 2017 Hello, I'm creating endless style game and I'm planning to reuse already existing sprites instead of creating new ones. My sprites are coming from the top moving towards the bottom of the screen, when they reach bottom I reset their position.y to 0 which brings them back to the top again, but I also wanted them to have random position.x every time they start again from the top, I called this function: obstacles.forEach(function(obstacle){if(obstacle.position.y > this.game.world.height){obstacle.position.y = 0; obstacle.position.x = this.game.rnd.between(0, this.game.world.width);}}) And after trying it out I got this error from the developers console: Quote Uncaught TypeError: Cannot read property 'rnd' of undefined Is it even posibble to call this.game.rnd.between from the update? If not what is the solution for that? Thanks! Link to comment Share on other sites More sharing options...
PhasedEvolution Posted August 7, 2017 Share Posted August 7, 2017 Yes you can call that function in update. That is not the problem. Check what "this.game" refers to. Link to comment Share on other sites More sharing options...
samme Posted August 7, 2017 Share Posted August 7, 2017 When you pass a function that uses this you must also pass a context for it: obstacles.forEach(function (obstacle) { if (obstacle.position.y > this.game.world.height) { obstacle.position.y = 0; obstacle.position.x = this.game.rnd.between(0, this.game.world.width); } }, this); Link to comment Share on other sites More sharing options...
zelcard Posted August 7, 2017 Author Share Posted August 7, 2017 Yes! Works like a charm. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts