저는이 튜토리얼을 사용하여 저의 배정을위한 간단한 JS 게임을 작성하는 데 도움을 줬습니다. 그러나 나는 지금 gameloop을보고 있으며이 특정 기능이 어떻게 작동하는지 전혀 모른다.date.now() '는'여기서 무엇을합니까? - 자바 스크립트
다음은이 자습서의 URL입니다. "
: 또한
// Let's play this game!
var then = Date.now();
reset();
main();
다음 설명 : 당신이 찾고있는 코드의 블록은 8"주요 게임 루프 " http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/
//Gameloop
var main = function() {
//var with timestamp
var now = Date.now();
//where does 'then' come from? I never declared it.
var delta = now - then;
//next up it just calls another func and provides parameter delta divided by 1000 which is the amount of miliseconds in a second
update(delta/1000);
//and it calls my render function
render();
//then it sets then back to Date.now()
then = now;
//No idea what this line does. still looking into it
requestAnimationFrame(main);
};
일부 예제 소스 코드에서 선언을 찾으려고했기 때문에이 질문을 주제로 끝내기로했습니다. – rlemon
'then'은 의심 할 여지없이 - 마스터 클로저의 계층 구조의 상위 범위에서 선언됩니다 . 표현 함수 변수 'main'은 호출 사이의 시간 간격을 (의심 할 여지없이) 업데이트하고 해당 프레임을 렌더링하는 데 사용됩니다. '다음'을'now'로 업데이트하여 같은 표현식 (main)을 호출하는 후속 'requestAnimationFrame'을 위해 준비하고, 새로운 업데이트와 델타 계산을합니다. –