How to change to landscape for a different scene?

Hello,

I am using the storyboard for different scenes in my app. All of them are portrait except one which I want to switch to landscape automatically. I know the orientation is set in the settings but how can I change this for the one scene? I searched and could not find much for doing this by code.

Thanks,

Warren
[import]uid: 184193 topic_id: 35854 reply_id: 335854[/import]

I’m not aware of any way to do this this “cleanly”. You could lock your app in portrait mode and for that one scene have all your graphics rotated but your coordinates are still going to be in portrait mode. Your X’s become Y’s and depending on which way you rotate your graphics, 0,0 could be the top right corner or the bottom left. [import]uid: 199310 topic_id: 35854 reply_id: 142572[/import]

I searched through the site and could not see where to do this. For this one scene it is just a small game like angry birds where it uses the physics and lets you throw an object at others stacked to knock down. It’s only one level so hopefully I can make this work with rotated graphics. Below is the basic code that I got from a demo on here and added a couple changes so far. Can you advise me what I may need to change in here? I know I will need to change the direction the game variable moves when the ball is thrown. But how do I change the direction of the gravity for the ball and the blocks that will fall down?

Thanks!

Warren

-----------------------------------------------------------------------------------------  
--  
-- main.lua  
--  
-----------------------------------------------------------------------------------------  
  
-- Your code here  
local m1 = 0  
  
local physics = require("physics")  
physics.start()  
physics.setScale(50)  
  
local game = display.newGroup()  
  
local circle = display.newImage( "gfx/anthony.png")  
circle.x =20  
circle.y = 220  
physics.addBody(circle, "dynamic", {density = .7, friction = 0.3, bounce = 0.2, radius = 40})  
game:insert(circle)  
local floor = display.newRect( 0, 300, 12048, 4 )  
physics.addBody(floor, "static", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})  
  
game:insert(floor)  
-- Add Blocks  
local x = 1200  
local y = 235  
local block1 = display.newImage("gfx/block1.png")  
block1.x = x  
block1.y = y  
physics.addBody(block1, "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})  
block1.myName = "b1"  
game:insert(block1)  
  
x = x + 50  
local block2 = display.newImage("gfx/block1.png")  
block2.x = x  
block2.y = 235  
physics.addBody(block2, "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})  
block2.myName = "b2"  
game:insert(block2)  
  
function circleTouched(event)  
  
 if event.phase == "began" then  
 display.getCurrentStage():setFocus(circle)  
 elseif event.phase == "ended" then  
 local p1 = event.xStart - event.x  
 local p2 = event.yStart - event.y  
 if p1 \> 20 then  
 p1 = 20  
 end  
 if p1 \< -17 then  
 p1 = -17  
 end  
 if p2 \> 10 then  
 p2 = 10  
 end  
 if p2 \< -30 then  
 p2 = -30  
 end  
print(p2)  
 circle:applyLinearImpulse(p1, p2, circle.x, circle.y)  
 display.getCurrentStage():setFocus(nil)  
  
 end  
end  
  
circle:addEventListener("touch", circleTouched)  
  
local function onCollision( event )  
 if ( event.phase == "began" ) then  
  
 --print( event.object2.myName )  
  
 end  
end  
  
Runtime:addEventListener( "collision", onCollision )  
  
function loop(e)  
 local targetx = 300 - circle.x  
 --game.x = game.x + ((targetx - game.x) \*0.05)  
 if circle.x \> 200 then  
 local d = (game.x - (circle.x - 200) )  
 d = d - game.x  
 game.x = d  
 end  
  
end  
  
Runtime:addEventListener("enterFrame", loop)  

[import]uid: 184193 topic_id: 35854 reply_id: 142578[/import]

You probably want to set the gravity on the X axis and not the Y and if its +9.8 or -9.8 depends on which way you’re going to rotate things.

You didn’t find any thing on this because it’s really not meant to be done. The OS makers don’t give us a way to force orientation on a scene by scene basis because it would frustrate the users. [import]uid: 199310 topic_id: 35854 reply_id: 142585[/import]

Thanks. In the code above that I posted, how is the gravity set to either X or Y? I do not see that. Is it defaulted to Y right now?

Never mind, I found physics.setGravity in the docs. I will give this a try tomorrow morning.

Warren
[import]uid: 184193 topic_id: 35854 reply_id: 142590[/import]

I’m not aware of any way to do this this “cleanly”. You could lock your app in portrait mode and for that one scene have all your graphics rotated but your coordinates are still going to be in portrait mode. Your X’s become Y’s and depending on which way you rotate your graphics, 0,0 could be the top right corner or the bottom left. [import]uid: 199310 topic_id: 35854 reply_id: 142572[/import]

I searched through the site and could not see where to do this. For this one scene it is just a small game like angry birds where it uses the physics and lets you throw an object at others stacked to knock down. It’s only one level so hopefully I can make this work with rotated graphics. Below is the basic code that I got from a demo on here and added a couple changes so far. Can you advise me what I may need to change in here? I know I will need to change the direction the game variable moves when the ball is thrown. But how do I change the direction of the gravity for the ball and the blocks that will fall down?

Thanks!

Warren

-----------------------------------------------------------------------------------------  
--  
-- main.lua  
--  
-----------------------------------------------------------------------------------------  
  
-- Your code here  
local m1 = 0  
  
local physics = require("physics")  
physics.start()  
physics.setScale(50)  
  
local game = display.newGroup()  
  
local circle = display.newImage( "gfx/anthony.png")  
circle.x =20  
circle.y = 220  
physics.addBody(circle, "dynamic", {density = .7, friction = 0.3, bounce = 0.2, radius = 40})  
game:insert(circle)  
local floor = display.newRect( 0, 300, 12048, 4 )  
physics.addBody(floor, "static", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})  
  
game:insert(floor)  
-- Add Blocks  
local x = 1200  
local y = 235  
local block1 = display.newImage("gfx/block1.png")  
block1.x = x  
block1.y = y  
physics.addBody(block1, "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})  
block1.myName = "b1"  
game:insert(block1)  
  
x = x + 50  
local block2 = display.newImage("gfx/block1.png")  
block2.x = x  
block2.y = 235  
physics.addBody(block2, "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})  
block2.myName = "b2"  
game:insert(block2)  
  
function circleTouched(event)  
  
 if event.phase == "began" then  
 display.getCurrentStage():setFocus(circle)  
 elseif event.phase == "ended" then  
 local p1 = event.xStart - event.x  
 local p2 = event.yStart - event.y  
 if p1 \> 20 then  
 p1 = 20  
 end  
 if p1 \< -17 then  
 p1 = -17  
 end  
 if p2 \> 10 then  
 p2 = 10  
 end  
 if p2 \< -30 then  
 p2 = -30  
 end  
print(p2)  
 circle:applyLinearImpulse(p1, p2, circle.x, circle.y)  
 display.getCurrentStage():setFocus(nil)  
  
 end  
end  
  
circle:addEventListener("touch", circleTouched)  
  
local function onCollision( event )  
 if ( event.phase == "began" ) then  
  
 --print( event.object2.myName )  
  
 end  
end  
  
Runtime:addEventListener( "collision", onCollision )  
  
function loop(e)  
 local targetx = 300 - circle.x  
 --game.x = game.x + ((targetx - game.x) \*0.05)  
 if circle.x \> 200 then  
 local d = (game.x - (circle.x - 200) )  
 d = d - game.x  
 game.x = d  
 end  
  
end  
  
Runtime:addEventListener("enterFrame", loop)  

[import]uid: 184193 topic_id: 35854 reply_id: 142578[/import]

You probably want to set the gravity on the X axis and not the Y and if its +9.8 or -9.8 depends on which way you’re going to rotate things.

You didn’t find any thing on this because it’s really not meant to be done. The OS makers don’t give us a way to force orientation on a scene by scene basis because it would frustrate the users. [import]uid: 199310 topic_id: 35854 reply_id: 142585[/import]

Thanks. In the code above that I posted, how is the gravity set to either X or Y? I do not see that. Is it defaulted to Y right now?

Never mind, I found physics.setGravity in the docs. I will give this a try tomorrow morning.

Warren
[import]uid: 184193 topic_id: 35854 reply_id: 142590[/import]