Question: I nned help with mu update in a game loop. User gioves us a interval and I times it by itself untill I get to
I nned help with mu update in a game loop. User gioves us a interval and I times it by itself untill I get to the user times amount. No mater what I do it's not working. Below are the instuctions and my JavaScript code.
Instuctions
With that said, there are some technical requirements I want followed.
Use a single JavaScript code file, name it anything you like.
Create a function named gameLoop in which you update the elapsed time since the last time the function was called.
The function will make a call to another function named update, which accepts the elapsed time as a parameter. It is in the update function where any active events are updated.
The function will make a call to another function named render. It is in the render function where any events that need reporting are displayed.
This function must use requestAnimationFrame to invoke the gameLoop function again.
Can not use setTimeout, setInterval or anything that causes asynchronous timer callbacks into your code. You have to track how much time has elapsed on each active event, and use that to know when to report or expire during the update part of your game loop.
When an event expires (# of times exhausted), it must be removed from the internal data structure(s) holding it.
My code
let events = [];
function buttonPressed(){ console.log('Events contains ' + events.length ' items');) inputAvailable = true; }
function processInput(){ if(inputAvailable){ let input = document.getElementById('id-input').value; console.log('The input contained: ', input); events.push(input);
inputAvailable = false; } }
function update(elapsedTime){ //not here }
function render(elapsedTime){ //here
while(elapsedTime != nTimes) delete elapsedTime;
requestAnimationFrame;
}
function gameLoop(){ update(elapsedTime); render(elapsedTime); requestAnimationFrame(gameLoop); }
function startGameloop(){ console.log('game starting...'); requestAnimationFrame(gameLoop); }
Any help would be very helpful. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
