requestAnimationFrame
은 꽤 낮은 수준이며, 이미 말한대로 작동합니다. 대략 60fps로 호출됩니다 (브라우저가 속도를 따라갈 수 있다고 가정). 따라서 일반적으로 게임 루프가있는 게임 엔진과 마찬가지로 그 위에 뭔가를 만들어야합니다. 내 게임 엔진에서
,이 (paraphased/단순화 여기)이 있습니다
window.requestAnimationFrame(this._doFrame);
...
_doFrame: function(timestamp) {
var delta = timestamp - (this._lastTimestamp || timestamp);
for(var i = 0, len = this.elements.length; i < len; ++i) {
this.elements[i].update(delta);
}
this._lastTimestamp = timestamp;
// I used underscore.js's 'bindAll' to make _doFrame always
// get called against my game engine object
window.requestAnimationFrame(this._doFrame);
}
그런 다음 내 게임 엔진의 각 요소는 자신을 업데이트하는 방법을 알고있다.
update: function(delta) {
this.elapsed += delta;
// has 8 seconds passed?
if(this.elapsed >= 8000) {
this.elapsed -= 8000; // reset the elapsed counter
this.doMyUpdate(); // whatever it should be
}
}
requestAnimationFrame
오히려 낮은 수준이다와 함께 캔버스 API, 그들은
있습니다 : 귀하의 경우 업데이트해야 각 요소에서 각 2, 6은 8 초 경과 된 시간을 추적하고 그에 따라 업데이트해야합니다 애니메이션 및 게임 엔진과 같은 것들을위한 빌딩 블록. 가능하다면 나는 cocos2d-js와 같은 기존의 것을 사용하려고합니다.
감사합니다! 이게 내가 한 일이야. 조금 더 파헤 치면 다음과 같은 사실이 드러났습니다. https://gist.github.com/1002116 이것은 본질적으로 가지고있는 것입니다. 내 실수는 하나의 메인 루프와 각 객체의 루프 내에서 작동하도록하려는 것입니다. –