hamobi
hamobi

Reputation: 8130

Corona SDK, OOP with display objects

I kinda understand the basics of OOP with lua using metatables. but things get a little hairy when I am really intending to subclass a display object. I don't believe I can pass the display object itself into the setmetatable function. I'd really like to add extra functions to the display object directly.

for example.. here is my player.lua file

local player = {}
local player_mt = { __index = player }  -- metatable

function player.new( world )    -- constructor

    local obj = display.newRect( world, 0, 0, 20, 20 )
    obj:setFillColor( 1,0,0 )

    local tbl = { obj = obj }
    return setmetatable( tbl, player_mt )
end

function player:fillColor( r,g,b )
    self.obj:setFillColor( r,g,b )
end

function player:setPos( x,y )
    self.obj.x, self.obj.y = x,y
end

return player

this works out okay.. but I'd really like self to refer to my obj variable. When I'm using this "class" I always have to reference the display object by saying player.obj. I'd rather it was just player

any way of accomplishing this or better approaches?

just tried using my shape object directly and calling fillColor on my updated player object. says it cant call fillColor on nil

local player = {}
local player_mt = { __index = player }  -- metatable

function player.new( world )    -- constructor

    local obj = display.newRect( world, 0, 0, 20, 20 )
    obj:setFillColor( 1,0,0 )

    return setmetatable( obj, player_mt )
end

function player:fillColor( r,g,b )
    self:setFillColor( r,g,b )
end

return player

Upvotes: 0

Views: 318

Answers (2)

Kumar KS
Kumar KS

Reputation: 521

Try creating like this,

local player= {}

function player.new(params)

  local self = {}
  setmetatable(self, {__index = player})

   local obj = display.newRect( world, 0, 0, 20, 20 )
   obj:setFillColor( 1,0,0 )

  self._obj  = obj  
  return self 
end


function player:fillColor( r,g,b )
  self._obj:setFillColor( r,g,b )
end

return player

Upvotes: 1

hamobi
hamobi

Reputation: 8130

this is the best way ive found so far of extending the display object directly

local new
local sayName

sayName = function( self )
    print( self.name )
end

new = function( world )
    local player = display.newRect( world, 0, 0, 20, 20 )
    player:setFillColor( 1,0,0 )
    player.name = 'bob'

    player.sayName = sayName
    return player
end

local public = {}
public.new = new
return public

Upvotes: 0

Related Questions