在使用Corona SDK的图形2.0时,将基本方向更改代码用于重新布局UI组件。

有人能发现这里的问题吗?当我运行此代码并在纵向和横向之间切换时,它不像预期效果那样工作(矩形的宽度没有随着有效宽度的大小改变而改变)

尝试得到处理自动调整UI元素以支持纵向和横向的基本(简单)概念/方法...

`` ` display.setStatusBar(display.HiddenStatusBar) local WIDTH_PERCENT = 0.8 local myGroup = display.newGroup()

local myRoundedRect = display.newRoundedRect(myGroup,0,0,display.contentWidth * WIDTH_PERCENT,50,25) myRoundedRect.strokeWidth = 3 myRoundedRect:setFillColor(0,1,0) myRoundedRect:setStrokeColor(1,0,0) myRoundedRect.y = 25

local myRoundedRect2 = display.newRoundedRect(myGroup,0,0,display.contentWidth * WIDTH_PERCENT,50,25) myRoundedRect2.strokeWidth = 3 myRoundedRect2:setFillColor(0,0,1) myRoundedRect2:setStrokeColor(1,0,0) myRoundedRect2.y = 75

local function positionItems() myRoundedRect.width = display.contentWidth * WIDTH_PERCENT myRoundedRect2.width = display.contentWidth * WIDTH_PERCENT

myRoundedRect.x = display.contentWidth / 2
myRoundedRect2.x = display.contentWidth / 2

end positionItems()

local function onOrientationChange(event) positionItems() end Runtime:addEventListener(“orientation”,onOrientationChange)

`` `

更新:

似乎存在一些问题,即当已放置一个矩形后,以编程方式设置它的宽度。我认为这是Corona支持的基本操作。因此,我在下面的代码中看到的是:

  • 如果最初设置为1000的正方形的宽度,每次切换方向它都会缩小,直到你看不到它
  • 如果最初设置为10,确实会出现非常奇怪的模式
  • 在重置矩形的大小后,再读取它,它似乎并没有改变值,但仍然无法解释上面两个问题

代码:

`` ` display.setStatusBar(display.HiddenStatusBar)

local WIDTH_PERCENT = 0.5 local INITIAL_BOX_WIDTH = 1000 - 立即通过“positionItems”函数设置的宽度/高度

local myGroup = display.newGroup()

local myRoundedRect = display.newRoundedRect(myGroup,0,0,INITIAL_BOX_WIDTH,INITIAL_BOX_WIDTH,25) myRoundedRect.strokeWidth = 1 myRoundedRect:setFillColor(0,1,0) myRoundedRect:setStrokeColor(1,0,0)

local function positionItems() local displayW,displayH = display.contentWidth,display.contentHeight

myRoundedRect.width = displayW * WIDTH_PERCENT
myRoundedRect.height = displayH * WIDTH_PERCENT

myRoundedRect.x =displayW/2
myRoundedRect.y =displayH/2

print(“将宽度设置为”.. displayW * WIDTH_PERCENT ..“。从Rec读取回来的值为”..myRoundedRect.width)

end positionItems()

local function onOrientationChange(event) positionItems() end Runtime:addEventListener(“orientation”,onOrientationChange)

`` `

配置(下面 - 或什么都没有会导致相同的问题)

`` ` local aspectRatio =display.pixelHeight/display.pixelWidth 申请= { content= { 宽度= aspectRatio > 1.5 and 320 or math.ceil(480 /aspectRatio), 高度= aspectRatio <1.5 and 480 or math.ceil(320 * aspectRatio), 规模= “letterbox”, } }

`` `

建立

`` ` 设置= { 方向= { 默认= “肖像”, 支持= {“肖像”,“横向左”,“横向右”,“倒置肖像” }, },

} `` `

点赞
用户825481
用户825481

onOrientationChange 函数中,传递的事件如下:

"portrait"
"landscapeLeft"
"portraitUpsideDown"
"landscapeRight"
"faceUp"
"faceDown"

你需要这样说:

local function onOrientationChange(event)
  if event.phase=="landscapeLeft" then
  --在此处编写代码
  end
end

祝你好运,希望这有所帮助。

2014-01-07 18:25:44
用户173520
用户173520

发现问题出在:

a) 在 newRoundedRect 中我使用了一个奇怪的模式,仅涉及到大半径值 b) 看起来 newRoundedRect 中核心缩小的矩形可能是一个bug,当切换为 newRect 后事情开始如预期般工作

2014-01-09 02:14:55