problem with simple camera which only moves on the y axis. (drag and drop code provided)

Ok, so I have a very simple camera which moves in the opposite direction to the player, when the player object bounces up towards the top of the screen. When the player starts to fall, I'd like the ground to move back to it's original position, matching the players speed, (it's for a half pipe skating game).

I've got the camera working fine, but it refuses to move the "masterGroup" back to it's original position. There appears to be an offset of roughly 10.2 pixels every time. This may be down to BOX2D making the player1 object bounce higher each time. I'm not worried how high he goes, just so long as the masterGroup moves back to it's original position each time.

I've been mulling this problem for ages, and am now really stuck, I've tried everything I can think of. Is it maybe a bug? Can someone please offer me some advice?

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
physics = require("physics")
physics.start()
physics.setGravity(0,9.8)
 
_H = display.contentHeight;
_W = display.contentWidth;
 
local masterGroup = display.newGroup()
local plangroup = display.newGroup()
local playergroup = display.newGroup()
 
masterGroup:insert(plangroup)
masterGroup:insert(playergroup)
 
function physObjects()
player1 = display.newRect (0, 0, 30, 30)
player1.x = _W / 2 
player1.y = 41 + player1.contentHeight / 2 -- was 35
player1:setFillColor(0, 0, 255)
physics.addBody(player1, {density = 1.0, friction = 0, bounce = 1})
player1.myName = "player1"
playergroup:insert(player1)
 
function velocity()
        vx, vy = player1:getLinearVelocity()
        --print (vy)
end
Runtime:addEventListener("enterFrame", velocity) 
 
player1.collision = onLocalCollision
player1:addEventListener( "collision", player1 )
 
ground = display.newRect (0, 0, _W, 5)
ground.x = _W / 2
ground.y = _H - ground.contentHeight / 2
ground:setFillColor(0, 255, 0)
physics.addBody(ground, "static", {density = 1.0, friction = 0, bounce = 0.2})
ground.myName = "ground"
playergroup:insert(ground)
 
end
 
function background()
back1 = display.newRect (0, 0, _W, 96)
back1.x = _W / 2
back1.y = _H - back1.contentHeight / 2
back1:setFillColor(244,244,244) 
plangroup:insert(back1)
 
back2 = display.newRect (0, 0, _W, 96)
back2.x = _W / 2
back2.y = _H - back1.contentHeight / 2 - 96
back2:setFillColor(215,215,215) 
plangroup:insert(back2)
 
back3 = display.newRect (0, 0, _W, 96)
back3.x = _W / 2
back3.y = _H - back1.contentHeight / 2 - 96 * 2
back3:setFillColor(174,174,174) 
plangroup:insert(back3)
 
back4 = display.newRect (0, 0, _W, 96)
back4.x = _W / 2
back4.y = _H - back1.contentHeight / 2 - 96 * 3
back4:setFillColor(120,120,120) 
plangroup:insert(back4)
 
back5 = display.newRect (0, 0, _W, 96)
back5.x = _W / 2
back5.y = _H - back1.contentHeight / 2 - 96 * 4
back5:setFillColor(65,65,65) 
plangroup:insert(back5)
end
 
function onLocalCollision( self, event )
        if ( event.phase == "began" ) then
                 
                                ----------------------------------player hits ground---------------------------------------------------------------------------
                        if (self.myName == "player1" and event.other.myName == "ground") then
                                                                canCamMove = true
                        end
                end
end             
 
function dynamicCam()
                canCamMove = false
        function cam()
                if canCamMove == true then 
                        if player1.y < 96 then 
                                        repeat masterGroup.y = masterGroup.y - vy/20 until masterGroup.y <= _H  
                                        print (masterGroup.y)
                        end
                end
        end
        
        Runtime:addEventListener("enterFrame", cam) 
end
 
function startGame()
        physObjects()
        background()
        dynamicCam()
end
 
startGame()

Please people, please can someone post a reply? I'm completely stuck and have written some drag and drop code to make it easier.

I don't want to beg...

If you really need help with code snippets or debugging you should use support here; http://www.anscamobile.com/corona/support/

Otherwise, hopefully someone will have time to go through all your code - you did provide plug and play so that's a plus.

Peach :)

Hey Peach,

Thanks for the response.

It's the cam() function between lines 87 and 97 which is causing the problem, the rest of the code is just to display objects/ setup physics etc.

I just need to know why the strange box2D bounce issue is having an effect on line 90 of my code? (the background is 10.2 pixels lower each time the box bounces).

Again, thanks for the response.

Dan

views:1669 update:2011/10/22 9:46:13
corona forums © 2003-2011