Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to call deeply nested function in external modules.
Started by Quitalizner Dec 03 2018 08:09 AM

2 replies to this topic
virtual camera perspective external modules

Best Answer Rob Miracle , 03 December 2018 - 01:02 PM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,000 posts
  • Corona SDK

  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:

rect:toCameraFront()

Rob


  • Quitalizner likes this

[TOPIC: post.html]
#3

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: virtual camera perspective, external modules