if Score = # then (end level)

Hello,

I am very new to corona and programming in general. I have done pretty well so far but I am running into some trouble. I am trying to figure out if there is a way to end a level depending on your score. For example if a player reaches a score of 1000 then the end level screen follows. I already have the basic score, highscore, save score, and load score functions down. I have done a lot of research but can’t seem to find anything explaining this in particular. If anyone has any ideas that would be great!

Thanks again!

-Brandon

Assuming your score is in a variable called score:

if score >= 1000 then

     – put code here to end your level

end

Rob

I have tried that but it doesn’t do anything. This is my score code.

– The Score

score = 0

scoreText= display.newText( “Score: 0”,0,0, “Helvetica”, 70 )

gui.overlay:insert(scoreText)

scoreText.anchorX = 0

scoreText.anchorY = 0

scoreText.x = display.screenOriginX + 30

scoreText.y = display.screenOriginY + 10 + bannerOffset

– Then on collision

local function onCollision( event )

if isPlaying then

if ( event.phase == “began” ) then

if (event.object1.myType == “player”) and (event.object2.myType == “good”) then

if not event.object2.collidedz then

          event.object2.collidedz = true

          transition.to( event.object2, { xScale=1.5, yScale=1.5, rotation=270, alpha=0.01, time=10} )

score = score + points_per_food

Is there something I am missing here? 

-Thanks for your time.

score = score + points_per_food

scoreText.text = "Score: " … score

Rob

That does not seem to do anything either. The following is my code from the score and what i’m trying to accomplish with the if score >= 1000

– The Score

score = 0

scoreText= display.newText( “Score: 0”,0,0, “Helvetica”, 70 )

gui.overlay:insert(scoreText)

scoreText.anchorX = 0

scoreText.anchorY = 0

scoreText.x = display.screenOriginX + 30

scoreText.y = display.screenOriginY + 10 + bannerOffset

scoreText.text = "Score: " … score

if score >= 1000 then

lifeIcons[lives].isVisible = false

lives = lives - 3

player:setSequence( “stand” )

isPlaying = false

isPaused = true

timer.cancel(spawnAnim)

backImg = display.newImage( “images/btn1_back.png” )

gui.overlay:insert(backImg)

backImg.x = display.screenOriginX-300

backImg.y = display.screenOriginY + 650

transition.moveTo( backImg, { x=display.contentWidth/2, time=700, transition=easing.outExpo } )

backImg:addEventListener( “tap”, backButtonListener )

restartImg = display.newImage( “images/btn1_restart.png” )

gui.overlay:insert(restartImg)

restartImg.x = display.actualContentWidth+300

restartImg.y = display.screenOriginY  + 780

transition.moveTo( restartImg, { x=display.contentWidth/2, time=700, transition=easing.outExpo } )

restartImg:addEventListener( “tap”, restartButtonListener )

deathOverlay.isVisible = true

tmpEndScoreText = display.newText(“You Won!”,0,0, “Helvetica”, 120 )

tmpEndScoreText.x = display.contentWidth+display.contentWidth/2

tmpEndScoreText.y = display.contentHeight/2-240

gui.overlay:insert(tmpEndScoreText)

transition.moveTo( tmpEndScoreText, { x=display.contentWidth/2, time=700, transition=easing.outExpo } )

pauseImg.isVisible = false

-Thanks

Is your score behind your background?  Corona layers objects based on the order you insert them into the group.

Rob

The contents of your if/then statement aren’t the problem - from what I gather by the code you posted, the issue is when you call that code. An if/then statement on its own is a one-time thing. It checks to see if the conditions are met, and if not, the app just chugs along and never revisits that block of code unless you explicitly tell it to. When you enter the code “if score >= 1000 then…” you are basically saying “if the score is >=1000 RIGHT NOW, then take these steps,” not (as I suspect you thought) “if score is ever >= 1000 then take tese steps.” So based on the code you posted, your app is checking to see if score >= 1000 milliseconds after you set the score to zero, and never again. To have your app check the score constantly, you can place that block of code in a Runtime “enterFrame” listener, but a more efficient way would be to check any time the score changes based on a collision (assuming that the collision is the only way to raise a player’s score…I’m making some educated guesses as to how your game works). So after you bump up a player’s score in the onCollision function, inside that same function you should update your scoreText’s.text propery as Rob described (so the visual score is updated), then check to see if score is >= 1000, and if it is, run the code to end the game. Good luck!

Thanks so much guys! @schroederapps it worked perfectly. I placed it in the collision on the “good” objects which give you the score and now its good to go! Thanks again!

Assuming your score is in a variable called score:

if score >= 1000 then

     – put code here to end your level

end

Rob

I have tried that but it doesn’t do anything. This is my score code.

– The Score

score = 0

scoreText= display.newText( “Score: 0”,0,0, “Helvetica”, 70 )

gui.overlay:insert(scoreText)

scoreText.anchorX = 0

scoreText.anchorY = 0

scoreText.x = display.screenOriginX + 30

scoreText.y = display.screenOriginY + 10 + bannerOffset

– Then on collision

local function onCollision( event )

if isPlaying then

if ( event.phase == “began” ) then

if (event.object1.myType == “player”) and (event.object2.myType == “good”) then

if not event.object2.collidedz then

          event.object2.collidedz = true

          transition.to( event.object2, { xScale=1.5, yScale=1.5, rotation=270, alpha=0.01, time=10} )

score = score + points_per_food

Is there something I am missing here? 

-Thanks for your time.

score = score + points_per_food

scoreText.text = "Score: " … score

Rob

That does not seem to do anything either. The following is my code from the score and what i’m trying to accomplish with the if score >= 1000

– The Score

score = 0

scoreText= display.newText( “Score: 0”,0,0, “Helvetica”, 70 )

gui.overlay:insert(scoreText)

scoreText.anchorX = 0

scoreText.anchorY = 0

scoreText.x = display.screenOriginX + 30

scoreText.y = display.screenOriginY + 10 + bannerOffset

scoreText.text = "Score: " … score

if score >= 1000 then

lifeIcons[lives].isVisible = false

lives = lives - 3

player:setSequence( “stand” )

isPlaying = false

isPaused = true

timer.cancel(spawnAnim)

backImg = display.newImage( “images/btn1_back.png” )

gui.overlay:insert(backImg)

backImg.x = display.screenOriginX-300

backImg.y = display.screenOriginY + 650

transition.moveTo( backImg, { x=display.contentWidth/2, time=700, transition=easing.outExpo } )

backImg:addEventListener( “tap”, backButtonListener )

restartImg = display.newImage( “images/btn1_restart.png” )

gui.overlay:insert(restartImg)

restartImg.x = display.actualContentWidth+300

restartImg.y = display.screenOriginY  + 780

transition.moveTo( restartImg, { x=display.contentWidth/2, time=700, transition=easing.outExpo } )

restartImg:addEventListener( “tap”, restartButtonListener )

deathOverlay.isVisible = true

tmpEndScoreText = display.newText(“You Won!”,0,0, “Helvetica”, 120 )

tmpEndScoreText.x = display.contentWidth+display.contentWidth/2

tmpEndScoreText.y = display.contentHeight/2-240

gui.overlay:insert(tmpEndScoreText)

transition.moveTo( tmpEndScoreText, { x=display.contentWidth/2, time=700, transition=easing.outExpo } )

pauseImg.isVisible = false

-Thanks

Is your score behind your background?  Corona layers objects based on the order you insert them into the group.

Rob

The contents of your if/then statement aren’t the problem - from what I gather by the code you posted, the issue is when you call that code. An if/then statement on its own is a one-time thing. It checks to see if the conditions are met, and if not, the app just chugs along and never revisits that block of code unless you explicitly tell it to. When you enter the code “if score >= 1000 then…” you are basically saying “if the score is >=1000 RIGHT NOW, then take these steps,” not (as I suspect you thought) “if score is ever >= 1000 then take tese steps.” So based on the code you posted, your app is checking to see if score >= 1000 milliseconds after you set the score to zero, and never again. To have your app check the score constantly, you can place that block of code in a Runtime “enterFrame” listener, but a more efficient way would be to check any time the score changes based on a collision (assuming that the collision is the only way to raise a player’s score…I’m making some educated guesses as to how your game works). So after you bump up a player’s score in the onCollision function, inside that same function you should update your scoreText’s.text propery as Rob described (so the visual score is updated), then check to see if score is >= 1000, and if it is, run the code to end the game. Good luck!

Thanks so much guys! @schroederapps it worked perfectly. I placed it in the collision on the “good” objects which give you the score and now its good to go! Thanks again!