HTML and CSS Reference
In-Depth Information
return this;
},
del: function(components) {
components = Q._normalizeArg(components);
for(var i=0,len=components.length;i<len;i++) {
var name = components[i];
if(name && this.has(name)) {
this.trigger('delComponent',this[name]);
this[name].destroy();
}
}
return this;
},
destroy: function() {
if(this.destroyed) { return; }
this.debind();
if(this.parent && this.parent.remove) {
this.parent.remove(this);
}
this.trigger('removed');
this.destroyed = true;
}
});
The base
Q.GameObject
class again inherits from
Q.Evented
, allowing it to listen for and trigger
events. The code has four main methods:
add
,
has
,
del
, and
destroy
. The first three are used to add, check
for, and remove components from an object respectively. The last method,
destroy
, is used to destroy the
object itself.
First is the
has
method, which checks if a
Q.GameObject
already has a certain component by checking if
the object has a property by the same name. This is a little risky because it relies on the developer to be careful
about component names and extended properties, but if there's a name conflict, other issues can result regard-
less.
Next the
add
and
del
methods add and remove components from a
Q.GameObject
, and they are almost
mirror images of each other. The
add
method loops over all the components to be added, looks them up in
Q.components
, and then creates the new component object. The
del
method does the reverse, looping over
the components to remove and calling the component's
remove
method for each.
Both methods have some additional logic in them to prevent the developer from adding or removing a com-
ponent more than once. Each also triggers an event with the component instance itself passed as the data argu-
ment.
The
destroy
method calls
debind
on the object, tries to remove it from its parent if it has one, and trig-
gers a
removed
event to allow any components to clean up if necessary. It also adds a
destroyed
property
to prevent the
destroy
method from being called more than once.