Events are just functions with reserved names that are called when different things happen. So to handle an event, just create a function in script with one of the following names...
Events
onClick |
Called every time the object is clicked. [Applies to object script only] |
onUpdate |
Called every time a frame is displayed. |
onInit |
Called once when the document is first initialized. |
onMouseDown |
Called when a mouse button is pressed. |
onMouseMove |
Called when the mouse is moved. |
onMouseUp |
Called when a mouse button is released. |
onMouseWheel |
Called when the mouse wheel is scrolled up or down. The amount is stored as a floating point number in the event structure as 'mouseWheelDelta'. +1.0 is a single notch up, and -1.0 is a notch down. |
onHoverBegin |
Called when the mouse moves from not over to being over an object. [Applies to object script only] |
onHoverEnd |
Called when the mouse moves from being over an object to not being. [Applies to object script only] |
onKeyChange |
Called when a keyboard key is pressed or released. |
onShow |
Special event that is called whenever a keyframe is displayed. [Applies to keyframe scripts only] |
onCollision |
Called when two objects that have the notifyCollision flag set on them collide. As this applies to object script only, the parameter passed to the event is the id of the other object hit. |
onCollisionBegin |
Same as the onCollision event, but only called once when the objects first collide. |
onCollisionEnd |
Same as the onCollision event, but only called once when the objects finish colliding. |
onRender(layerid) |
Called every time a frame is rendered. The scene might occur in several layers, so the layer id being drawn is also supplied as a string parameter. |
onPhysicsContact(objectid) |
Called when an object set to be solid touches another solid object. The function is only called once until the objects have separated. |
onPhysicsSeparate(objectid) |
Called when an object set to be solid is no longer touching another solid object. The function is only called once until the objects are in contact again. |
onPathBlocked() |
If an object is following a path but it is obstructed by an object en-route, this function will be called. |
onPathComplete() |
If an object is following a path and makes it to it's final destination, this function will be called. |
onInputBegin() |
Called when an object in receiving text input. |
onInputChange() |
Called when an object is changed due to text input. |
onInputEnd() |
Called when the user has finished changing the object from text input. |
onEffectObjectEnter(effectObjectId) |
Called when the object enters an effect object with volume (currently applies to an effectcube in the Objects window). This will be called once when the origin of the current object enters the volume, and the Id of the effect object will be passed in. For example, if you made an effect cube called 'underwater', you could use this event to apply a screen effect and sound when the viewer went under the sea. |
onEffectObjectLeave(effectObjectId) |
Called when the object leaves an effect object with volume (effectcube). See onEffectObjectEnter for more information. |
onExitRequest(e) |
Called when the user has tried to close the application (not in browser mode). Set system.exitAbort to true to abort the exit and continue. |
onBackgroundLoadComplete(e) |
Called when a file loaded with 'idle' set to true has completed loading. The event structure e will have members loadFilename set to the filename and loadSuccess set to true if the load succeeded or false if it failed. |
onControlNotify(info) |
This event will be called if an object is attached to a dxcontrol, and the control calls the function scene.controlNotify(info) to signal a change. The info can be any string to provide optional additional information. |
onVariableSet(variableId) |
This event will be called whenever a variable is set using script.xxx access. The variableId is the identifier of the variable passed as a string. |
Event Structure
Every event mouse/keyboard event also has the option to receive an event structure that describes the detail of the event. To get this, just add a variable in the function parenthesis, by convention say calling this 'e'. For example...
function onMouseWheel(e) // note the 'e' now in the definition
{
print("Mouse wheel changed by: "+e.mouseWheelDelta);
}
The event structure contains the following members...
srcElement |
A string containing the id of the object that raised the event (if any). |
clientX |
X position of the mouse when the event occured (integer). |
clientY |
Y position of the mouse when the event occured (integer). |
button |
The mouse button(s) involved in the event (integer). |
ctrlKey |
Boolean value, true if the Control key was down when the event occured. |
altKey |
Boolean value, true if the Alt key was down when the event occured. |
shiftKey |
Boolean value, true if the Shift key was down when the event occured. |
mouseWheelDelta |
Amount the mouse wheel changed for the event - a floating point number, -1 per notch down or +1 per notch up. |
rawDX |
Raw amount the mouse was moved by in the X direction (integer). |
rawDY |
Raw amount the mouse was moved by in the Y direction (integer). |
keyId |
The string identifier of the key changed in an onKeyChange event. |
keyDown |
A boolean for an onKeyChange event that if true represents a key press down. |
Examples
To handle the onClick event, edit the script for an object and enter the following code...
function onClick()
{
print("You clicked!");
}


