I have a variable called auti. When you fire, it sets it to 5 and every frame reduces it by one. when it is equal to 0 then you can fire again. at 60 the fire rate is what I like.
Is there any way to remove the dependence of the frame rate? Playground lines 350-357 have two different methods of increasing a variable. line 350 should increase the variable si by one every frame, it runs every 1000/60 milliseconds. line 351 increase sf by one every frame. lines 352-355 stop the two functions when si hits 1000, and 356 and 357 show the variables in the onscreen '“console.”
why does si increase faster than sf?
Any help would be nice.
TLDR: The gun fires faster the higher the framerate.
You are updating the shots per frame, so it becomes dependent on the frame rate. You should either have a separate loop that would update after every set interval (like your si variable, which depends on setInterval’s callback), or keep a timer that would calculate the time elapsed every frame, and make update based on how much time has progressed.
Example:
let prevTime = date.now(); // time from prev frame
const updateInterval = 10; // update every 10ms
let carryOverElapsedTime = 0; // leftover time from previous prev
scene.registerBeforeRender(function() {
let currTime = date.now();
let elapsedTime = carryOverElapsedTime + currTime - prevTime;
prevTime = currTime;
let updateCount;
for (updateCount = 0; updateCount < elapsedTime/updateInterval; updateCount++) {
update();
}
carryOverElapsedTime = elapsedTime - updateInterval * updateCount;
});`
Of course there are better solutions out there but that just illustrates the basic idea.
Also, you can re-architecture your code so that update function takes in a time parameter i.e.: update( delta_t ). Make all your changes, including animation, etc, based on the delta_t. This is a better approach since the above solution is not guaranteed to be run every set interval (but approximated to run that once per interval on average after many frames), but takes more effort to make it work properly.
I just found a stupid simple solution. I had the akm firing every 6th frame. Because it is firing every sixth, it would fire 1000/60*6 seconds. Basically, every .1 seconds.
Math breakdown (for myself) 1000ms in a second. divide by 60 because 60,fps at every 6th frame is the desired fire rate. 1000/60 is how long between each frame. multiplied by 6 to represent the 6 frame firing interval. 100ms = .1 seconds.
That sounded long but all I had to do is set auti to 100 and every frame subtract dT from it until it reaches on or below zero!