Hello,
Recently I’ve had the need of extending the “display” library of Corona.
The thing I was trying to do is saving the r/g/b/a values of a displayObject as properties on the displayObject itself. See code below:
local defaultR, defaultG, defaultB, defaultA = display.getDefault("fillColor") local defaultLineR, defaultLineG, defaultLineB, defaultLineA = display.getDefault("lineColor") local function extendDisplayObject(\_funcName) local cachedFunc = display[\_funcName] display[\_funcName] = function(...) local obj = cachedFunc(unpack(arg)) obj.r = \_funcName == "newLine" and defaultLineR or defaultR obj.g = \_funcName == "newLine" and defaultLineG or defaultG obj.b = \_funcName == "newLine" and defaultLineB or defaultB obj.a = \_funcName == "newLine" and defaultLineA or defaultA local cachedSetFillColor = obj.setFillColor obj.setFillColor = function(...) obj.r = arg[2] obj.g = arg[3] obj.b = arg[4] obj.a = arg[5] or obj.a cachedSetFillColor(unpack(arg)) end return obj end end extendDisplayObject("newCircle") extendDisplayObject("newContainer") extendDisplayObject("newEmbossedText") extendDisplayObject("newEmitter") extendDisplayObject("newImage") extendDisplayObject("newImageRect") extendDisplayObject("newLine") extendDisplayObject("newPolygon") extendDisplayObject("newRect") extendDisplayObject("newRoundedRect") extendDisplayObject("newSnapshot") extendDisplayObject("newSprite") extendDisplayObject("newText")
It works in the simulator, but on devices (iOS & Android) it gives the following error:
“…display.lua:16: attempt to index local ‘obj’ (a nil value)”
Could someone point me in the right direction to look for the solution?