ZRT Posted January 18, 2014 Share Posted January 18, 2014 Hello everyone, I'm having a trouble resetting my for loop, based on an example found here. Here's my code: This in my create() function. obstaclesBottom = game.add.group(); for (var i = 0; i < 10; i++) { h = obstaclesBottom.create(600 + Math.random() * 3500, 220 , 'obstacle'); h.events.onOutOfBounds.add(obstacleReset); h.body.velocity.x = -200; }I have this, but doesn't reset, only 10 of those obstacles are appearing.function obstacleReset(obstaclesBottom) { h.reset(h.x, -10);}I want to make those obstacles appear until the player sprite collide with them which I already did. Is there any other way to make that loop goes on (always randomize the position of the enemy obstacles/sprites) that may come in handy? Thank you. Link to comment Share on other sites More sharing options...
rich Posted January 18, 2014 Share Posted January 18, 2014 What are the actual bounds of your world? As it looks like you might be resetting them out of bounds (-10). Link to comment Share on other sites More sharing options...
ZRT Posted January 18, 2014 Author Share Posted January 18, 2014 The canvas is 800x350. Nothing else is explicitly set. Tried setting h.reset(h.x, 600 + Math.random() * 3500); but still nothing. Only 9 'obstacles' are showing. I the second parameter is actually the x axis position, right? That's what I got from the example. Link to comment Share on other sites More sharing options...
ZRT Posted January 19, 2014 Author Share Posted January 19, 2014 Any help? Link to comment Share on other sites More sharing options...
Heppell08 Posted January 19, 2014 Share Posted January 19, 2014 Instead of h.reset(h.x, 600 + Math.random() * 3500); why not try h.reset(h.x = game.world.randomX); Link to comment Share on other sites More sharing options...
ZRT Posted January 20, 2014 Author Share Posted January 20, 2014 EDIT: It seems to work now, with this code. I just added Y position and velocity in the reset function. Not sure if it's the right way to do it, feels kinda laggy but still goes on until collision with player object. function createObstacles() { for (var i = 0; i < 5; i++) { ob = obstaclesBottom.create(600 + Math.random() * 1500, 220 , 'obstacle'); ob.events.onOutOfBounds.add(obstaclesReset, this); ob.body.velocity.x = -200; }}function obstaclesReset(obstaclesBottom) { ob.reset(600 + Math.random() * 1500, 220); ob.body.velocity.x = -200;}I tried separating the functions (also changed the names of some variables too), and I'm calling the createObstacle(); in function create(); but it still won't work as intended. I might be still spawning them somewhere out of bounds, I don't know.. :/function obstacleCreate() { for (var i = 0; i < 2; i++) { ob = obstaclesBottom.create(600 + Math.random() * 3500, 220 , 'obstacle'); ob.events.onOutOfBounds.add(obstacleReset, this); ob.body.velocity.x = -200; }}function obstacleReset(obstaclesBottom) { ob.reset(ob.x, game.world.randomX);}Should I pass Y position in the reset? I only want one function to start over and go on in a loop. Link to comment Share on other sites More sharing options...
Recommended Posts