Hi,
I really need this function, to lock the device orientation after the main screen?
Can you please put this into a next update?
Thanks! [import]uid: 89239 topic_id: 26951 reply_id: 326951[/import]
Hi,
I really need this function, to lock the device orientation after the main screen?
Can you please put this into a next update?
Thanks! [import]uid: 89239 topic_id: 26951 reply_id: 326951[/import]
You should be able to ‘lock’ the orientation by only listen one supported orientation in the build.settings file. For example, this code should only allow the ‘landscapeRight’ orientation:
settings = {
orientation =
{
default = "landscapeRight",
supported = { "landscapeRight" },
}
}
[edit: fixed code I had pasted from my project] [import]uid: 38000 topic_id: 26951 reply_id: 109438[/import]
This doesn’t help, I would like to be able to support both orientation in the build file and at some point in the game to lock it on the chosen orientation.
This would be fantastic… [import]uid: 89239 topic_id: 26951 reply_id: 109454[/import]
Using an orientation event listener, you could manually flip your display group when the user turns the device, then, you would just remove the event listener to ‘lock’ the orientation.
(I’ll try to post code later tonight if I have time) [import]uid: 38000 topic_id: 26951 reply_id: 109549[/import]
Here’s some example code showing how you could rotate the orientation, and then lock it:
display.setStatusBar( display.HiddenStatusBar )
local button --forward reference so we can change button label
local currentOrientation = "landscapeRight" --Default in build.settings
local lock = true
local group = display.newGroup()
local widget = require'widget'
local function lockOrientation(event)
if lock then
lock = false --turn off the orientation change
display.remove(button) -- because of weird bug with button:setLabel
button = nil
button = widget.newButton{
width = 300,
height = 90,
label = "Unlock Rotation",
fontSize = 30,
onRelease = lockOrientation,
}
button.x = 480
button.y = 320
group:insert(button)
else
lock = true --turn orientation change back on
display.remove(button) -- because of weird bug with button:setLabel
button = nil
button = widget.newButton{
width = 300,
height = 90,
label = "Lock Rotation",
fontSize = 30,
onRelease = lockOrientation,
}
button.x = 480
button.y = 320
group:insert(button)
end
end
button = widget.newButton{
width = 300,
height = 90,
label = "Lock Rotation",
fontSize = 30,
onRelease = lockOrientation,
}
button.x = display.contentWidth/2
button.y = display.contentHeight/2
group:insert(button)
group:setReferencePoint(display.CenterReferencePoint)
local function changeOrientation(event)
if lock then
if event.type == "landscapeLeft" then
if currentOrientation == "landscapeRight" then
group:rotate(180)
elseif currentOrientation == "portrait" then
group:rotate(-90)
elseif currentOrientation == "portraitUpsideDown" then
group:rotate(90)
end
currentOrientation = "landscapeLeft" -- set orientation to current so that next change we know how much the device was rotated
elseif event.type == "landscapeRight" then
if currentOrientation == "landscapeRight" then
group:rotate(180)
elseif currentOrientation == "portrait" then
group:rotate(90)
elseif currentOrientation == "portraitUpsideDown" then
group:rotate(-90)
end
currentOrientation = "landscapeRight" -- set orientation to current so that next change we know how much the device was rotated
elseif event.type == "portrait" then
if currentOrientation == "landscapeRight" then
group:rotate(-90)
elseif currentOrientation == "landscapeLeft" then
group:rotate(90)
elseif currentOrientation == "portraitUpsideDown" then
group:rotate(180)
end
currentOrientation ="portrait" -- set orientation to current so that next change we know how much the device was rotated
elseif event.type == "portraitUpsideDown" then
if currentOrientation == "landscapeRight" then
group:rotate(90)
elseif currentOrientation == "landscapeLeft" then
group:rotate(-90)
elseif currentOrientation == "portrait" then
group:rotate(180)
end
currentOrientation = "portraitUpsideDown" -- set orientation to current so that next change we know how much the device was rotated
end
end
end
Runtime:addEventListener("orientation", changeOrientation)
Hope this helps!
[import]uid: 38000 topic_id: 26951 reply_id: 109604[/import]
Assuming the above is the main.lua file, here is the build.settings:
settings = {
orientation =
{
default = "landscapeRight",
supported = { "landscapeRight" },
}
}
[import]uid: 38000 topic_id: 26951 reply_id: 109605[/import]
Thanks for your help but using this method I encountered different problems like: the UI Alert messages are upside down and the fonts are acting really strange so this workaround is not helping.
I was hoping for an official function to do this from Ansca…
Thank you! [import]uid: 89239 topic_id: 26951 reply_id: 109852[/import]