Construct 2 is being retired on 1 Jul, 2021. We strongly recommend upgrading to Construct 3.

cr functions

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

cr.vector2 is a simple x,y position (also usable as a size). Create with: new cr.vector2(x, y)

Since r52, it is not recommended to use cr.vector2 in runtime scripts. The use of many temporary vector2s creates a lot of garbage collector overhead which can cause poor performance in some browsers. Prefer to directly manipulate separate x and y javascript numbers instead.

cr.vector2.x
cr.vector2.y
The x and y co-ordinates of the vector./dd]
cr.vector2.offset(x, y)
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

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.

cr.rect.left
cr.rect.top
cr.rect.right
cr.rect.bottom
Defines the position of the rectangle.
cr.rect.set(left, top, right, bottom)
Convenience function to set each member in one call.
cr.rect.width()
cr.rect.height()
Return the size of the rectangle.
cr.rect.offset(x, y)
Offset the rectangle by the given x and y co-ordinates.
cr.rect.intersects_rect(rc)
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.

cr.quad

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.tlx
cr.quad.tly
cr.quad.trx
cr.quad.try_ (note underscore to avoid "try" keyword)
cr.quad.brx
cr.quad.bry
cr.quad.blx
cr.quad.bly
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).
cr.quad.set_from_rect(rc)
Set the quad points to represent a shape that is identical to the given
cr.rect

object.

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.
cr.quad.bounding_box(rc)
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.
cr.quad.intersects_quad(q)
Test if the quad intersects another cr.quad.

cr.ObjectSet

An object set represents a mathematical set of javascript objects, i.e. each object can only be stored once or not at all, never twice. Adding an object to an ObjectSet that already contains it has no effect. The ObjectSet uses the .toString() method to distinguish objects, so any objects stord in the ObjectSet must have an appropriate overload that uniquely identifies it. Since the underlying container is a javascript object, performance is better than using an array and lookups to store unique objects.

Create a new ObjectSet with: new cr.ObjectSet()

cr.ObjectSet.contains(x)
Test if the set contains object x.
cr.ObjectSet.add(x)
Add the object x to the set if not already present, else no effect.
cr.ObjectSet.remove(x)
Remove the object x from the set if present, else no effect.
cr.ObjectSet.clear()
Remove all objects from the set, returning it to an empty state.
cr.ObjectSet.isEmpty()
Returns true if the object set is in an empty state.
cr.ObjectSet.count()
Return the number of objects stored in the set.
cr.ObjectSet.valuesRef()
Return a read-only reference to the javascript array containing all the objects in the set. This is faster than values().
cr.ObjectSet.values()
Return an array containing a copy of all the objects in the set. Since it returns a copy, the result can be modified.

Utility functions

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.
cr.to_radians(a)
Return a converted from degrees to radians.
cr.to_degrees(a)
Return a converted from radians to degrees.
cr.clamp_angle_degrees(a)
Return a wrapped in to the range [0, 360)
cr.to_clamped_degrees(a)
Return a converted from radians to degrees and wrapped in to the range [0, 360).
cr.to_clamped_radians(a)
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)
Return logical XOR of x and y (javascript has no native operator for this).
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.
cr.seal(o)
Seal the object o. If ECMAScript 5 is not supported, has no effect.
cr.freeze(o)
Freeze the object o. If EXMAScript 5 is not supported, has no effect.
Construct 2 Javascript SDK Manual 2020-06-05