How can I get the overall scale of an object (image)? that is if the image is within multiple groups, each with scaling, and I want to get the overall scale? (e.g. if 3 nested groups are scaled by 50% then resultant scale would be 0.125
Are you saying, if you have this…
local g1 = display.newGroup() local g2 = display.newGroup() local g3 = display.newGroup() local circ = display.newCircle( 10, 10, 10 ) g1:insert(g2) g2:insert(g3) g3:insert(circ) g1:scale(0.8,0.8) g2:scale(0.5,0.5) g3:scale(2,2) circ:scale(1.25,1.25)
…that you want to get the absolute scale of circ?
If so, do this:
local scaleX = display.contentScaleX local scaleY = display.contentScaleY local tmp = circ while(tmp) do scaleX = scaleX \* tmp.xScale scaleY = scaleY \* tmp.yScale tmp = tmp.parent end print(scaleX, scaleY)
Note: If you only want the scale relative to the ‘design space’ and not the actual screen size, set scaleX and scaleY to 1 to start.
thanks - so there’s no direct API I guess, but compact little function that we can use then…
No, there is no Corona function that I know of. This is a pretty rare thing to check.
Here is a compact form of the above. Save it as a module and require it to use it.
local utils = {} function utils.getScale( obj, exact ) local scaleX = (exact) and display.contentScaleX or 1 local scaleY = (exact) and display.contentScaleY or 1 while(obj) do scaleX = scaleX \* obj.xScale scaleY = scaleY \* obj.yScale obj = obj.parent end return scaleX, scaleY end return utils
local utils = require "utils" -- assuming saved in "utils.lua" local scaleX, scaleY = utils.getScale( myObj )
excellent - thanks again
this is effectively equivalent to the frame transformation that localToContent() performs, and asking “where is 1?”
for example, assuming uniform scaling and no rotation:
local zx,zy = circ:localToContent(0,0)
local sx,sy = circ:localToContent(1,1)
print("scale = ", (sx-zx))
(if you have rotation, then need pythagorus for distance)
[edited. brain-typing mismatch re localToContent]
That is a nice variation Dave.
Are you saying, if you have this…
local g1 = display.newGroup() local g2 = display.newGroup() local g3 = display.newGroup() local circ = display.newCircle( 10, 10, 10 ) g1:insert(g2) g2:insert(g3) g3:insert(circ) g1:scale(0.8,0.8) g2:scale(0.5,0.5) g3:scale(2,2) circ:scale(1.25,1.25)
…that you want to get the absolute scale of circ?
If so, do this:
local scaleX = display.contentScaleX local scaleY = display.contentScaleY local tmp = circ while(tmp) do scaleX = scaleX \* tmp.xScale scaleY = scaleY \* tmp.yScale tmp = tmp.parent end print(scaleX, scaleY)
Note: If you only want the scale relative to the ‘design space’ and not the actual screen size, set scaleX and scaleY to 1 to start.
thanks - so there’s no direct API I guess, but compact little function that we can use then…
No, there is no Corona function that I know of. This is a pretty rare thing to check.
Here is a compact form of the above. Save it as a module and require it to use it.
local utils = {} function utils.getScale( obj, exact ) local scaleX = (exact) and display.contentScaleX or 1 local scaleY = (exact) and display.contentScaleY or 1 while(obj) do scaleX = scaleX \* obj.xScale scaleY = scaleY \* obj.yScale obj = obj.parent end return scaleX, scaleY end return utils
local utils = require "utils" -- assuming saved in "utils.lua" local scaleX, scaleY = utils.getScale( myObj )
excellent - thanks again
this is effectively equivalent to the frame transformation that localToContent() performs, and asking “where is 1?”
for example, assuming uniform scaling and no rotation:
local zx,zy = circ:localToContent(0,0)
local sx,sy = circ:localToContent(1,1)
print("scale = ", (sx-zx))
(if you have rotation, then need pythagorus for distance)
[edited. brain-typing mismatch re localToContent]
That is a nice variation Dave.