How to delay something for a certain period of time?

Hi. I have forked @will 's Flappy Bird game, and I am modifying it so it is more like the original game.

            var zrot = pc.math.clamp(this.velocity, -2, -0.75);
            zrot += 1;
            this.entity.setLocalEulerAngles(0, 0, zrot * 90);

From what I understand, this piece of code makes the bird face upwards (around 22.5 degrees) and fall down looking 90 degrees at the floor.

However, I want to delay this slightly so the bird looks upwards for slightly longer, and then faces downwards. I have tried using the timeout function, but after the period of time I set, the bird freezes and can no longer jump.

Any help with this problem is appreciated, thank you :slight_smile:

Here is a post on how to add a timer and execute something after a period of time.

Though two things if you don’t mind :slight_smile: :

  • Avoid opening a new thread for similar topics
  • Use the forum search method before posting, there are diamonds of knowledge hidden :wink:

The dt variable and update function is the standard way and I’ve used it a lot. However, someone recently posted the following chunk of code that I think he called a “null tween” - null, because no entity is actually tweened. It is just a sneaky way to get access to the time delay function built into tween.js.

It requires that you have the tween.js library loaded, so it may not be a good approach if you don’t want to do that. It is a good idea to get familiar with the update function, but I’ve found this simple chunk of code to be very handy for when I just want something delayed in a simple way. The “1” is the amount of delay in seconds.

this.app.tween({}).to({}, 1, pc.Linear)
    .on('complete', function() {
        // do whatever here
    }, this).start();

Thanks @wturber
Is there any way to stop the timer so it can reset once the event has restarted?
Thanks :slightly_smiling_face:

This did not work for my case when I tried, it didn’t really do anything. @wturber’s response did help but I need to know how to restart it for every click. :slight_smile:

I’m very far from being an expert coder, but my experience is that the “null tween” timer ends and will restart when called again. Note that “do whatever here” follows the .on(‘complete’). That is because the tween has completed. Calling it again should restart it. Though I can imagine possible complications if you call it again before it has had time to complete the first time.

@_Lio3LivioN, @wturber suggestion seems great, but if you are trying to execute that code in your Flappy bird project that you posted in the other topic I think it will fail.

That project uses an older version of the tween library. Try experimenting in an empty project first by attaching the latest tween library, to check how the code should behave.