Is this a bug or a feature?

So, I am still evaluating Corona SDK and things are a little weird at the moment.

Here is a simple project:

http://dl.dropbox.com/u/29886031/OrientationMadness.zip

With the commented out section of code, notice how changing the orientation of the simulator does indeed cause a new background to be loaded in.

Now, uncomment the section at the bottom of main.lua.

Notice how everything is now messed up.

Is it a bug or something I am not doing right?

it is a feature try this code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
display.setStatusBar(display.HiddenStatusBar)
 
local displayGroup = display.newGroup()
 
local background = display.newImageRect("portrait.png", 320, 480)
 
displayGroup:insert(background)
 
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
 
 
 
 
local circle1 = display.newCircle(50, 50, 30)
circle1.x = 50
circle1.y = 50
circle1:setFillColor(0, 255, 255)
 
displayGroup:insert(circle1)
 
local circle2 = display.newCircle(50, 50, 30)
circle2.x = display.contentWidth - 50
circle2.y = 50
circle2:setFillColor(255, 0, 255)
 
displayGroup:insert(circle2)
 
local circle3 = display.newCircle(50, displayGroup.height - 50, 30)
 
circle3:setFillColor(255, 255, 0)
 
displayGroup:insert(circle3)
 
local circle4 = display.newCircle(displayGroup.width - 50, displayGroup.height - 50, 30)
 
circle4:setFillColor(0, 0, 0)
 
displayGroup:insert(circle4)
 
 
Runtime:addEventListener("orientation", function(event)
 
                                          print("Orientation changed to: " .. event.type)
                                          print("event.delta = " .. event.delta)
 
                                          if event.type == "portrait" then
                                            background:removeSelf()
 
                                            background = display.newImageRect("portrait.png", 320, 480)
 
                                            displayGroup:insert(background)
 
                                            background:toBack()
                                          elseif event.type == "portraitUpsideDown" then
                                            background:removeSelf()
 
                                            background = display.newImageRect("portraitUpsideDown.png", 320, 480)
 
                                            displayGroup:insert(background)
 
                                            background:toBack()
                                          elseif event.type == "landscapeRight" then
                                            background:removeSelf()
 
                                            background = display.newImageRect("landscapeRight.png", 480, 320)
 
                                            displayGroup:insert(background)
 
                                            background:toBack()
                                          elseif event.type == "landscapeLeft" then
 
                                            background:removeSelf()
 
                                            background = display.newImageRect("landscapeLeft.png", 480, 320)
 
                                            displayGroup:insert(background)
 
                                            background:toBack()
                                          end
                                          
                                          
                                        circle1:removeSelf()
                                        circle2:removeSelf()
                                        circle3:removeSelf()
                                        circle4:removeSelf()
                                        
                                                                                circle1 = display.newCircle(0, 0, 30)
                                                                                circle1.x = 50
                                                                                circle1.y = 50
                                                                                circle1:setFillColor(0, 255, 255)
                                                                                
                                                                                displayGroup:insert(circle1)
                                                                                
                                                                                circle2 = display.newCircle(0, 0, 30)
                                                                                circle2.x = display.contentWidth - 50
                                                                                circle2.y = 50
                                                                                circle2:setFillColor(255, 0, 255)
                                                                                
                                                                                displayGroup:insert(circle2)
                                                                                
                                                                                circle3 = display.newCircle(0,  0, 30)
                                                                                circle3.x = 50
                                                                                circle3.y = display.contentHeight - 50
                                                                                circle3:setFillColor(255, 255, 0)
                                                                                
                                                                                displayGroup:insert(circle3)
                                                                                
                                                                                circle4 = display.newCircle(0, 0, 30)
                                                                                circle4.x = display.contentWidth - 50
                                                                                circle4.y = display.contentHeight - 50
                                                                                circle4:setFillColor(0, 0, 0)
                                                                                
                                                                                displayGroup:insert(circle4)
 
                                         background.x = display.contentWidth / 2
                                                                                 background.y = display.contentHeight / 2
                                        end)

@hgvyas123

Thank you!

I see that you also re-layout the circles after an orientation change. I knew I had to do that, but what I could not understand is why not doing it would interfere with the positioning of the various backgrounds. That's what seemed strange to me. (I expected the circles to end up in various incorrect positions, but I didn't expect the backgrounds to be affected at all.)

I made another package that shows an equally weird behavior:

http://dl.dropbox.com/u/29886031/OrientationMadness2.zip

In this code, I position the top-level group to the center of the screen and I use local coordinates to position the circles within the top-level group.
You will observe that the relative position of the circles remain the same throughout all orientation changes, as expected.

However, it seems the origin of the top-level group becomes the bottom left corner of the screen when the orientation is set to landscape (both landscape right and landscape left).

What do you think?

ok as per you said changed the same prev one example see this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
display.setStatusBar(display.HiddenStatusBar)
 
_W = display.contentWidth;
_H = display.contentHeight;
 
local displayGroup = display.newGroup()
 
 
 
print("displayGroup.xOrigin = " .. displayGroup.xOrigin)
print("displayGroup.yOrigin = " .. displayGroup.yOrigin)
print("displayGroup.xReference = " .. displayGroup.xReference)
print("displayGroup.yReference = " .. displayGroup.yReference)
 
local background = display.newImageRect("portrait.png", 320, 480)
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
 
displayGroup:insert(background)
 
Runtime:addEventListener("orientation", function(event)
 print("Orientation changed to: " .. event.type)
                                          print("event.delta = " .. event.delta)
 
                                          if event.type == "portrait" then
                                            background:removeSelf()
 
                                            background = display.newImageRect("portrait.png", 320, 480)
                                            displayGroup:insert(background)
 
 
                                            background:toBack()
                                          elseif event.type == "portraitUpsideDown" then
                                            background:removeSelf()
 
                                            background = display.newImageRect("portraitUpsideDown.png", 320, 480)
                                            displayGroup:insert(background)
 
 
                                            background:toBack()
                                          elseif event.type == "landscapeRight" then
                                            background:removeSelf()
 
                                            background = display.newImageRect("landscapeRight.png", 480, 320)
                                            displayGroup:insert(background)
 
 
                                            background:toBack()
                                          elseif event.type == "landscapeLeft" then
 
                                            background:removeSelf()
 
                                            background = display.newImageRect("landscapeLeft.png", 480, 320)
                                            displayGroup:insert(background)
 
 
                                            background:toBack()
                                          end
                                          background.x = display.contentWidth / 2
                                                                                  background.y = display.contentHeight / 2
                                        end)
 
local circle1 = display.newCircle(50, 50, 30)
circle1.x = 50
circle1.y = 50
circle1:setFillColor(0, 255, 255)
 
displayGroup:insert(circle1)
 
local circle2 = display.newCircle(50, 50, 30)
circle2.x = display.contentWidth - 50
circle2.y = 50
circle2:setFillColor(255, 0, 255)
 
displayGroup:insert(circle2)
 
local circle3 = display.newCircle(50, displayGroup.height - 50, 30)
 
circle3:setFillColor(255, 255, 0)
 
displayGroup:insert(circle3)
 
local circle4 = display.newCircle(displayGroup.width - 50, displayGroup.height - 50, 30)
 
circle4:setFillColor(0, 0, 0)
 
displayGroup:insert(circle4)

OK, so, basically, the behavior changes in some incoherent fashion depending on the order of coordinate assignments.

It doesn't make much sense to me.

One thing is sure, I must stop anything else I am doing until I find a rational explanation for what is happening in those sample programs.

Alright, so, there was a problem in my code due to Lua closures. Taking a global snapshot of display.contentHeight and display.contentWidth is a bad idea when you plan to support orientation changes because your globals won't be updated when orientation changes.

The other key thing to understand is that when auto-rotation happens, none of the coordinates of your objects are updated and the old coordinates won't make sense in the new orientation.
So, you have to re-layout your objects and adjust their coordinates after each orientation change.

Project updated: http://dl.dropbox.com/u/29886031/OrientationMadness3.zip

I don't think it's a bug anymore, but it's a gotcha the documentation should cover.

nice one :)

views:1856 update:2011/10/1 9:04:19
corona forums © 2003-2011