The rectangle object
This is easy to confuse with the rectangle shape, which we have explored earlier. The rectangle object is a visible 4-sided figure on screen which is not the scene.

Creating such an object
This is simple. The game object defines the addRect(id,pos,col) method. The inputs are exactly the same as those of setupScene, however, a color must be specified. The function returns the rectangle object - NOT the DOM id returned by setupScene.

Properties of the rect object
The id property is a string containing the id of the rectangle object in DOM. It is not recommended that you access this directly. The animating property is true when you have set the rectangle to animate. The variables position and color store the position (as a rect) and color (as an ldk color) of the object at present. The depth property specifies the z-Index of the object.
Changing these properties by themselves has no effect. You also need to call the update() method after your changes have been made.

To move the object by an (xV,yV) vector, use the method move(xV,yV). To place the object at a new position, use the function place(r). To help you find a good position to place the object, the method centreOn(p) returns the rect of the object transformed so that it centres on p.

The function animate(r,c,t) causes the object to move to the position and scale of rectangle r, and change color until it is the color c. This happens over the course of the time t, which is in milliseconds.

To stop the animation at any time, call the stop() method. To find out how fast an object is animating, use the getSpeed() method.

To cause an object to move indefinitely, change the variables speedX and speedY to your desired values and call the drive() function periodically. Each time you call the drive function, you move the object by the speeds specified.

Deleting the object
To do this, simply call the remove() method. Make sure you never reference the instance again!

Calling events
Sometimes you might want a function to run before or after the animation, or when the object is just about to be deleted. For this, use the events array, a property of the rectangle object. The following global constants specify the number of the events array at which you should store your functions: ldk_animationEnd, ldk_animationStart and ldk_onDelete. For instance, to call a method when an object is deleted, you might call[ldk_onDelete]=myMethod;

Last edited Dec 13, 2009 at 11:25 PM by larztheloser, version 2


No comments yet.