Here’s a simple example:
An object named “Disc” already added to world, which changes its text to “Start!” when the simulation starts and changes again to “Stop!!” when the simulation stops:
The “OnStart” property can be particularly useful to initialise pa scene with objects kept at predefined locations. Ex to add 10 objects on x-axis at separation 1m each.
The "OnUpdate" Event
The argument passed to this event is instantaneous time since simulation is running.
Let's consider an example:
Say that you want to add discs to the simulation after every 100 steps
Step 1: Declare global variable t in onStart() event by adding script t=0; to onStart Script in world
Step 2: Create disc after every 100 steps
The "OnUpdate" property can also be useful to put constraint over object's motion.
To create simulation where three objects disc, disc1, disc2 follow each other. Create new simulation and add 3 discs (with default names) and arrange at vertices of triangle as shown
Now add following code to onUpdate() property of world script and save script (hopefully with no compilation error)
var r1= disc.getPosition();
var r2= disc1.getPosition();
var r3= disc2.getPosition();
on running simulation you will observe that each disc follows its next disc which seems too Cool!!
The "OnCollision" Event
Here's an example:
Say you want to exchange brushes of two bodies on collision.
var brush = body2.getBrush();
The "OnKeyPress" and "OnKeyRelease" Properties
The "OnKeyPress" and "OnKeyRelease" properties are called whenever a key is pressed and released respectively. Those two properties are called with a single argument: the key that was pressed or released.
Here's an example:
An object named "Disc" already added to world, which changes its text when the simulation starts and changes again to "Stop!!" when the simulation stops:
function onKeyPressed (keyCode)
Disc.setText("Key Pressed=" + key );
Disc.setText("Key Released=" + key );
You can check which key was pressed or released by comparing the variable key with KeyCode. So for example, if you would like to make an object "jump" whenever the UP arrow key is pressed, you'll have to set its "OnKeyPress" property to something similar to the snippet shown below: