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. Functions that deal with a particular type of value are part of that value. Such a value is called an object and its functions are called methods. In Corona, we'll be focusing more on display objects since they are essential for game development.
Display objects
Anything drawn to 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 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
is the object's opacity. A value of0
is transparent and1.0
is opaque. The default value is1.0
.object.height
is in local coordinates.object.isVisible
controls whether the object is visible on the screen.true
is visible andfalse
is not. The default istrue
.object.isHitTestable
allows an object to continue to receive hit events even if it is not visible. Iftrue
, objects will receive hit events regardless of visibility; iffalse
, events are only sent to visible objects. Defaults tofalse
.object.parent
is a read-only property that returns the object's parent.object.rotation
is the current rotation angle (in degrees). Can be a negative or positive number. Default is0
.object.contentBounds
is a table with propertiesxMin
,xMax
,yMin
, andyMax
in screen coordinates. Generally used to map the object in a group to the screen coordinates.object.contentHeight
is the height in screen coordinates.object.contentWidth
is the width in screen coordinates.object.width
is in local coordinates.object.x
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 will move the object in the x-direction.object.xOrigin
specifies the x-position of the object's origin relative to the parent's origin. It is in the object's local coordinates. Changing the value of this property will move the object in the x-direction.object.xReference
defines the x-position of the reference point relative to the object's local origin. For most display objects, the value defaults to 0, meaning the x-position of the origin and the reference point are the same.object.xScale
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
specifies the y-position (in local coordinates) of the object relative to the parent—the parent's origin to be precise.object.yOrigin
specifies the y-position of the object's origin relative to the parent's origin. It is in the object's local coordinates. Changing the value of this property will move the object in the y-direction.object.yReference
defines the y-position of the reference point relative to the object's local origin. For most display objects, the value defaults to 0, meaning the y-position of the origin and the reference point are the same.object.yScale
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 reference 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, the dot operator (.
) and the colon operator (:
). Both are valid ways to create object methods.
The call to an object method using the dot operator is passed to the object if it's the first argument:
object = display.newRect(110, 100, 50, 50)
object:setFillColor(255, 255, 255)
object.translate( object, 10, 10 )
The colon operator method is merely a shortcut with less typing involved to create the function:
object = display.newRect(110, 100, 50, 50)
object:setFillColor(255, 255, 255)
object:translate( 10, 10 )
The display objects share the following methods:
object:rotate( deltaAngle )
orobject.rotate( object, deltaAngle )
—Effectively addsdeltaAngle
(in degrees) to the current rotation property.object:scale( sx, sy )
orobject.scale(object, sx, sy )
—Effectively multipliesxScale
andyScale
properties bysx
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:setReferencePoint( referencePoint )
orobject.setReferencePoint( object, referencePoint )
—Sets the reference point either to the center of the object (default) or to one of several convenient points along the bounding box of the object. The argumentreferencePoint
should be one of the following:display.CenterReferencePoint
display.TopLeftReferencePoint
display.TopCenterReferencePoint
display.TopRightReferencePoint
display.CenterRightReferencePoint
display.BottomRightReferencePoint
display.BottomCenterReferencePoint
display.BottomLeftReferencePoint
display.CenterLeftReferencePoint
object:translate( deltaX, deltaY )
orobject.translate( object, deltaX, deltaY )
—Effectively addsdeltaX
anddeltaY
to thex
andy
properties respectively. This will move the object from its current position.object:removeSelf( )
orobject.removeSelf( object )
—Removes the display object and frees its memory, assuming 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.