大廠有完整編輯器(時間軸、圖層、狀態機),中小團隊需低成本、可維護的方案。本文提出參考 Flash「圖層→動作→幀序列」層級結構的資料驅動動畫系統,無需圖形化編輯器,純腳本/資料定義即可。
// 偽代碼結構
AnimationSet = {
layers: [ // 圖層列表,索引 0 為最底層
{
id: "bg",
actions: { // 動作映射表
"idle": { frames: [0,1,2,3], fps: 8 },
"shine": { frames: [4,5,6,7,8], fps: 12 }
}
},
{
id: "character",
actions: {
"walk": { frames: [10,11,12,13], fps: 10 },
"attack": { frames: [20..30], fps: 15 }
}
}
// 更多圖層...
]
}
class AnimationPlayer {
AnimationSet set;
int currentLayer = 0;
String currentAction = "idle";
int frameIndex = 0;
int frameTimer = 0;
void update() {
for (Layer layer : set.layers) {
Action act = layer.actions.get(layer.currentAction);
if (act == null) continue;
frameTimer++;
if (frameTimer >= act.frameDelay) { // frameDelay = 1000/fps 或幀數
frameTimer = 0;
frameIndex = (frameIndex + 1) % act.frames.length;
// 可檢查單次播放結束觸發回調/切換動作
}
// 繪製當前幀
int imgIdx = act.frames[frameIndex];
drawImage(layer, imgIdx);
}
}
}
| 特性 | 說明 |
|---|---|
| 資料驅動 | 美術產出圖集+JSON/二進位定義,程式零修改支援新角色 |
| 圖層合成 | 角色裝備、表情、特效疊加無需硬編碼 |
| 動作融合 | 上半身攻擊+下半身行走 → 兩圖層獨立播放不同動作 |
| 記憶體友善 | 圖集共享,僅存索引與計時器 |
| 腳本擴展 | 可加入事件幀(音效、碰撞箱生成、粒子觸發) |
AnimationSet 載入 → AnimationPlayer 實例化 → 每幀 update(delta) + render(g)。drawImage 次數。「思想比代碼重要」。參考 Flash 成熟架構,以圖層-動作-幀序列三級結構建立資料模型,配合極簡播放器,即可在無編輯器環境下實現專業級動畫表現。適合 2D RPG、ACT、STG 等專案快速落地。