my second "<, >" doesn't want to work.

Ok so i have a scoring mechanism for a scoreTest app and here’s the problem…

So i have this piece of code.

local box2 = display.newImageRect("box2.png", 30, 30 ) box2.x = display.contentCenterX box2.y = 350 local function updateHighScore(event) if event.phase == "began" then if score \> highScore then highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score) elseif highScore \> score then print("test") end end end box2:addEventListener("touch", updateHighScore)

So when score is greater than highScore and i press the box then the highScore changes to the score

now when highScore is greater than score then its supposed to print “test” but nothing happens… what am i doing wrong?

–  I Use SubLime Text Editor

What happens if highScore == score? You don’t test for that condition.  Perhaps this would make more sense:

 if score \> highScore then highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score) else print("test") end

Heres a video of what i want and whats not happening…

https://youtu.be/2ejLV-EQAH4

can anyone help?

Have you tried hardcoding your highScore and score values to that they fulfill your second condition, to confirm everything is being called correctly? Have you added in print statements to confirm the values that you believe should be available?

Ya i ve tried this.

local function updateHighScore(event) if event.phase == "began" then if score \> highScore then highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score) print("hello") end if score \< highScore then print("test") end end end box2:addEventListener("touch", updateHighScore)

It prints hello but when the score is smaller than highScore it doesnt print test…

I think your problem is here:

 highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score)

You’re changing the visible text to the value of score. You never change the value of highScore.  Since highScore starts at 0 and you add 0 to it, highScore stays 0. You never show highScore, you show score. So score is always greater than highScore.

Rob

ok so i did this and i didnt get anything working… everything stayed the same…

 265 posts Corona SDK Today, 06:06 PM Ya i ve tried this. local function updateHighScore(event) if event.phase == "began" then if score \> highScore then highScoreTxt.text = string.format("highScore: %d", score) print("hello") end if score \< highScore then print("test") end end end box2:addEventListener("touch", updateHighScore

I meant, actually printing out the values for score and highScore. This is a very simple function so something must be slipping through the cracks. As to my first suggestion, why don’t you set your variable values in the actual function, to make sure it’s working correctly. Then, you can move those hardcoded variables directly above the function being called. Then, you move the hardcoded values to the function that is setting the variables in the first place. Then you can track back where the breakdown has occurred. 

I dont seem to follow … If i send you a zip file would you be able look it over and maybe get it working? everything is very simple in the file…

Did you read my post above?
 

Yes i did… but im getting so confused… 

You have one variable called “score” that you track the current score in.  You have another variable called “highScore” that you keep track of the high score in. Then you have a display.newText object that you use to show “score” (the one incremented every second) and another display.newText() that you want to show the high score with.

You are successfully incrementing “score” and showing that as well as resetting it.  However in your button handler where you want to record the high score, you’re doing:

highScore = highScore + 0

instead of:

highScore = score

which is what you want to do to record the high score.  That’s why your if statement isn’t working.  You should also go back to my previous point and do the “else” model.

You have a second error when you go to display highScore. You’re not displaying highScore, but score.

What happens if highScore == score? You don’t test for that condition.  Perhaps this would make more sense:

 if score \> highScore then highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score) else print("test") end

Heres a video of what i want and whats not happening…

https://youtu.be/2ejLV-EQAH4

can anyone help?

Have you tried hardcoding your highScore and score values to that they fulfill your second condition, to confirm everything is being called correctly? Have you added in print statements to confirm the values that you believe should be available?

Ya i ve tried this.

local function updateHighScore(event) if event.phase == "began" then if score \> highScore then highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score) print("hello") end if score \< highScore then print("test") end end end box2:addEventListener("touch", updateHighScore)

It prints hello but when the score is smaller than highScore it doesnt print test…

I think your problem is here:

 highScore = highScore + 0 highScoreTxt.text = string.format("highScore: %d", score)

You’re changing the visible text to the value of score. You never change the value of highScore.  Since highScore starts at 0 and you add 0 to it, highScore stays 0. You never show highScore, you show score. So score is always greater than highScore.

Rob

ok so i did this and i didnt get anything working… everything stayed the same…

 265 posts Corona SDK Today, 06:06 PM Ya i ve tried this. local function updateHighScore(event) if event.phase == "began" then if score \> highScore then highScoreTxt.text = string.format("highScore: %d", score) print("hello") end if score \< highScore then print("test") end end end box2:addEventListener("touch", updateHighScore