Skip to main content

[Corona SDK] 多螢幕支援定位函式


為方便 Universal version 的製作,在畫面配置上,我都是採用對應螢幕四方界線方式做定位如 top, left, right, bottom, centerX, centerY,這樣在多螢幕顯示時,物件的位置才能一致,以下是定位函式分享,提供給有需要的朋友使用。


--[[
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})
view raw main.lua hosted with ❤ by GitHub

Comments

Post a Comment

Popular posts from this blog

[Flex] PureMVC standard with Spring extensions

由於上次稍微玩了一下 Robotlegs 依賴注入(DI) 主導的 MVC 框架,而著名也使用依賴注入的 Java / Java EE 的 Spring framework 出了 for ActionScript 的版本,剛好在最近 Spring ActionScript 1.0 正式 release 了(想了解 Spring 是啥咪東東的話請自行找 google 大神),這個版本除了基本框架外,也包含了 Cairngorm 與 PureMVC 的外掛...想當然耳,就拿來測試一下用在 PureMVC 內的感覺囉!! 參考了 官方範例 中 PureMVC 唯二的範例原始檔,以下使用的是「設定檔依賴注入 facade 透過 addConfigSource() 的方式來 init 」:(其實除了 embed 外,都是外部載入) Online Demo with source code 工作環境:FlashBuilder, Flex SDK4 請下載 PureMVC Standard 版本 再下載 Spring ActionScript 最新版本後,除了 spring-actionscript-cairngorm 不需要外,都放到 /src 下(記得只需要 org 開始...),也別忘了lib 內的 swc 檔 copy 到 /libs 下 Spring 的 injection 並不像 Robotlegs 直接來個 [Inject] metadata 的自動化那樣方便,但是其冷血度(檔案的鬆偶程度)更勝後者!如果你要使用設定檔(applicationContext.xml) 來做注入的話,準備工作就挺多的...XD 依照 applicationContext.xml 內設定的方式分別寫入 constructor 或者是 setter 依賴注入(本範例統一使用 setter injection) 為了跟大家都沒關係所以都使用 interface 來處理,所以你會在範例中發現大家都有介面...(並沒有真的研究過 Spring,也許還有其他作法) 準備 compiler 時候要用的 classe。由於在 setter, getter 的寫法上都使用 interface,所以真正用到的 class 需要預先在輸出階段就打包到程式內。 基本上 PureMVC 類 class...

[AIR] JoSi FXGtoLayout

JoSi FXGtoLayout v0.3.0, Adobe AIR 3 runtime 這個又是一個 "就是" 系列懶人小工具,主要是針對 Adobe fxg 格式做分析轉成 Mobile 開發用的視圖程式碼,加速畫面配置使用。 為什麼會製作這個工具,原因主要是本人在使用的 Corona SDK 與 Titanium SDK 都沒有好用的視覺化編輯工具。一般設計師產出 layout 檔會使用 PhotoShop 來製作,在不多花錢的原則下,畫面對齊的基準就是其輸出的 fxg 資料做對應,如果要一筆一筆將資料鍵入,做久也是會膩的,所以花了點時間將這個工具做出來...