Jump to content

How does reset(); works?


Recommended Posts

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

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

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

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...