Class R.engine.BaseObject
Extends
R.engine.PooledObject.
The base object class which represents an object within
the engine. Objects of this type can have an associated DOM element,
and will also inherit the #update method to perform processing
for every frame generated.
Defined in: baseobject.js.
Constructor Attributes | Constructor Name and Description |
---|---|
R.engine.BaseObject(name)
Create a base object.
|
Method Attributes | Method Name and Description |
---|---|
addEvent(ref, type, data, fn)
Add an event handler to this object.
|
|
addEvents(handlers)
Helper method to simplify wiring event handlers to event types.
|
|
destroy()
Destroy the object, cleaning up any events that have been
attached to this object.
|
|
Get the class name of this object
|
|
Get the element which represents this object within its rendering context.
|
|
jQ()
A helper method to provide access to the jQuery object wrapping the
element for this object.
|
|
release()
Release the object back into the object pool.
|
|
removeEvent(ref, type)
Remove the event handler assigned to the object for the given type.
|
|
setElement(element)
Set the element which will represent this object within
its rendering context.
|
|
triggerEvent(eventName, eventObj, data)
Trigger an event on the object.
|
|
update(renderContext, time, dt)
Abstract update method to set the state of the object.
|
- Methods borrowed from class R.engine.PooledObject:
- clearObjectDataModel, getId, getName, getObjectDataModel, getProperties, isDestroyed, setName, setObjectDataModel, toString, toXML
Class Detail
R.engine.BaseObject(name)
Create a base object.
- Parameters:
- name
- {String} The name of the object
Method Detail
addEvent(ref, type, data, fn)
Add an event handler to this object. Within the
event handler, this refers to the object upon which the event is being triggered. It is
possible to bind an event simply by calling addEvent with the type and callback, like
so:
this.addEvent("click", function(evt) { this.doSomething(evt); });However, if you need to reference another object during the binding process, such as when a render context is binding an event to a game object, you could pass a reference object as the first argument:
// "this" refers to the render context someObj.addEvent(this, "click", function(evt) { // Inside the handler, "this" is the target of the event this.doSomething(evt); });The purpose behind this is that if the render context assigned the event, it should probably remove the handler, rather than the game object needing to remove the handler. But, if the game object also has a "click" handler, you don't want to remove that handler since the game object may still need it. If the event handler explicitly returns
false
(not null
, or
undefined
), further event handlers will not be processed.
- Parameters:
- ref Optional
- {Object} The object reference which is assigning the event
- type
- {String} The event type to respond to
- data Optional
- {Array} Optional data to pass to the handler when it is invoked.
- fn
- {Function} The function to trigger when the event fires
addEvents(handlers)
Helper method to simplify wiring event handlers to event types. You can assign
multiple handlers, by name, using object or array notation, for example:
// Shorthand this.addEvents({ "keydown": function(evt, which) { this.onKeyDown(which); }, "keyup": function(evt, which) { this.onKeyUp(which); } }); // Super shorthand this.addEvents(["onKeyDown", "onKeyUp"]);Object notation allows you to still be explicit in which handler method to apply or call, and the arguments which are handled. Array notation will simply create a method call which takes the names of the event handlers, removes "on" and then lower cases the remainder to create the event assignment. In the example above, onKeyDown is the name of your method to be called, and keydown will be the name of the actual event.
- Parameters:
- handlers
- {Object|Array} The event assignments
destroy()
Destroy the object, cleaning up any events that have been
attached to this object. Calls the destroy event
before the object is destroyed.
{String}
getClassName()
Get the class name of this object
- Returns:
- {String} "R.engine.BaseObject"
{HTMLElement}
getElement()
Get the element which represents this object within its rendering context.
- Returns:
- {HTMLElement} The HTML element
{jQuery}
jQ()
A helper method to provide access to the jQuery object wrapping the
element for this object. This allows direct access to the DOM.
- Returns:
- {jQuery} A jQuery object
release()
Release the object back into the object pool.
removeEvent(ref, type)
Remove the event handler assigned to the object for the given type. The optional
ref argument is used when another object assigned the event handler, such as:
// Handler #1 someObject.addEvent("click", function(evt) { this.doSomething(evt); }); // Handler #2 someObject.addEvent(anotherObject, "click", function(evt) { this.doSomething(evt); });You would remove the "click" handler that anotherObject assigned (handler #2), and not one that was bound by someObject (handler #1):
someObject.removeEvent(anotherObject, "click");
- Parameters:
- ref Optional
- {Object} The object reference which assigned the event
- type
- {String} The event type to remove
setElement(element)
Set the element which will represent this object within
its rendering context.
- Parameters:
- element
- {HTMLElement} The HTML element this object is associated with.
triggerEvent(eventName, eventObj, data)
Trigger an event on the object. Event handlers assigned with
#addEvent will be triggered and passed the event object
and the data. The scope of the listener function will be the
object.
- Parameters:
- eventName
- {String} The event to trigger
- eventObj Optional
- {Event} The original event object
- data
- {Array} An array of data to pass to the event handler
update(renderContext, time, dt)
Abstract update method to set the state of the object. This method
will be called each frame that is generated by the engine. The
context where the object will be rendered is passed, along with the
current engine time. Use this method to update components and
perform housekeeping on the object.
- Parameters:
- renderContext
- {R.rendercontexts.AbstractRenderContext} The context the object exists within
- time
- {Number} The current engine time, in milliseconds
- dt
- {Number} The delta between the world time and the last time the world was updated in milliseconds.