Corona SDK Mobile Game Development:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

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 of 0 is transparent and 1.0 is opaque. The default value is 1.0.
  • object.height is in local coordinates.
  • object.isVisible controls whether the object is visible on the screen. true is visible and false is not. The default is true.
  • object.isHitTestableallows 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. Defaults to false.
  • 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 is 0.
  • object.contentBounds is a table with properties xMin, xMax, yMin, and yMax 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.contentWidthis 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.yOriginspecifies 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.yReferencedefines 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 )or object.rotate( object, deltaAngle )—Effectively adds deltaAngle (in degrees) to the current rotation property.
  • object:scale( sx, sy ) or object.scale(object, sx, sy )—Effectively multiplies xScale and yScale properties by sx and sy respectively. If the current xScale and yScale values are 0.5 and sx and sy are also 0.5, the resulting scale will be 0.25 for xScale and yScale. This scales the object from 50 percent of its original size to 25 percent.
  • object:setReferencePoint( referencePoint ) or object.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 argument referencePoint 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 ) or object.translate( object, deltaX, deltaY )—Effectively adds deltaX and deltaY to the x and y properties respectively. This will move the object from its current position.
  • object:removeSelf( ) or object.removeSelf( object )—Removes the display object and frees its memory, assuming there are no other references to it. This is equivalent to calling group:remove(IndexOrChild) on the same display object, but is syntactically simpler. The removeSelf() syntax is also supported in other cases such as removing physics' joints in Physics.