0

I am running into an issue where I want to change the value of my slider controller (just adding 1) each time it passes by a marker. This is my current code

if (marker.numKeys > 0) { value = marker.nearestKey(time).index; }

However, the issue I am running into is that midway between markers it changes value.

3 Answers 3

0

You just need to check if the nearest marker is in the past or the future. If it's in the future, just subtract 1 from its index. This avoids the use of a loop.

if (marker.numKeys > 0) {
    nearestMarker = marker.nearestKey(time);
    if (nearestMarker.time > time){
        nearestMarker.index - 1;
    } else {
        nearestMarker.index;
    }
}

You can shorten this using a ternary operator—at the cost of readability—to

if (marker.numKeys > 0) {
    nearestMarker = marker.nearestKey(time);
    nearestMarker.index - ((nearestMarker.time > time)? 1 : 0 )
}
0

marker.nearestKey picks the closest marker - even if it is in the future.

A simple solution would be to iterate over the markers with a for loop instead:

if (marker.numKeys > 0) {
    let i;
    for (i = 0; i < marker.numKeys; i++) { // For every marker we have, add 1 to i
        if (marker.key(i+1).time > time)   // If the marker is past current time,
            break;                         // stop the loop
    }
    i; // Return i value
}
0

If anyone is interested, here is a solution from the Adobe forums:

m = marker;
val = 0;
if(m.numKeys > 0){
  n = m.nearestKey(time).index;
  if (time >= m.key(n).time) n = Math.min(m.numKeys,n+1);
  t = m.key(n).time - time;
  val = linear(t,0,framesToTime(5),n,n-1);
}
val

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.