GameLogic.Gameloop Class
A class to manage the game logic and time.
Provide the simplest way to use a regular loop, splitting draw and update.
All elements added in object
are updated and draw when te loop is started.
Elements can be added with addObject and
rmObject. All kind of elements are supported:
if it's a function, it will be called during the update phase.
If it's an object, the gameloop search update
and draw
method for call them.
Note that you can safety call rmObject during an update or draw phase: the element will be deleted at the end of phase.
var gl = new Gameloop();
var win = new Window(canvasContext);
gl.add(win);
var nextRectTime = 5000;
gl.add(function(elapsedTime) {
nextRectTime -= elapsedTime;
//All 5sec, a rect is added.
if (nextRectTime < 0) {
var rect = new Rect({
x: Math.random() * 200
y: Math.random() * 200
});
//no need to add the rect to gl: gl will draw the window, which will draw the rects.
win.addChild(rect);
nextRectTime = 5000;
}
});
Gameloop also provides some interesting method for measure performances with getRealFPS and getRealTPS.
Constructor
GameLogic.Gameloop
()
Item Index
Properties
Methods
addObject
-
object
This method allows you to add an object to the Gameloop. when the gameloop is refreshing itself it tries to call the update and draw function of each object which are in its list. You can add any kind of object. you should add draw and update method to these objects because the gameloop will call them each cycle.
Parameters:
-
object
Objectit is an object which will be added to the Gameloop's internal list.
draw
()
draw the content of gameloop. called automatically at the beginning of each step.
getRealFPS
()
Number
this method returns the average fps off ten seconds.
Returns:
getRealTPS
()
Number
This method returns the average of TPS (average of update calls) in ten seconds.
Returns:
isRunning
()
Boolean
indicate if the loop is active or not.
Returns:
true
if loop is running; false
if the loop is stopped or paused.
pause
()
stop the update Gameloop Elements are still drawn, but not updated. You can resume the game with start
rmObject
-
object
This method allows you to remove an object from the Gameloop's list.
Parameters:
-
object
Objecta reference to the object that you want to suppress from the Gameloop's list.
start
()
start or unpause the gameloop. If gameloop is already stated, do nothing.
stop
()
stop the gameloop Both update and draw are stopped. The elements are not removed, so you can use start to resume play. If you need to keep the screen displayed, you should instead use pause.
update
()
update the logic one step. called automatically each step by start.
Properties
fps
Number
The value that limits the maximum number of frames per second. Used only if requestAnimationFrame is not found . Note: changes are effective only when gameloop is restarted.
Default: 30
object
Array
protected
array which contains all games elements.
You must add elements to object
for updating
and drawing these elements.
If an element is a function, it's called during update phase. If it's an object, its draw function will be called during draw phase, an its update function during update phase. If a function does not exist, the gameloop will ignore it. update and draw functions are not mandatory.
startDate
Date
a Date object which represents the instant when you called
the start method of the gameloop. null
if not started.
tickPerSecond
Number
The frequency of function calls update Note: changes are effective only when gameloop is restarted.
Default: 60