Delay creation?

Hi, I am trying to transfer a game from java (using greenfoot) to js: here.

I am completely new to js, html and phaser. So I encountered this problem: I need to generate a bunch of objects (apples) that have animation to them. I also need them to be generate over time, in fact increasing over time, for example, at the beginning there will be 2 apples spawn per second, toward the end of the game, it should be something like 10.

So, my idea is to create 100 apples (because the goal of my game is to reach 100 apples). But I need some kind of delay between spawning apples. Any idea what I should do? Also, I need them to be animated.

Here is my code so far:
//red apple

    redApples = this.physics.add.group();
    for (var i =0; i < 6; i++)
    {
        redApples.create(120*i, getRandomInt(100, 400), 'redapple');
    }
    this.anims.create({
        key: 'animatedApple',
        frames: this.anims.generateFrameNumbers('redapple', {start: 0, end:1}),
        frameRate: 5,
        repeat: -1
    });

    redApples.children.iterate(function (child) {
        child.play('animatedApple', true);
    });

You can use the phaser timer to spawn a new apple all 500ms

this.time.addEvent({
    delay: 500,
    callback: ()=>{
        // spawn a new apple
    },
    loop: true
})
5 Likes

wow ok haha, this works perfectly, thanks!

2 Likes