How to call deeply nested function in external modules.

Started by
Quitalizner
Dec 03 2018 08:09 AM

I'm not familiar with that plugin or where you got the code, but by a quick read of this, you pass "rect" to camera:add(). That comes into the add method as "obj". The code inside of add, adds new methods to "rect". Therefore if you want to move the rect to the front of the camera, you would call:

Quitalizner

[GLOBAL: userInfoPane.html]

Quitalizner

Enthusiast

30 posts

Corona SDK

03 December 2018 - 08:09 AM

Hi, I was going through the perspective plugin in the marketplace. I found these lines of code

function view:add(obj, lyr, isFocus)
local lyr = lyr or 4
layers[lyr]:insert(obj)
obj._perspectiveLayer = lyr
if isFocus then view:setFocus(obj) end
-- Move an object to a layer
function obj:toLayer(newLayer) if layer[newLayer] then layer[newLayer]:insert(obj) obj._perspectiveLayer = newLayer end end
--Move an object back a layer
function obj:back() if layer[obj._perspectiveLayer + 1] then layer[obj._perspectiveLayer + 1]:insert(obj) obj._perspectiveLayer = obj.layer + 1 end end
--Moves an object forwards a layer
function obj:forward() if layer[obj._perspectiveLayer - 1] then layer[obj._perspectiveLayer - 1]:insert(obj) obj._perspectiveLayer = obj.layer - 1 end end
--Moves an object to the very front of the camera
function obj:toCameraFront() layer[1]:insert(obj) obj._perspectiveLayer = 1 obj:toFront() end
--Moves an object to the very back of the camera
function obj:toCameraBack() layer[#layers]:insert(obj) obj._perspectiveLayer = #layers obj:toBack() end
end

Rob Miracle

[GLOBAL: userInfoPane.html]

Rob Miracle

Moderator

25,000 posts

Corona SDK

03 December 2018 - 01:02 PM

Best Answer

I'm not familiar with that plugin or where you got the code, but by a quick read of this, you pass "rect" to camera:add(). That comes into the add method as "obj". The code inside of add, adds new methods to "rect". Therefore if you want to move the rect to the front of the camera, you would call:

Quitalizner

Thanks Rob, although you are right with the call to the function, the add function in the plugin has typos which results in errors. The fixed code is as follows:-

function view:add(obj, lyr, isFocus)
local lyr = lyr or 4 -- if layer is not specified choose the 4th layer
layers[lyr]:insert(obj)
obj._perspectiveLayer = lyr -- this is the layer number not one
if isFocus then view:setFocus(obj) end
-- Move an object to a layer
function obj:toLayer(newLayer) if layers[newLayer] then layers[newLayer]:insert(obj) obj._perspectiveLayer = newLayer end end
--Move an obj back a layer
function obj:back() if layers[obj._perspectiveLayer + 1] then layers[obj._perspectiveLayer + 1]:insert(obj) obj._perspectiveLayer = obj._perspectiveLayer + 1 end end
--Moves an obj forwards a layer
function obj:forward() if layers[obj._perspectiveLayer - 1] then layers[obj._perspectiveLayer - 1]:insert(obj) obj._perspectiveLayer = obj._perspectiveLayer - 1 end end
--Moves an obj to the very front of the camera
function obj:toCameraFront() layers[1]:insert(obj) obj._perspectiveLayer = 1 obj:toFront() end -- the very front of the first display group
--Moves an ojbect to the very back of the camera
function obj:toCameraBack() layers[#layers]:insert(obj) obj._perspectiveLayer = #layers obj:toBack() end -- the very back of the last display group
end

If someone would inform the author Caleb to update the code, it would be helpful.