How to call deeply nested function in external modules.

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

I can call the add function as usual using:

local camera = require("perspective").createView local rect = display.newRect(100,100, 50,50) camera:add(rect,4)

But how do i go about call the obj:toLayer() in the function view:add()

 i tried few things like camera:add:rect:toLayer(5). But i’m not sure.

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:

rect:toCameraFront()

Rob

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.

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:

rect:toCameraFront()

Rob

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.