I don’t see any event listeners which are checking your if statement. If you simply create a stand alone if statement then it will run once when the file is loaded, and not be run again until you call that function. You need some event listener to trigger the statement, so I have used a timer which runs every 1000ms and runs the function checking where marios position is.
In order to control how your images are being loaded on screen, you should also use display groups. This helps you control the layers on screen to make sure Mario is not hidden behind the background of your game, and the same for your gameover image.
Lastly, I think it is good practice to create your objects outside of the if statement. This may be in a table, or alone like in the below example. (This is debatable since I am only a novice and am not sure what the best practice for managing the assets are).
[lua]–creates display group to order the layers of your images
local screen = display.newGroup()
–mario, the plumber, the man
local mario = display.newImage(“mario.png”)
mario.x, mario.y = x,x --starting point on level
mario.isAlive = yes
–gameover image
local gameover = display.newImage(“gameover.png”)
gameover.x, gameover.y = x,x --somewhere off the screen so it is ready to be used
gameover.isActive = no --control variable, helps keep track of active objects on screen
–display groups
–insert them in order of background --> foreground
screen:insert(mario)
screen:insert(gameover)
–event which runs to check if mario is dead at the interval depicted in the event timer
function isMarioDead( event )
if(mario >= 700) then
gameover.x, gameover.y = display.contentWidth /2, display.contentHeight /2
gameover.isActive = yes
mario.isAlive = no
–play mario death animation here
end
end
timer.performWithDelay( 1000, isMarioDead, -1 )[/lua] [import]uid: 135255 topic_id: 33184 reply_id: 131829[/import]