I believe it is my usage of the Date() object. I am creating a new one every update in my system code ( it updates every 60 times a second ). I do this because I want a millisecond accurate clock useful for timers, animations, etc.
So I think it's because I use too much memory? Though I thought javascript has a kind of "trash" system that throws away unused "garbage". I've tried deleting variables of the Date but I get the same results of a slow down.
This is what's executed ever update:
Code:
function system()
{
// time reference
var Current = new Date();
// system loop here
step( Current ); // the local time variable gets passed to step
draw();
}
I presume your code performs screen updates, and I suspect that they can't keep up with the frequency of the function calls. You haven't shown enough code to be certain.
I presume your code performs screen updates, and I suspect that they can't keep up with the frequency of the function calls. You haven't shown enough code to be certain.
Problem: The fps is dramatically slowed.
The updates are run through a setInterval of function system 60 times a second.
This is unmodified version of the system
Code:
// init function
function init()
{
loadImages();
obj_player = new Player();
map_init();
background_color = "gray";
if(typeof game_loop != "undefined")
clearInterval(game_loop);
game_loop = setInterval( system, 1000/120);
}
// commence starting
init();
// system parts - unchanged, draw is not shown though (unnecessary)
function step( Current )
{
obj_player.step( Current );
}
function system()
{
// time reference
var Current = new Date();
// system loop here
step( Current );
draw();
delete Current;
}
The updates are run through a setInterval of function system 60 times a second.
The delete statement has no effect on local variables.
You are calling system() every 8ms, which is close to the maximum that a browser will allow, and far higher than the frequency that you can expect screen updates to occur.
Is it necessary to call draw() if .face is not updated?
The delete statement has no effect on local variables.
You are calling system() every 8ms, which is close to the maximum that a browser will allow, and far higher than the frequency that you can expect screen updates to occur.
Is it necessary to call draw() if .face is not updated?
By the way, the code showing 1000/120 for the milliseconds to update is a test. It was showing me that the program is fast at first but slows not much later in the code.
Also, yes this is just a very basic program. The person moves around, so if I do not update according face not changing, the person will stay frozen in the spot till otherwise.
Also, yes this is just a very basic program. The person moves around, so if I do not update according face not changing, the person will stay frozen in the spot till otherwise.
In that case you should only call draw() when that happens, then I'm sure you won't have a problem.
In that case you should only call draw() when that happens, then I'm sure you won't have a problem.
I think what I said, could have been taken two ways haha, sorry. I meant that if I don't call draw, my character will look like hes staying still when he should be moving. Also, draw needs to be called in order to update the view of the game on the screen. Is this the reason it's slowing down? Are there better ways of making loops for a game?
I think what I said, could have been taken two ways haha, sorry. I meant that if I don't call draw, my character will look like hes staying still when he should be moving. Also, draw needs to be called in order to update the view of the game on the screen. Is this the reason it's slowing down?
That is what I understood. Remove the call to draw from its shown location and insert it immediately after the switch statement in step.
Make certain you don't have multiple setIntervals active at the same time. If you start a second before clearing the first you will soon have as many processes running as the computer can handle.