Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Metatables — Gideros Forum

Metatables

hoanhoan Member
edited April 2012 in General questions
Hey everyone

I'm trying to write a corona compatibility layer for certain functions like: display.*, system.* etc

So I can add methods to a Sprite object so:

object:setFillColor is translated into object:setColorTransform

However, I've never used metatables before, and want these to be translated:

object.x => object:getX()
object.y => object:getY()

I call this on an object to add extra methods
function __addDisplayMethods(object)
	local mt = getmetatable(object)
	local oldIndex = mt.__index
 
	object.setFillColor = function(self,r,g,b,a)
		object:setColorTransform(r/255,g/255,b/255,a/255)
	end
 
	object.insert = function(self,thing)
		object:addChild(thing)
	end
 
	object.__index = function(self,key)
		print("I")
	end
 
	mt.__index = function(t, key)
		print("Ky = " .. key)
		if key == 'x' then
			return object:getX()
		else
			oldIndex(t, key)
		end
	end
end
However old gideros methods don't work any longer. object:setPosition() fails. Is there a way to chain these metatables or not?

Thanks
Hoan

Likes: Ozzan

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • evsevs Member
    edited April 2012
    As an aside - If you want to format your code without the nasty yellow
    make sure that inside your opening <> you have pre lang="lua" exactly like that.
    Then in the closing <> you have /pre - no spaces around any of it!
     
    if not yellow then print("Hallelujah!") end
    cheers

    evs
  • hoanhoan Member
    Hey I figured it out. Anyone else working on corona compatibility layer? Lets pool efforts
  • gorkemgorkem Maintainer
    @ozapps has been working on it
Sign In or Register to comment.