Little code problem

So I have a sprite named “mario”. When mario falls a certain distance aka off screen I want it to produce a game over screen. Here’s my code:

if mario.y \>= 700 then  
 local gameover = display.newImageRect( "gameover.png")   
  
end  

I’m a rookie at coding so I’m not sure why this isn’t pulling up the gameover.png when mario falls past 700
Any help would be awesome.
Cheers
-Andrew [import]uid: 72845 topic_id: 33184 reply_id: 333184[/import]

I had run into something similar at one point in my app as well. Couldn’t find any problems with the syntax. Finally gave up, and just changed it to something like

if mario.y \> 699 then  
 local gameover = display.newImageRect( "gameover.png")   
end  

And it worked. [import]uid: 79933 topic_id: 33184 reply_id: 131788[/import]

Still no luck : (

I’ll post my code in hopes that maybe that helps

[code]
local instance = sprite.newSprite(set);
instance.x = display.contentWidth/70
physics.addBody(instance, {bounce = 0.2, friction = .1})
instance.name = “mario”
instance.angularDamping = 5000
instance.isFixedRotation = true
if instance.y > 700 then
local gameover = display.newImageRect( “gameover.png”)

gameover.x = 600
gameover.y = 430

end
[/code] [import]uid: 72845 topic_id: 33184 reply_id: 131798[/import]

If you print instance.y just before the test, what is its value?

If you set instance.y = 701 just before the test, what happens? [import]uid: 79933 topic_id: 33184 reply_id: 131799[/import]

I’ve noticed that if I make:

if mario.y > 1
I get no display of the game over image

If I make it:
if mario.y > -1

then the game over image is already on the screen when the level loads

As for printing a value, I’m writing this in TextWrangler and have no idea how to run the code to see a value print out.
As I said before I’m a big rookie : ( [import]uid: 72845 topic_id: 33184 reply_id: 131810[/import]

Are you on a mac? Launch Corona Terminal, instead of the simulator (I think the same is true on windows). From there, choose your main.lua file to launch.

print(" Hello.") statements will then output in the terminal window. Stone knives and bearskins… But it’s something. [import]uid: 79933 topic_id: 33184 reply_id: 131816[/import]

On a mac, tried to import my main.lua into the corona terminal and I couldn’t. [import]uid: 72845 topic_id: 33184 reply_id: 131818[/import]

Hello @fakemode,
If you don’t have this “position check” within a Runtime listener, it must be put inside one. Simply checking its position on the first cycle of the app’s execution will perform that check only once. That’s why you’re getting a response with the “>-1” case… Mario is already at y=0 (I assume), thus the case checks as true. But if you want to check the case continually, you need to check every frame using a Runtime listener.

Brent
[import]uid: 9747 topic_id: 33184 reply_id: 131822[/import]

huh? ok…

So on the mac, in your applications folder, is the corona SDK folder. In there is the app “corona Terminal”… drag that to your desktop/make a link.

Close any terminal you might have open, and then launch the link. It should automatically give you the welcome dialog, In that, hit the “Simulator” button, and then choose your main.lua…

Then you should see the console output, print statements, etc… [import]uid: 79933 topic_id: 33184 reply_id: 131821[/import]

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]

+1 RE appearing the event isn’t firing – just be sure to cancel that timer if you change scenes etc. [import]uid: 52491 topic_id: 33184 reply_id: 131834[/import]

I had run into something similar at one point in my app as well. Couldn’t find any problems with the syntax. Finally gave up, and just changed it to something like

if mario.y \> 699 then  
 local gameover = display.newImageRect( "gameover.png")   
end  

And it worked. [import]uid: 79933 topic_id: 33184 reply_id: 131788[/import]

Still no luck : (

I’ll post my code in hopes that maybe that helps

[code]
local instance = sprite.newSprite(set);
instance.x = display.contentWidth/70
physics.addBody(instance, {bounce = 0.2, friction = .1})
instance.name = “mario”
instance.angularDamping = 5000
instance.isFixedRotation = true
if instance.y > 700 then
local gameover = display.newImageRect( “gameover.png”)

gameover.x = 600
gameover.y = 430

end
[/code] [import]uid: 72845 topic_id: 33184 reply_id: 131798[/import]

If you print instance.y just before the test, what is its value?

If you set instance.y = 701 just before the test, what happens? [import]uid: 79933 topic_id: 33184 reply_id: 131799[/import]

I’ve noticed that if I make:

if mario.y > 1
I get no display of the game over image

If I make it:
if mario.y > -1

then the game over image is already on the screen when the level loads

As for printing a value, I’m writing this in TextWrangler and have no idea how to run the code to see a value print out.
As I said before I’m a big rookie : ( [import]uid: 72845 topic_id: 33184 reply_id: 131810[/import]

Are you on a mac? Launch Corona Terminal, instead of the simulator (I think the same is true on windows). From there, choose your main.lua file to launch.

print(" Hello.") statements will then output in the terminal window. Stone knives and bearskins… But it’s something. [import]uid: 79933 topic_id: 33184 reply_id: 131816[/import]

On a mac, tried to import my main.lua into the corona terminal and I couldn’t. [import]uid: 72845 topic_id: 33184 reply_id: 131818[/import]

Hello @fakemode,
If you don’t have this “position check” within a Runtime listener, it must be put inside one. Simply checking its position on the first cycle of the app’s execution will perform that check only once. That’s why you’re getting a response with the “>-1” case… Mario is already at y=0 (I assume), thus the case checks as true. But if you want to check the case continually, you need to check every frame using a Runtime listener.

Brent
[import]uid: 9747 topic_id: 33184 reply_id: 131822[/import]

huh? ok…

So on the mac, in your applications folder, is the corona SDK folder. In there is the app “corona Terminal”… drag that to your desktop/make a link.

Close any terminal you might have open, and then launch the link. It should automatically give you the welcome dialog, In that, hit the “Simulator” button, and then choose your main.lua…

Then you should see the console output, print statements, etc… [import]uid: 79933 topic_id: 33184 reply_id: 131821[/import]

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]