Pages

Friday, March 25, 2011

Forum rescue #002: Timer

Okay, time to tackle an easy but nonetheless important one!

Here's what U-Corp wrote (original thread):








So the task at hand is to have an object created and after 5 seconds destroyed. Sounds simple enough, but if you're going to do the following





you'll be out of luck.

A wait action might be useful in certain cases, but you have to keep in mind that it breaks picking. Go ahead and try it:

> Download current state cap file <

When five seconds are up, all instances of the sprite object you may have eventually created by clicking will be destroyed. And we really don't want that!

Of course there is a really easy solution: private variables! Instead of having a wait action delay the destroy command 5000 ms, give the sprite object a PV 'Lifespan' and set the initial value to 5000.

Now add an always event with an action that substracts 1000*TimeDelta from the PV 'Lifespan'. Multiplying it with TimeDelta makes sure that it will take one second to substract 1000, which is exactly what we want since we are dealing with ms here.


  
> Download current state cap file <

All that is left to do is to tell Construct to destroy the sprite when the PV reached a value of 0 or below.




Now if you click around, you will happily notice that it works. Every instance of the sprite object will be destroyed when its own five seconds ran out.

> Download final cap file <

As basic as this example is I do believe it's important to get the hang of how to use certain mechanics in Construct when you're starting out. Here's hoping this brief post can be helpful to some!


2 comments:

  1. Softblow, the answer is ALWAYS timedelta!

    Thanks for this nifty bit of information!

    ReplyDelete