為方便 Universal version 的製作,在畫面配置上,我都是採用對應螢幕四方界線方式做定位如 top, left, right, bottom, centerX, centerY,這樣在多螢幕顯示時,物件的位置才能一致,以下是定位函式分享,提供給有需要的朋友使用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
moveTo function | |
erinylin.com | |
for Graphics 2.0 | |
]]-- | |
display.setDefault("background", 1, 1, 1) | |
function moveTo(object, params) | |
local T = display.screenOriginY -- Top | |
local L = display.screenOriginX -- Left | |
local R = display.viewableContentWidth - L -- Right | |
local B = display.viewableContentHeight - T-- Bottom | |
local cX, cY = display.contentCenterX, display.contentCenterY | |
object.x, object.y = 0, 0 | |
local bounds = object.contentBounds | |
local offsetX = (bounds.xMax + bounds.xMin) * 0.5 | |
local offsetY = (bounds.yMax + bounds.yMin) * 0.5 | |
local hW, hH = 0.5 * object.contentWidth , 0.5 * object.contentHeight | |
if params.left then | |
object.x = params.left+L + hW - offsetX | |
elseif params.right then | |
object.x = R-params.right - hW - offsetX | |
elseif params.centerX then | |
object.x = params.centerX + cX - offsetX | |
end | |
if params.bottom then | |
object.y = B-params.bottom - hH - offsetY | |
elseif params.top then | |
object.y = params.top + T + hH - offsetY | |
elseif params.centerY then | |
object.y = params.centerY + cY - offsetY | |
end | |
return object | |
end | |
------------------------------------- | |
-- usage | |
------------------------------------- | |
local rect1 = display.newRect( 0, 0, 50, 50 ) | |
rect1:setFillColor(1, 0, 0) | |
moveTo(rect1, {top=0, left=0}) | |
local rect2 = display.newRect( 0, 0, 50, 50 ) | |
rect2:setFillColor(1, 0.2, 0) | |
moveTo(rect2, {top=0, right=0}) | |
local rect3 = display.newRect( 0, 0, 50, 50 ) | |
rect3:setFillColor(1, 0.4, 0) | |
moveTo(rect3, {bottom=0, right=0}) | |
local rect4 = display.newRect( 0, 0, 50, 50 ) | |
rect4:setFillColor(1, 0.6, 0) | |
moveTo(rect4, {bottom=0, left=0}) | |
local group = display.newGroup() | |
local rect5 = display.newRect( 0, 0, 50, 50 ) | |
rect5:setFillColor(1, 1, 0) | |
group:insert(rect5) | |
local rect6 = display.newRect( 0, 50, 50, 50 ) | |
rect6:setFillColor(1, 0, 1) | |
group:insert(rect6) | |
-- ignore anchor settings | |
group.anchorX, group.anchorY= 0, 0 | |
-- grouping moveTo | |
moveTo(group, {centerX=0, centerY=0}) | |
local rect7 = display.newRect( 0, 0, 50, 50 ) | |
rect7:setFillColor(0, 1, 1) | |
rect7.anchorX, rect7.anchorY= 0, 0 | |
moveTo(rect7, {top=0, left=50}) | |
太感謝了~很實用!
ReplyDelete