HTML and CSS Reference
In-Depth Information
The scene functionality is concise. It consists of a class called Q.Scene that has a simple purpose: Capture
a callback method and an optional options hash. Next it has a Q.scene method that acts as both a getter (when
passed one argument) and setter method (when passed two arguments).
The idea behind the scene functionality is that you want a self-contained way to set up a level or section of
your game, and putting in a self-contained method makes it easy to swap between different scenes.
Writing the Stage Class
The Q.Stage class is responsible for keeping track of a list of sprites and letting them update and render them-
selves. It's a good bit beefier than the Q.Scene class but does a lot more as well. Much of the code should look
familiar to the class from Chapter 2.
Add the definition of the Stage class from Listing 11-8 to the bottom of the Quintus.Scenes module
before the final closing curly braces.
Listing 11-8: The Overlap method and Stage class
Q.overlap = function(o1,o2) {
return !((o1.p.y+o1.p.h-1<o2.p.y) || (o1.p.y>o2.p.y+o2.p.h-1) ||
(o1.p.x+o1.p.w-1<o2.p.x) || (o1.p.x>o2.p.x+o2.p.w-1));
};
Q.Stage = Q.GameObject.extend({
// Should know whether or not the stage is paused
defaults: {
sort: false
},
init: function(scene) {
this.scene = scene;
this.items = [];
this.index = {};
this.removeList = [];
if(scene) {
this.options = _(this.defaults).clone();
_(this.options).extend(scene.opts);
scene.sceneFunc(this);
}
if(this.options.sort && !_.isFunction(this.options.sort)) {
this.options.sort = function(a,b) { return a.p.z - b.p.z; };
}
},
each: function(callback) {
for(var i=0,len=this.items.length;i<len;i++) {
callback.call(this.items[i],arguments[1],arguments[2]);
}
},
 
 
Search WWH ::




Custom Search