Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Generate items with delay


QuentinIcky
 Share

Recommended Posts

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

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

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

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

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

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

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

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...