How do loop something?

I know the general basics of a loop, but I’m coding a game where you have to buy something then something would loop. Ex:

var x = 0
var z = 0
function functionhere() { 
if (mouseIsOver(sprite) && mouseWentDown("leftButton")) {
      x = x + 1
 }
if (x == 1) {
      for (var y = 0; y < longnumberhere; y++) {
          z = z + 1
  }
 }
}
//it should loop i think but i dont why it doesnt work

Put it in the draw() function.

I’m pretty sure that wouldn’t work and plus I do have it in there, but I didn’t show it.

Is the functionhere() function being called?

I just did it as a replacement for draw for no absolute reason I did actually put draw, like I said I put it on there. Also it’s a draw function it probably does not need to be called.

If you can share your code with us, we can look at it and try to help you debug.

thanks,

Mike

I basically just did.

Sorry. I meant if you share a link to your code, we can try it. It’s ok if you don’t want to share as well; It’s sometimes just harder to debug if we can’t play with it a little.

My thought was similar to @wutadam in that the function needs to be called. Even if it is in the draw loop, it may not just run automatically, it may need to be called directly before it runs.

Even if you wanted to put that code into a program that is separate from the game you are making and just share that link, I would be happy to look at it…

Mike

1 Like

@suu,

Here’s a link to a project that mostly uses your code. I just added the text to display the value of X.

I created the function outside of the draw loop and called it inside the draw loop and it is appearing to work.

Does this help?

Let us know if not!

Thanks,

Mike

1 Like

I debugged it by myself somehow, but thanks! If I caused any inconvenience I’m sorry.

1 Like