In the rest of the documentation, you may have noticed references to cr.vector2 or cr.rect. These, and some other common functions, are declared in common_prelude.js. Like the rest of the runtime, they are in the cr namespace (for Construct Runtime). This section documents these classes and functions.
cr.vector2 is a simple x,y position (also usable as a size). Create with: new cr.vector2(x, y)
- The x and y co-ordinates of the vector./dd]
Vector addition. Modifies the vector2 offset() is called on.
- cr.vector2.mul(x, y)
- Vector multiplication. Modifies the vector2 mul() is called on.
cr.rect is a simple 2D rectangle. It is implicitly axis-aligned. Create with: new cr.rect(left, top, right, bottom)
Creating many rect objects can result in garbage collector overhead causing poor performance. Re-use existing rect objects wherever possible.
- Defines the position of the rectangle.
- cr.rect.set(left, top, right, bottom)
- Convenience function to set each member in one call.
- Return the size of the rectangle.
- cr.rect.offset(x, y)
- Offset the rectangle by the given x and y co-ordinates.
- Test if the rectangle intersects another cr.rect.
- cr.rect.contains_pt(x, y)
- Test if the point lies inside or on the border of the rect.
A quad is simply four 2D points that form a four-sided shape. Unlike cr.rect, quads can represent rectangles at any angle (non-axis-aligned). Quads do not have to store a rectangle - each of the four points can have any position at all - but in the runtime, they are always used for the purpose of rotated rectangles.
Create a quad with: new cr.quad()
- cr.quad.try_ (note underscore to avoid "try" keyword)
- The co-ordinates of the four points making up the quad. The terms tl, tr, br and bl refer to "top left", "top right", "bottom right" and "bottom left" respectively (which correspond to their positions when representing an unrotated rectangle).
- Set the quad points to represent a shape that is identical to the given
- cr.quad.set_from_rotated_rect(rc, a)
- Set the quad points to represent a cr.rect rotated by a radians.
- cr.quad.offset(x, y)
- Offset the quad by a point.
- Set the cr.rect rc to a rect representing the axis-aligned bounding box of the quad.
- cr.quad.contains_pt(x, y)
- Test if the point lies inside the quad.
- Test if the quad intersects another cr.quad.
Create a new ObjectSet with: new cr.ObjectSet()
- Test if the set contains object x.
- Add the object x to the set if not already present, else no effect.
- Remove the object x from the set if present, else no effect.
- Remove all objects from the set, returning it to an empty state.
- Returns true if the object set is in an empty state.
- Return the number of objects stored in the set.
- Return an array containing a copy of all the objects in the set. Since it returns a copy, the result can be modified.
The following functions are not all related, but are often useful.
- cr.RGB(red, green, blue)
- Generate a color value. Useful for color parameters.
- cr.arrayRemove(arr, index)
- Modify the array parameter arr to remove the element at index.
- cr.arrayFindRemove(arr, item)
- Modify th array parameter arr to remove the element equal to item.
- cr.clamp(x, a, b)
- Return x, or a if x is lower than a, or b if x is greater than b.
- Return a converted from degrees to radians.
- Return a converted from radians to degrees.
- Return a wrapped in to the range [0, 360)
- Return a converted from radians to degrees and wrapped in to the range [0, 360).
- Return a converted from degrees to radians and wrapped in to the range [0, 2pi).
- cr.angleDiff(a1, a2)
- Return the smallest angle, in radians, between a1 and a2.
- cr.angleRotate(start, end, step)
- Return start rotated towards end by step radians.
- cr.angleClockwise(a1, a2)
- Return true if a2 is clockwise of a1 (in radians) by the smallest angle.
- cr.xor(x, y)
- cr.lerp(a, b, x)
- Linearly interpolate a to b by x.
- cr.segments_intersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y)
- Test if the line a1 to a2 intersects the line b1 to b2.
- Seal the object o. If ECMAScript 5 is not supported, has no effect.
- Freeze the object o. If EXMAScript 5 is not supported, has no effect.