ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Position increment

FAQ   •   VIEW ALL
 Position increment on Feb 22, 2018 at 2:39:07 pmLast Edited By Bartholomé Enake on Feb 22, 2018 at 6:06:47 pm

Hello,
I come to ask for help for the following subject:

I made a composition with a ball, an imaginary line (x = 250) and a counter.
The principle is as follows : the counter counts the number of frames when the ball is on the left.
The counter stops when the ball is on the right and continues to counting when it returns to the left.

It looks like that :

And here is the code of the counter, it works perfectly :
```balle = thisComp.layer("Balle"); compteur =0; for (i = 0; i <= timeToFrames(time); i++) { if (balle.transform.position.valueAtTime(framesToTime(i))[0]<=250) { compteur++; } } compteur```

Now I would like to animate the counter's position and ask him to go up 1 pixel (so down the Y position) as soon as he counts a frame. Here is the code that I entered on the same logic of the first and based only on the counter and not on the ball :

```pY = effect("Nombres")("Position")[1]; valeurAvant = effect("Nombres")("Valeur/Décalage/Maxi aléatoire").valueAtTime(framesToTime(timeToFrames(time)-1)); valeur = effect("Nombres")("Valeur/Décalage/Maxi aléatoire").valueAtTime(time); ecart = valeur-valeurAvant; marche = -1; for (i= 0; i <= timeToFrames(time); i++) { if (ecart==1){ pY = effect("Nombres")("Position").valueAtTime(framesToTime(i))[1] + marche ; } } [value[0],pY] ```

This code doesn't work and I have almost tried everything (I say almost because there is a solution that I didn't find ...).
I try to:
• Put the variables in the loop, it does not work.
• Debugging each variable in separate text layers and they seem to work
• To insert this code and to adapt it in a dissociated position, it does not work.

Well, it's been a few days that I drool before this code but nothing works. 😳
If anyone would be able to tell me where it's wrong ... 😉
Thank you !

 Re: Position incrementon Feb 22, 2018 at 6:19:36 pm

I'm not completely sure, but it looks like you are encountering one of the limitations of expressions, which is that an expression only has access to the pre-expression value of the property hosting the expression. So anything your expression has done to that property on previous frames is gone on the current frame. Your expression needs to loop back in time, frame by frame, until you find the frame where an event occurred, calculate how long ago that was, and calculate the appropriate result based on that amount of time.

Dan

 Re: Position incrementon Feb 23, 2018 at 8:53:57 am

But how does this make my first expression work while it is based exactly on the same system.
I even try to transform the expression of the position so that it is based on the ball and not on the meter but it doesn't work either.

Here is the code :
``` pY = effect("Nombres")("Position")[1]; balle = thisComp.layer("Balle"); marche = -1; for (i = 0; i <= timeToFrames(time); i++) { if (balle.transform.position.valueAtTime(framesToTime(i))[0]<=250) { pY = effect("Nombres")("Position").valueAtTime(framesToTime(i))[1] + marche ; } } [value[0],pY] ```

Is there not a way to do what I want differently ?

 Re: Position incrementon Feb 23, 2018 at 2:26:20 pm

Try this:

``` balle = thisComp.layer("Balle"); deltaY = 0; for (i = 0; i <= timeToFrames(time); i++){ if (balle.transform.position.valueAtTime(framesToTime(i))[0]<=250){ deltaY++; } } value + [0,deltaY]; ```

Dan