Search the Community
Showing results for tags 'setInterval'.
-
Hi All, I've seen discussions about the use of timers for control of timed repetition, and examples that use Phaser.time as well as others that use the setInterval mechanism. My question is - is there a best practice recommendation on which way is better? In my situation, I require the use of intervals in order to move a character sprite along a predefined path (generated by A*). There appear to be a number of different ways to do this, and the interpolation function popped up during my research as well. I'm keen to find out the trade offs and decision making involved by folks, and whether there is a recognised or standard best way to do it. Kind Regards, Andrew.
- 3 replies
-
- timer
- setinterval
-
(and 1 more)
Tagged with:
-
Do you want custom FPS with smooth animations? I have made a method that makes this possible. The first 1-3 seconds may be buggy, but after that it works smooth. The update() method is called (custom FPS) times per second. The draw() method is called how many times the browser refreshes per second. Here's the code: (mainCanvas is a canvas with the size 800,600) window.requestAnimFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ) { window.setTimeout(callback, Math.floor(1000 / 60)); };function step(){ for (var i = 0; i < updates.length; i++) { if (updates[i] == tick) { utick++; update(); } } tick++; var cursec = (new Date()).getSeconds(); if (cursec != prevsec) { prevsec = cursec; frametime = tick; tick = 0; uframetime = utick; utick = 0; updates = []; for (var i = 0; i < fps; i++) { var uind = Math.ceil(frametime / fps * i); if (uind >= frametime) { uind = frametime - 1; } updates[i] = uind; } } draw(); requestAnimFrame(step);}function update(){}function draw(){ ctx.clearRect(0, 0, 799, 599); ctx.fillText(uframetime.toString(), 10, 20);}var mainCanvas = document.getElementById("mainCanvas");var ctx = mainCanvas.getContext("2d");var fps = 30;var prevsec = (new Date()).getSeconds();var tick = 0;var frametime = 0;var utick = 0;var uframetime = 0;var updates = [];ctx.font="20px Georgia";step();
- 2 replies
-
- requestAnimationFrame
- FPS
-
(and 3 more)
Tagged with: