(solved)Changing backgrounds when a phsics object goes offscreen

Hey everyone, I am new to Corona (been using it for 3 weeks now) and I am having trouble getting a certain game mechanic to work in my code…

I want to change the background and redraw the main physics object when that object goes offscreen at the bottom. Unfortunately it doesn’t do that, the object just keeps falling.
What I want to accomplish is having a ball fall down from the sky and changing the backgrounds every time it reaches the bottom of the screen and then redrawing the object on top. I want to have 3-4 layers (bgs) until he hits the ground.

The way I coded it would have the screen change and not have it scroll with the actor. I would like to see if I can accomplish this method then maybe I will try to implement the other method.
Any help would be appreciated,

Here is my code:

[code]

module(…, package.seeall)

local localGroup = display.newGroup()
local physics = require(“physics”)
local levelstate = 1
physics.start()
physics.setGravity(0, 8.1)

–***************
–this new method is needed for the each screen!
–***************
function new()

display.setStatusBar( display.HiddenStatusBar )

startGame()
return localGroup

end
–*************
–End of New() Function–
–*************
–==
function startGame()

if (levelstate == 1) then
levelA()
end

if (levelstate == 2) then
levelB()
end

if (levelstate == 3) then
levelC()
end

if (levelstate == 4)then
endLevel()
end

end–end of startGame
function levelA()
local bg = display.newImage(“bg_leveloneA.png”)
local bob = display.newImage(“bob_sprite.png”)
bob.x = _W / 2
physics.addBody(bob)

–if bob exits screen, change levelstate to go to the
–next screen
if (bob.y < 300) then

levelstate = levelstate + 1
end–if

end–end of levelA()

function levelB()
local bg = display.newImage(“bg_leveloneB.png”)
local bob = display.newImage(“bob_sprite.png”)
localGroup:insert(bg)
localGroup:insert(bob)
bob.x = _W / 2
physics.addBody(bob)

end–end of levelB()

function levelC()
local bg = display.newImage(“bg_leveloneB.png”)
local bob = display.newImage(“bob_sprite.png”)
localGroup:insert(bg)
localGroup:insert(bob)
bob.x = _W / 2
physics.addBody(bob)

end–end of LevelC()

function endLevel()
bob.x = _W / 2

end

–=========
–end of page

[/code] [import]uid: 12540 topic_id: 11356 reply_id: 311356[/import]

I should also note that this part was messed around with a bit:

if (bob.y \< 300) then  
   
levelstate = levelstate + 1  
end--if  

I originally had it at (bob.y > 500) but I started to mess with the values to get the if statement to change the levelstate…

also in levelC() method I need to change the background and add other things to the code overall, but I am trying to get the change screen between the first and the second bg to work first.

[import]uid: 12540 topic_id: 11356 reply_id: 41200[/import]

I finally solved my problem. I started using print to debug the function calls and I eventually got things rolling.

Here is my code for others that want to achieve similar goals:
[lua]local localGroup = display.newGroup()
local physics = require(“physics”)
local levelstate = 1
physics.start()
physics.setGravity(0, 8.1)

–***************
–this new method is needed for the each screen!
–***************
function new()

display.setStatusBar( display.HiddenStatusBar )

startGame()
return localGroup

end
–*************
–End of New() Function–
–*************
–==
function startGame()

print(“start game begins”)

if (levelstate == 1) then
levelA()
end

if (levelstate == 2) then
levelB()
end

if (levelstate == 3) then
levelC()
end

if (levelstate == 4)then
endLevel()
end

end–end of startGame
function levelA()
print (“Level A in progress”)
bg = display.newImage(“bg_leveloneA.png”)
bob = display.newImage(“bob_smSprite.png”)
bob.x = _W / 2
bob.y = -20
physics.addBody(bob)

–if bob exits screen, change levelstate to go to the
–next screen
Runtime:addEventListener(“enterFrame”, bobOffscreen)
end–end of levelA()

function levelB()
print(“level B in progress”)
bg = display.newImage(“bg_leveloneB.png”)
bob = display.newImage(“bob_smSprite.png”)

physics.addBody(bob)
bob.y = -20
bob.x = _W / 2

Runtime:addEventListener(“enterFrame”, bobOffscreen)

end–end of levelB()

function levelC()
print(“level C in progress”)

bg = display.newImage(“bg_leveloneC.png”)
bob = display.newImage(“bob_smSprite.png”)

physics.addBody(bob)
bob.y = -20
bob.x = _W / 2
Runtime:addEventListener(“enterFrame”, bobOffscreen)

end–end of LevelC()

function endLevel()
print(“End of Level!! :)”)
bob = display.newImage(“bob_smSprite.png”)
bob.x = _W / 2

end

function bobOffscreen()
print(“bobOffscreen function”)
if (bob.y > 480) then
print("if statement of bobOffscreen / level state = " … levelstate)
–bg:removeself()
levelstate = levelstate + 1
Runtime:removeEventListener(“enterFrame”, bobOffscreen)
startGame()
end–if

end–end of bobOffscreen

–=========
–end of page[/lua]

May the code be with you,

~jeff [import]uid: 12540 topic_id: 11356 reply_id: 41355[/import]