Objects
Tables and functions are objects; variables do not actually contain these values, only references to them. Tables are also used in what is known as object-oriented programming. Variables and methods that manipulate those variables can be collected together into objects. Such a value is called an object, and its functions are called methods. In Corona, we'll focus more on display objects since they are essential for game development.
Display objects
Anything drawn on the screen is made by display objects. In Corona, the assets you see displayed in the simulator are instances of display objects. You have probably seen shapes, images, and text, which are all forms of display objects. When you create these objects, you'll be able to animate them, turn them into backgrounds, interact with them using touch events, and so on.
Display objects are created by calling a function known as a factory function. There is a specific kind of factory function for each type of display object. For example, display.newCircle()
creates a vector object.
Instances of display objects behave in a manner similar to Lua tables. This enables you to add your own properties to an object as long as they do not conflict with the system-assigned properties and method names.
Display properties
The dot operator is used to access properties. Display objects share the following properties:
object.alpha
: This is the object's opacity. A value of 0 is transparent and 1.0 is opaque. The default value is 1.0.object.height
: This is in the local coordinates.object.isVisible
: This controls whether the object is visible on the screen. True is visible and false is not. The default is true.object.isHitTestable
This allows an object to continue to receive hit events even if it is not visible. If true, objects will receive hit events regardless of visibility; if false, events are only sent to visible objects. It defaults to false.object.parent
: This is a read-only property that returns the object's parent.object.rotation
: This is the current rotation angle (in degrees). It can be a negative or positive number. The default is 0.object.contentBounds
: This is a table with thexMin
,xMax
,yMin
, andyMax
properties in screen coordinates. It is generally used to map the object in a group to the screen coordinates.object.contentHeight
: This is the height in screen coordinates.object.contentWidth
: This is the width in screen coordinates.object.width
: This is in local coordinates.object.x
: This specifies the x position (in local coordinates) of the object relative to the parent—the parent's origin to be precise. It provides the x position of the object's reference point relative to the parent. Changing the value of this will move the object in the x direction.object.anchorX
: This specifies the x position of the object's alignment to the parent's origin. Anchors range from 0.0 to 1.0. By default, new objects have their anchor set to 0.5.object.xScale
: This gets or sets the x scaling factor. A value of 0.5 will scale the object to 50 percent in the x direction. The scaling occurs around the object's reference point. The default reference point for most display objects is center.object.y
: This specifies the y position (in local coordinates) of the object relative to the parent—the parent's origin to be precise.object.anchorY
: This specifies the y position of the object's alignment to the parent's origin. Anchors range from 0.0 to 1.0. By default, new objects have their anchor set to 0.5.object.yScale
: This gets or sets the y scaling factor. A value of 0.5 will scale the object to 50 percent in the y direction. The scaling occurs around the object's anchor point. The default reference point for most display objects is center.
Object methods
Corona can create display objects to store object methods as properties. There are two ways this can be done: using the dot operator (".
") or using the colon operator (":
"). Both are valid ways to create object methods.
This is an example of the dot operator:
object = display.newRect(110, 100, 50, 50) object.setFillColor(1.0, 1.0, 1.0) object.translate( object, 10, 10 )
This is an example of the colon operator:
object = display.newRect(110, 100, 50, 50) object:setFillColor(1.0, 1.0, 1.0) object:translate( 10, 10 )
The call to an object method using the dot operator is passed to the object if it's the first argument. The colon operator method is merely a shortcut with less typing involved to create the function.
Display objects share the following methods:
object:rotate(deltaAngle)
orobject.rotate(object, deltaAngle)
: This effectively addsdeltaAngle
(in degrees) to the current rotation property.object:scale(sx, sy)
orobject.scale(object, sx, sy)
: This effectively multiplies thexScale
andyScale
properties usingsx
andsy
, respectively. If the currentxScale
andyScale
values are 0.5 andsx
andsy
are also 0.5, the resulting scale will be 0.25 forxScale
andyScale
. This scales the object from 50 percent of its original size to 25 percent.object:translate(deltaX, deltaY)
orobject.translate(object, deltaX, deltaY)
: This effectively addsdeltaX
anddeltaY
to thex
andy
properties respectively. This will move the object from its current position.object:removeSelf()
orobject.removeSelf(object)
: This removes the display object and frees its memory, assuming that there are no other references to it. This is equivalent to callinggroup:remove(IndexOrChild)
on the same display object, but is syntactically simpler. TheremoveSelf()
syntax is also supported in other cases, such as removing physics joints in physics.