HTML and CSS Reference
In-Depth Information
this.context = this.canvas.getContext('2d');
this.main = new Main().Initialise();
setInterval(function() {objectManager.Draw();},
frameTime);
return this;
}
5.
With the canvas element initialized, we can then move on to adding objects to the
application. This is achieved by pushing a number of items to the previously declared
array of objects, which can be seen in the preceding code. We will also look at how to
remove these objects once we are inished with them.
this.AddObject = function(Object) {
this.objects.push(Object);
this.objects.sort(function(a,b){return a.z - b.z;})
};
this.RemoveObject = function(object) {
for (var i = 0; i < this.length; ++i) {
if (this[i] === object) {
this.remove(i);
break;
}
}
}
6.
You can see that we are attempting to remove an item from our objects array. However,
to do this, we must irst add a new function to JavaScript's built-in array object.
Array.prototype.remove = function (a, b) {
var rest = this.slice((b || a) + 1 || this.length);
if(a < 0)
this.length + a;
else
this.length = a;
return this.push.apply(this, rest);
};
7.
Now that we can add and remove objects to our framework, we can move on to our
inal part of the framework, drawing objects to our canvas. This is done by clearing
the canvas and then drawing each object to it every time the Draw function is called.
this.Draw = function () {
var frame = new Date().getTime();
 
Search WWH ::




Custom Search