 # AE Expressions: a Basic Count-Down and Hold

## Recommended Posts This came from a question on another forum. How to have a type layer count from 100 to 0.

Here's the expression:

t = (1 / thisComp.frameDuration) * time ;

count = 100 - t ;

if (count < 0){

0

}else{

Math.floor(count)

}

If we add this to the "source type" of a layer, this will do a simple countdown from 100 down to 0 for each frame that passes. How? Let's look!

1/thisComp.frameDuration

This is equal to the time it takes for one frame to elapse in seconds.

time

is the current time in seconds. Therefore,

t = (1 / thisComp.frameDuration) * time ;

declares 't' to be equal to the current time in frames, regardless of fps.

Then we say

count = 100 - t ;

So, instead of going 0 - 100, now we have a value going from 100 to 0. There's probably a mathematical term for doing this flip-around, but I don't know what it is.

Now, thinking ahead, if we were making a countdown to zero, we want our countdown to stop at zero. So, we need to do a simple test:

if (count < 0){

0

If 'count' is less than zero, then pass the value of 0. Therefore we will never see a negative number.

}else{

If it is not less than zero.. then do this:

Math.floor(count)

We are handing this value to be our source text. First, let's just say we had "count" here. Our source text would display a 100 - 0 countdown occasionally including a non-integer number like 4.9999999. Why? Because NTSC sucks. Our math is dealing with this multiple of 29.97 which yields some crazy numbers occasionally. If you are using PAL, then you might not even need this. BUT, we should always make our code bullet-proof.

So, instead we need to round our number to the integer closest to and not greater than 'count'. Hence, Math.floor(count).

Make sense?

Word.

Edited by graymachine

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×

• #### Activity

×
• Create New...