QuentinIcky Posted February 3, 2017 Share Posted February 3, 2017 Hi all, I'm trying to generate items randomly. For the moment it's ok, but I would like to add a little delay from every items. For exemple, I would like to find a way to generate them every 2scd. My code below : In the create() : emails = level2.add.group(); emails.enableBody = true; In update() function createEmails(mail) { var mail = emails.create(level2.world.randomX, 0, 'email'); mail.width = 50; mail.height = 40; // mail.name = 'mail' + x.toString() + y.toString(); // mail.checkWorldBounds = true; mail.events.onOutOfBounds.add(mailOut, this); var numY = Math.random() * 500 + 200; // Get a number between 200 and 500; mail.body.velocity.y = numY; var numX = Math.floor(Math.random() * 99) + 1; ; numX *= Math.floor(Math.random() * 2) == 1 ? 1 : -1; mail.body.velocity.x = numX; } function startGame(mail) { if (score >= 1000) { emails.destroy() } else if (scoreEnemy >= 1000) { emails.destroy() } else { createEmails(); } } Thanks by advance ! Link to comment Share on other sites More sharing options...
Rydez Posted February 3, 2017 Share Posted February 3, 2017 If I'm understanding correctly, you would like to call the createEmails function every 2 seconds? Perhaps you could use setInterval. You could try putting something like this in the create function: setInterval(function () { createEmails(mail); }, 2000); // 2000 milliseconds is 2 seconds Another method would be to call createEmails within the update function. Maybe something like this would do the trick: var startTime = new Date().getTime(); // in milliseconds function update() { var currentTime = new Date().getTime(); if (currentTime - startTime >= 2000) { createEmails(mail); } } Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 If you wanted to add random delays then use setTimeouts with a random time from within the function, that way when it gets called it'll run the function and generate another random time for the next one. This could potentially make it easier to stop generating items too. Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 3, 2017 Author Share Posted February 3, 2017 Thanks for your answer. @Rydez I tried both methods but I have the same result : the items are generated infinitely after 2 seconds, but not one time every 2 seconds. Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 function createEmails () { ... setTimeout(createEmails, 2000) } Add the setTimeout to the function and call it, it'll keep calling itself every 2 secs from then on. If you wanted to stop it then wrap the setTimeout in a conditional. Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 3, 2017 Author Share Posted February 3, 2017 @mattstyles thanks for your answer too. I tried it but it does the same behaviour. Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 Ha ha, then you're doing something else wrong! The setInterval answer would work too. e.g. whack the following into the browser console or node repl: function hello () { console.log('hi') setTimeout(hello, 2000) } hello() or function hello () { console.log('hi') } setInterval(hello, 2000) Both of these output a log message regularly (ish, JS is only ever ish for timing stuff) every 2 seconds. If yours isn't then something else in your code is causing some issues. Any idea what else in your code could be causing the issue? Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 3, 2017 Author Share Posted February 3, 2017 Yes if I write your code in my browser console I have the good behaviour. My createEmails() function was not inside the update(). I remember that when I did that, I got errors, but not now.. so I'm trying some stuff Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 3, 2017 Author Share Posted February 3, 2017 I've edited my first post to add the function who call createEmails() @mattstyles I tried to put the settimeout inside the stratGame() function, on the "else", but it didn't work Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 14 minutes ago, QuentinIcky said: I tried to put the settimeout inside the stratGame() function, on the "else", but it didn't w @QuentinIcky No that would not work. Think about a stack of functions, and then treat setTimeout as a mechanism that defers pushing a function on to the stack after a set delay. By sticking your setTimeout inside your startGame function, it invokes the function you pass it after the delay, but never gets called again. By doing: function hello () { console.log('hi') setTimeout(hello, 2000) } You create a function named `hello`, when you call that function it uses setTimeout to call the `hello` function again after 2 seconds, which, when invoked, uses setTimeout again, hence you keep calling the function every 2 seconds which queues up another function! Forever! at least, forever in this example. Does that make sense? Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 3, 2017 Author Share Posted February 3, 2017 Yes I understand that, I tried to put it into the creatEmails() like this : function createEmails(mail) { var mail = emails.create(level2.world.randomX, 0, 'email'); mail.width = 50; mail.height = 40; // mail.name = 'mail' + x.toString() + y.toString(); // mail.checkWorldBounds = true; mail.events.onOutOfBounds.add(mailOut, this); var numY = Math.random() * 500 + 200; // Get a number between 200 and 500; mail.body.velocity.y = numY; var numX = Math.floor(Math.random() * 99) + 1;; numX *= Math.floor(Math.random() * 2) == 1 ? 1 : -1; mail.body.velocity.x = numX; setTimeout(createEmails, 2000) } but no emails are generated. Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 Then that is a separate issue, and it's to do with how you're adding emails to the world/system? I take it the `emails.create` function has a side effect that creates a new email object and stuffs it in to an array somewhere? As you're invoking the function the `emails.create` function gets called each 2 secs, but maybe something else isn't working right with the new updated list? Also, whats your intention with passing mail to the `createEmails` function? You immediately overwrite the param. Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 9, 2017 Author Share Posted February 9, 2017 Sorry for the delay (haha) ! Thanks for your answer ! Emails are added like this inside my create() function emails = level2.add.group(); emails.enableBody = true; and then inside the update() function in createEmails() function : function createEmails(mail) { var mail = emails.create(level2.world.randomX, 0, 'email'); ..... ..... } And yes I agree, passing mail in paramater is useless Link to comment Share on other sites More sharing options...
ldd Posted February 10, 2017 Share Posted February 10, 2017 inside your create() function emails = level2.add.group(); emails.enableBody = true; createEmails() OUTSIDE of create() and update() function createEmails() { var mail = emails.create(level2.world.randomX, 0, 'email'); ..... ..... setTimeout(createEmails, 5000); } you also want to avoid doing setTimeout and setInterval inside an update() method... unless it's behind a conditional that your turn off.ex: (this is completely unrelated to your question) window._flag = false; ..... update(){ if(!window._flag){ window._flag = true; setTimeout(function(){ window._flag = false; //... other things you want to do go here }, 5000); } } This is because the update method is (allegedly) being called multiple times... so you are just creating a bunch of setTimeouts. You have to make sure that you are not creating a new timeout before the old one finishes. Link to comment Share on other sites More sharing options...
samme Posted February 10, 2017 Share Posted February 10, 2017 examples/v2/time/basic-looped-event azzz 1 Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 10, 2017 Author Share Posted February 10, 2017 Thanks you both for your answers, I'll try tonight or tomorrow Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 11, 2017 Author Share Posted February 11, 2017 @ldd , you rock ! It's works perfectly. Thanks all for your answers ! Just one question, why do I have to put some function outside of update() or create() ? I made it too for others functions but I didn't know why it didn't work inside these. Link to comment Share on other sites More sharing options...
ldd Posted February 11, 2017 Share Posted February 11, 2017 reasons for not putting it inside 'update': - basically, update is called once every 1/60 seconds. setTimeout is a very lazy thing. It basically doesn't want to work immediatly, and update is all there always updating the game... unless you put a flag like I mentioned before, you are just asking for trouble by putting setTimeout there. reasons for not putting it inside 'create': - you are probably doing (or going to do) other things inside create. so calling create recursively won't do. Creating a function inside create is asking for trouble, again.You could put in inside create, but you need to remember to CALL it at least once outside the function itself. Anyways, clean simple code is always better than a mess. Link to comment Share on other sites More sharing options...
samme Posted February 11, 2017 Share Posted February 11, 2017 I must recommend game.time.events.add over setTimeout. Link to comment Share on other sites More sharing options...
QuentinIcky Posted February 16, 2017 Author Share Posted February 16, 2017 Thanks @ldd for your explanations. It's more clear. And thanks you @samme I'm gonna see this. Why do you recommend that ? Link to comment Share on other sites More sharing options...
samme Posted February 18, 2017 Share Posted February 18, 2017 On 2/16/2017 at 1:12 PM, QuentinIcky said: And thanks you @samme I'm gonna see this. Why do you recommend that ? It's simpler, more performant, and more accurate. And it will pause when the game pauses. Link to comment Share on other sites More sharing options...
Recommended Posts