here is how i am generating a mask … works fine for iphone and iphone4 but go to a tablet where the scale factor is not a simple multiple and it never saves the file right … always off by a pixel which causes the mask to be broken and not work.
Maybe you can see where i am going wrong. The following code works just fine on iphone and iphone4 … you get a nice 20 x 20 image masking the requested 12 x 12 region.
But you take it to the iPad simulator or other with a fractional scale factor and even though it prints out that the size of the image is 20 x 20 just prior to saving it to disk … the image on disk is 19 x 19 on disk and the mask is not centered anymore.
This has vexed me every time i pick this code back up saying today …!
Cheers,
m
function generateMaskFile( width, height ) local filename = "mask-"..width.."x"..height .. ".png" -- --Adjust height and width to a multiple of 4 -- as per requirement given by Corona -- if (width % 4 ~= 0) then width = width + (4 - (width % 4)) end if (height % 4 ~= 0) then height = height + (4 - (height % 4)) end local scaledWidth = display.contentScaleX \* width local scaledHeight = display.contentScaleY \* height print("Masking file missing for screen size: " .. width .. " / " .. height .. "\ndisplay.actualContentWidth = \n" .. display.actualContentWidth .. "\ndisplay.actualContentHeight = " .. display.actualContentHeight ) local realWidth = width + 8 local realHeight = height + 8 -- create the mask group and the surrounding black area local maskgroup = display.newGroup() local invisible = display.newRect(maskgroup, 0, 0, realWidth, realHeight) invisible:setFillColor(0, 0, 0) print("Rect Height and width: " .. invisible.width .. " / " .. invisible.height) local visible = display.newRect(maskgroup, 0, 0, width, height) invisible:setReferencePoint(display.CenterReferencePoint) visible:setReferencePoint(display.CenterReferencePoint) invisible.x,invisible.y = 0 , 0 visible.x,visible.y = 0, 0 maskgroup.x,maskgroup.y = display.contentCenterX, display.contentCenterY maskgroup.xScale = display.contentScaleX maskgroup.yScale = display.contentScaleY print("maskgroup width/height: " .. maskgroup.width .. " / " .. maskgroup.height) display.save( maskgroup, filename, system.TemporaryDirectory ) maskgroup:removeSelf() end generateMaskFile(12, 12)