跳到正文
DEV Community· liangqi zhao·· 4 小時前AI 評分44

如何為每個動畫影格設定不同時長並取樣循環

How to Sample a Loop with Different Durations for Each Animation Frame

AI 導讀

這篇教學示範如何按各動畫影格的時長,將經過時間映射至循環中的影格索引。以 [100, 150, 250] 毫秒為例,循環長 500 毫秒,500 毫秒時回到第 0 格;程式亦處理負時間,並拒絕非正數時長。教學提醒,遊戲事件應另按時間軸檢測,避免更新步長過大而跳過短影格。

正文

liangqi zhao

By Liangqi Zhao for MotifDeck, an AI game art workspace. This tutorial was prepared with AI assistance.

Uniform frame rates are convenient, but they make every image stay on screen for the same length of time. A short anticipation frame and a longer hold can be useful for a hand-authored animation. Here is a small, engine-independent way to map elapsed time to a frame when each frame has its own duration.

The input contract

Supply one positive duration in milliseconds per frame, in the same order as the images. For example, [100, 150, 250] describes a 500 ms loop. The first frame occupies 0–99 ms, the second 100–249 ms, and the third 250–499 ms. At exactly 500 ms the loop returns to frame zero.

function frameAt(durations, elapsed) {
  if (!durations.length ||
      durations.some(d => !Number.isFinite(d) || d <= 0) ||
      !Number.isFinite(elapsed)) {
    throw new Error('Expected positive durations and finite time');
  }

  const total = durations.reduce((sum, duration) => sum + duration, 0);
  let time = ((elapsed % total) + total) % total;
  for (let i = 0; i < durations.length; i++) {
    if (time < durations[i]) return i;
    time -= durations[i];
  }
  return durations.length - 1;
}

The double modulo keeps a negative time within the loop too; a caller can instead reject negative time if its application never seeks backwards. Validate frame count against the duration array before displaying the result. This example is generic JavaScript, not a claim about MotifDeck's export format.

Test the edges

For the example above, check 99 → frame 0, 100 → frame 1, 250 → frame 2, and 500 → frame 0. Also reject zero or negative durations so a malformed list does not produce ambiguous intervals. The example and these boundaries were checked locally with Node.js.

Keep rendering and gameplay separate

Use the returned index to choose the visual frame, but do not assume every game event must happen exactly when that index changes. A missed render or a large update step can skip over a short frame. Keep gameplay events on a timeline that can detect crossed intervals, and test them separately.

MotifDeck lets users inspect motion and choose frames and timing after generation. Its free plan is a limited preview; generated-file downloads require paid unlock. See the official site for current availability.

來源:DEV Community · dev.to