Object Oriented Programming and Images

I looked over: 

https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

In order to learn about Lua and Object Oriented Programming.

However I came across a peculiar problem. So I created the class brick and wanted to add a function within the brick so that if it’s hit it could delete itself. This whole setup is causing all sorts of problems and I wasnt sure if I was even going about it right. 

Question: How do I  add collision filters within an object. 

function brick.new( x, y, name, ageInYears ) -- constructor local newbrick = { name = name or "Unnamed", age = ageInYears or 2, body = display.newImageRect( "brick.png", 100, 50 ), } newbrick.body.x = x newbrick.body.y = y physics.addBody( newbrick.body, "static", { friction=0.5, bounce=0.3 } ) local function onLocalPostCollision( self, event ) self:rollOver() end newbrick.body.postCollision = onLocalPostCollision newbrick.body:addEventListener( "postCollision" ) return setmetatable( newbrick, brick\_mt ) end function brick:hit() print(self.name) display.remove(self.body) end

** WARNING 1 - Tweaked a couple times after first post to fix syntax errors and  add comments **

** WARNING 2 - Only ever run in my ‘head’.  May contain typos ** 


  1. What is your reason for wanting to use OOP in game development?
     

  2. There are different approaches you can take for modular design.  Here are two:
     
    A. The one you’re trying which is creating Lua objects with an ‘OOP’ layer on top of them and then ‘somehow’ managing actual display objects with them.
     
    B. Using a Lua module to produce display objects with fields and functions attached, which are managed using standard Corona SDK techniques.
     
    I am NOT a proponent of A.  One you’ve got to mess with metatables, I feel you’ve gone too far.  
     
    Now, if you’re doing all this to learn more about Lua, then that is cool, but I honestly don’t think this will help you design games.  
     
    Let me give you a simple ‘module’ example to clarify what I’m suggesting instead (B above)
     
    The Module

    local brick = {} – Forward declarations local onlPostCollision local onRollover local onHit – Builder function to make ‘bricks’ function brick.new( group, x, y, params ) group = group or display.currentStage params = params or {} local theBrick = display.newImageRect( group, “brick.png”, 100, 50 ) theBrick.x = x or 0 theBrick.y = y or 0 theBrick.data = {} theBrick.data.isA = “brick” theBrick.data.name = params.name or “Unnamed” theBrick.data.ageInYears = params.ageInYears or 2 physics.addBody( theBrick, params.bodyType or “static”, { friction = params.friction or 0.5, bounce = params.bounce or 0.3 } ) theBrick.postCollision = onPostCollision theBrick:addEventListener( “postCollision” ) – Attach additional functions theBrick.rollOver = onRollover theBrick.hit = onHit return theBrick end – Single ‘postCollision’ function used by All bricks (only defined ONCE) onPostCollision = function( self, event ) self:rollOver() end – Single ‘rollover’ function used by all bricks (only defined ONCE) onRollover = function( self ) – Whatever this does… end – Single ‘hit’ function used by all bricks (only defined ONCE) onHit = function( self ) print(self.data.name) display.remove( self ) end return brick 

 
 
The Use

local brickBuilder = require "brick" -- assuming module is in ~/brick.lua local group = display.newGroup() -- Table to track bricks (if you need it) local myBricks = {} -- Make some bricks using our modular builder. No metatables required. -- local brick = brickBuilder.new( group, 50, 100, { name = "Brick 1", ageInYears = 10 } ) myBricks[brick] = brick local brick = brickBuilder.new( group, 150, 100, { name = "Brick 2", ageInYears = 3 } ) myBricks[brick] = brick local brick = brickBuilder.new( group, 250, 100, { name = "Brick 3", ageInYears = 20, bounce = 1 } ) myBricks[brick] = brick

Final note.

There are game types that I think would benefit from a more elaborate OOP based approach, where Lua ‘objects’ manage display objects.  Specifically, RPGs, multiplayer games, and games with LOTS of data and a few display objects.

However, if you’re making one Lua object for every display object I feel you have missed the point and are simply complicating things to the point where bugs are bound to occur.

Thank you! This works for the most part, but I’m still having problems calling the functions attached to brick, which was most of my purpose in using OOP. It seems to work for the collision but not the other two. 

These? 

local brick = brickBuilder.new( group, 150, 100, { name = "Brick 2", ageInYears = 3 } ) brick:rollOver() brick:hit()

What is the trouble?  An error?  Check your syntax and scope.  Also, once a brick is removed calling these won’t work properly.

I’m sorry this is so weird it does work. I don’t know if there was some sort of dumb syntax error but earlier it had said that the functions were nil. It works now though so thank you so much!

** WARNING 1 - Tweaked a couple times after first post to fix syntax errors and  add comments **

** WARNING 2 - Only ever run in my ‘head’.  May contain typos ** 


  1. What is your reason for wanting to use OOP in game development?
     

  2. There are different approaches you can take for modular design.  Here are two:
     
    A. The one you’re trying which is creating Lua objects with an ‘OOP’ layer on top of them and then ‘somehow’ managing actual display objects with them.
     
    B. Using a Lua module to produce display objects with fields and functions attached, which are managed using standard Corona SDK techniques.
     
    I am NOT a proponent of A.  One you’ve got to mess with metatables, I feel you’ve gone too far.  
     
    Now, if you’re doing all this to learn more about Lua, then that is cool, but I honestly don’t think this will help you design games.  
     
    Let me give you a simple ‘module’ example to clarify what I’m suggesting instead (B above)
     
    The Module

    local brick = {} – Forward declarations local onlPostCollision local onRollover local onHit – Builder function to make ‘bricks’ function brick.new( group, x, y, params ) group = group or display.currentStage params = params or {} local theBrick = display.newImageRect( group, “brick.png”, 100, 50 ) theBrick.x = x or 0 theBrick.y = y or 0 theBrick.data = {} theBrick.data.isA = “brick” theBrick.data.name = params.name or “Unnamed” theBrick.data.ageInYears = params.ageInYears or 2 physics.addBody( theBrick, params.bodyType or “static”, { friction = params.friction or 0.5, bounce = params.bounce or 0.3 } ) theBrick.postCollision = onPostCollision theBrick:addEventListener( “postCollision” ) – Attach additional functions theBrick.rollOver = onRollover theBrick.hit = onHit return theBrick end – Single ‘postCollision’ function used by All bricks (only defined ONCE) onPostCollision = function( self, event ) self:rollOver() end – Single ‘rollover’ function used by all bricks (only defined ONCE) onRollover = function( self ) – Whatever this does… end – Single ‘hit’ function used by all bricks (only defined ONCE) onHit = function( self ) print(self.data.name) display.remove( self ) end return brick 

 
 
The Use

local brickBuilder = require "brick" -- assuming module is in ~/brick.lua local group = display.newGroup() -- Table to track bricks (if you need it) local myBricks = {} -- Make some bricks using our modular builder. No metatables required. -- local brick = brickBuilder.new( group, 50, 100, { name = "Brick 1", ageInYears = 10 } ) myBricks[brick] = brick local brick = brickBuilder.new( group, 150, 100, { name = "Brick 2", ageInYears = 3 } ) myBricks[brick] = brick local brick = brickBuilder.new( group, 250, 100, { name = "Brick 3", ageInYears = 20, bounce = 1 } ) myBricks[brick] = brick

Final note.

There are game types that I think would benefit from a more elaborate OOP based approach, where Lua ‘objects’ manage display objects.  Specifically, RPGs, multiplayer games, and games with LOTS of data and a few display objects.

However, if you’re making one Lua object for every display object I feel you have missed the point and are simply complicating things to the point where bugs are bound to occur.

Thank you! This works for the most part, but I’m still having problems calling the functions attached to brick, which was most of my purpose in using OOP. It seems to work for the collision but not the other two. 

These? 

local brick = brickBuilder.new( group, 150, 100, { name = "Brick 2", ageInYears = 3 } ) brick:rollOver() brick:hit()

What is the trouble?  An error?  Check your syntax and scope.  Also, once a brick is removed calling these won’t work properly.

I’m sorry this is so weird it does work. I don’t know if there was some sort of dumb syntax error but earlier it had said that the functions were nil. It works now though so thank you so much!