Overview

In depth explanation of the dmc-objects module.

Table of Contents

{toc:style=disc|indent=20px|minLevel=2|exclude=Table of Contents|printable=false}

dmc-objects exports

Here are the items which are exported by dmc-objects:

{code:language=none|title=dmc_objects Exports}-- these are references to the button classes and a OOP help method

Objects.inheritsFrom Objects.Object Objects.CoronaBase Objects.CoronaPhysics``` They can be used to help with inheritance, retrieve class constants, etc. For example:

{code:language=none|title=dmc_objects Exports Example} local Objects = require( "dmc_objects" ) local inheritsFrom = Objects.inheritsFrom local CoronaBase = Objects.CoronaBase

local Shape = inheritsFrom( CoronaBase ) -- create a class called Shapes```

DMC Objects Functionality and Classes

There are some helper functions and three distinct classes which will help jumpstart OOP in Lua. The level will depend on what you're trying to do:

Level 1: Helper Functions ( bless(), inheritsFrom() )

  • You want to create your very own hierarchy, in pure Lua.
  • No Corona needed or wanted.
  • This is barebones.
  • The Object class in dmc_objects.lua is an example of this in use.

See the [Barebones Example|#BarebonesExample] below.

Level 2: Object Class

  • Object class which has some basic functionality ( destroy(), superCall(), print() ).
  • Still, no Corona, but you want more features.
  • The Corona Base class in dmc_objects.lua is an example of this in use.

See the [Object Class Example|#ObjectClassExample] below.

Level 3: Corona Base class

  • The first class to help with building Corona-based objects.
  • If working with Corona and no physics engine, then this is what you want.

See the [Corona Base Class Example|#CoronaBaseClassExample] below.

Level 4: Physics Base class

  • Same as Corona Base class, but has more convenience functions for a component placed in the physics engine.
  • If working with Corona and using the physics engine, then this is what you want.

See the [Corona Physics Class Example|#CoronaPhysicsClassExample] below.

{anchor:BarebonesExample}

Working with the Barebones methods

The following example is a brief snippet, enough to get you going.

For more information, look at the class support functions in dmc_objects.lua. Also the Object class is an example of this.

Example

{code:language=none|title=barebones.lua}

-- Imports and Setup

-- import DMC Objects file local Objects = require( "dmc_objects" )

-- setup some aliases to make code cleaner local inheritsFrom = Objects.inheritsFrom

--====================================================================--

-- MyBarebonesClass class

-- we inherit from 'nil' because we're at the top of the chain local MyBarebonesClass = inheritsFrom( nil )

MyBarebonesClass:new() -- do the rest end

-- more class methods, etc here ...

-- return the class object so we can get to it when doing a require()

return MyBarebonesClass



{code:language=none|title=main.lua}local BareBones = require( 'barebones' )
-- this is the MyBarebonesClass object

local newObj = BareBones:new()

{anchor:ObjectClassExample}

Working with the Object class

Here is a brief example of inheriting from the Object Class. The Corona Base Class is an example of a class derived from the Object Class and can be used for reference. For more information, look at the Object Class and Corona Base Class in dmc_objects.lua.

Example

{code:language=none|title=basic.lua}--====================================================================--

-- Imports and Setup

-- import DMC Objects file local Objects = require( "dmc_objects" )

-- setup some aliases to make code cleaner local inheritsFrom = Objects.inheritsFrom local Object = Objects.Object

--====================================================================--

-- MyBasicClass class

local MyBasicClass = inheritsFrom( Object )

MyBasicClass:new() local o = self:_bless() -- do the rest end

-- more class methods, etc here ...

-- return the class object so we can get to it when doing a require()

return MyBasicClass``` {code:language=none|title=main.lua}local BasicClass = require( 'basic' ) -- the MyBasicClass object

local newObj = BasicClass:new()



{anchor:CoronaBaseClassExample}

## Working with the Corona Base class

The following partial example is taken from the Shape example which comes with DMC Corona Library. Look in the folder examples/dmc_objects/DMC-MultiShapes/.

For more explanation about the steps required to inherit from the Corona Base Class, see the document [docs:Subclass the Corona Base class].
{color:#000000}{*}Example (partial)*{color} {code:language=none|title=shapes.lua|linenumbers=true}--====================================================================-- -- Imports and Setup --====================================================================-- -- import DMC Objects file local Objects = require( "dmc_objects" ) -- setup some aliases to make code cleaner local inheritsFrom = Objects.inheritsFrom local CoronaBase = Objects.CoronaBase --====================================================================-- -- Shapes class - lines --====================================================================-- local Shape = inheritsFrom( CoronaBase ) Shape.NAME = "Shape Base" Shape.TRANSPARENT ={ 0, 0, 0, 0 } Shape.STROKE_WIDTH = 5 Shape.P_LIST1 = nil Shape.P_LIST2 = nil -- Shape constructor function Shape:new( options ) local o = self:_bless() o:_init( options ) o:_createView() o:_initComplete() return o end function Shape:_init() self:superCall( "_init" ) --== Create Properties == local rand = math.random self.color ={ rand(255), rand(255), rand(255) } end --== Methods -- methods work for point-base shapes -- override for general functions function Shape:_createView() local p_list1 = self.P_LIST1 local p_list2 = self.P_LIST2 if p_list1 and p_list2 then local d = display.newLine( unpack( p_list1 ) ) d:append( unpack( p_list2 ) ) self:_setDisplay( d ) d:setColor( unpack( self.color ) ) d.width = self.STROKE_WIDTH end end --====================================================================-- -- Square class --====================================================================-- local Square = inheritsFrom( Shape ) Square.NAME = "Square" Square.P_LIST1 ={ 0, 0, 40, 40 } -- we could make this from points, but just showing an override function Square:_createView() local d = display.newRect( unpack( self.P_LIST1 ) ) self:_setDisplay( d ) d:setStrokeColor( unpack( self.color ) ) d:setFillColor( unpack( Shape.TRANSPARENT ) ) d.strokeWidth = self.STROKE_WIDTH end --====================================================================-- -- Circle class --====================================================================-- local Circle = inheritsFrom( Shape ) Circle.NAME = "Circle" Circle.P_LIST1 ={ 0, 0, 20 } -- using Corona native circle method function Circle:_createView() local d = display.newCircle( unpack( self.P_LIST1 ) ) self:_setDisplay( d ) d:setFillColor( unpack( Shape.TRANSPARENT ) ) d:setStrokeColor( unpack( self.color ) ) d.strokeWidth = self.STROKE_WIDTH end --====================================================================-- -- Diamond class --====================================================================-- local Diamond = inheritsFrom( Shape ) Diamond.NAME = "Diamond" Diamond.P_LIST1 ={ 0, -40, 20, 0 } Diamond.P_LIST2 ={ 0, 40, -20, 0, 0, -40 } --====================================================================-- -- Hexagon class --====================================================================-- local Hexagon = inheritsFrom( Shape ) Hexagon.NAME = "Hexagon" Hexagon.P_LIST1 ={ 0, 17.2, 10, 0 } Hexagon.P_LIST2 ={ 30, 0, 40, 17.2, 30, 34.4, 10, 34.4, 0, 17.2 } -- There are many ways to get at the classes in a file. -- This is simpler than the example on the filesystem local ShapeFactory = {} ShapeFactory.Shape = Shape ShapeFactory.Square = Square ShapeFactory.Circle = Circle ShapeFactory.Diamond = Diamond ShapeFactory.Hexagon = Hexagon -- return the factory object so we can get to it when doing a require() return ShapeFactory

{code:language=none|title=main.lua|linenumbers=true}local Shapes = require( 'shapes' ) -- this is the ShapeFactory object local Circles = Shapes.Circle

local newDiamond = Shapes.Diamond:new() local newCircle = Circles:new()



{anchor:CoronaPhysicsExample}

## Working with the Corona Physics Class

The following example is a brief snippet, enough to get you going. Some of the methods we've seen before have been omitted, eg, `new()`.

This is the same as the Corona Base class example, except the class inheritance is different - this gives additional methods on the objects.
{anchor:CoronaPhysicsClassExample}

### Example

{code:language=none|title=spaceship.lua|linenumbers=true}--====================================================================--
-- Imports and Setup
--====================================================================--

-- import DMC Objects file
local Objects = require( "dmc_objects" )

-- setup some aliases to make code cleaner
local inheritsFrom = Objects.inheritsFrom
local CoronaPhysics = Objects.CoronaPhysics

--====================================================================--
-- Physics class
--====================================================================--

local SpaceShip = inheritsFrom( CoronaPhysics )
SpaceShip.NAME = "Space Ship"


-- more class methods, etc here
...


-- return the class object so we can get to it when doing a require()

return SpaceShip

{code:language=none|title=main.lua}local SpaceShip = require( 'spaceship' ) -- this is the SpaceShip object

local newShip = SpaceShip:new()

```

how to:

  • object printing
  • getters and setters
  • events and listeners