let AC_GAME_OBJECTS = []; class AcGameObject { constructor() { AC_GAME_OBJECTS.push(this); this.time_delta = 0; // 距离上一帧的事件间隔 this.has_call_start = false; // 是否调用过start函数 } start() { // 最开始执行一次 } update() { // 每一帧执行一次(除了第一帧) } destory() { for (let i in AC_GAME_OBJECTS) { if (AC_GAME_OBJECTS[i] === this) { AC_GAME_OBJECTS.splice(i,1); break; } } } } let last_timestamp; // 上一帧执行完的时刻 // timestamp: 当前时刻 let AC_GAME_OBJECT_FRAME = (timestamp) => { for (let obj of AC_GAME_OBJECTS) { // 所有对象执行一次start函数 if (obj.has_call_start === false) { obj.start(); obj.has_call_start = true; } else { obj.time_delta = timestamp - last_timestamp; obj.update(); } } last_timestamp = timestamp; requestAnimationFrame(AC_GAME_OBJECT_FRAME); // 递归执行该函数 } requestAnimationFrame(AC_GAME_OBJECT_FRAME); export { AcGameObject, }