App on device is different than app on simulator!

Hello,

        I’ve done my app and it works great on the Corona Simulator, but, once I run it on my device, it doesn’t run a bunch of instruction.

These are:

function onCollision( event ) if ( event.phase == "began" ) then if event.object1.myName == “gd” and event.object2.myName == “hero” then speed = 0 local matchScore = score.get() local bestScore = (bestScore or 0) + score.load() if (matchScore \< bestScore) then best.alpha = 1 scoreToBeat.text = score.load() scoreToBeat.alpha = 1 else score.save() newRecord.alpha = 1 end end end end Runtime:addEventListener( "collision", onCollision )

Particularly, it performs only “speed = 0” and only the first time I run the app, which means that if I start a new game, it won’t even work “speed = 0”.

I’m 100% sure that the app is running on the device is the same that runs on the simulator (I tried to change some text).

What can I do? Thanks!

Some runtime error maybe occures?

Have you looked at your device’s console log to look for errors?

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Thank you both, yes, I’ve took a look a the device’s console log and it says “Attempt to compare number with nil” on 

The code I’m using now is the following at the error is on “(if a<b)then…newRecord.alpha = 1 end”

function onCollision( event ) if ( event.phase == "began" ) then if event.object1.myName == "ground" and event.object2.myName == "spaceShip" then local a = score.get() print(a) local b = score.load() print(b) if (a \< b) then best.alpha = 1 scoreToBeat.text = score.load() scoreToBeat.alpha = 1 else score.save() newRecord.alpha = 1 end timer.cancel(tmrScore) gameOver.alpha = 1 tapToReplay.alpha = 1 replay.alpha = 0.01 fade.alpha = 0 timer.cancel(tmrIS) spaceShip.alpha = 1 if(playEffects) then media.playEventSound( "sounds/gameover.mp3" ) playEffects = false end speed = 0 end end end Runtime:addEventListener( "collision", onCollision )

The fact is that if I print both a and b they aren’t nil!

Can you post more of the stack trace?

Sorry I didn’t catch what you mean with “stack trace”. Is it the log file?

Yes, there should be more lines to the error message.  It would help to have a few lines before too.

Rob

Yes, I’ll post the complete log file as soon as I can (few hours)

Here you are:

Apr 5 14:11:00 iPhone-di-Lorenzo gameInDev2[2507] \<Warning\>: 21 Apr 5 14:11:00 iPhone-di-Lorenzo gameInDev2[2507] \<Warning\>: Could not read scores from scorefile.txt . Apr 5 14:11:00 iPhone-di-Lorenzo gameInDev2[2507] \<Warning\>: nil Apr 5 14:11:00 iPhone-di-Lorenzo gameInDev2[2507] \<Warning\>: Runtime error /Users/developer/Desktop/gameInDev2/game.lua:260: attempt to compare number with nil stack traceback: [C]: ? /Users/developer/Desktop/gameInDev2/game.lua:260: in function \</Users/developer/Desktop/gameInDev2/game.lua:238\> ?: in function \<?:218\> Apr 5 14:11:00 iPhone-di-Lorenzo gameInDev2[2507] \<Warning\>: Runtime error stack traceback: [C]: ? /Users/developer/Desktop/gameInDev2/game.lua:260: in function \</Users/developer/Desktop/gameInDev2/game.lua:238\> ?: in function \<?:218\>

as you can see it says “Could not read scores from scorefile.txt”. I’ve checked out the project sandbox and I figured out that it can write on that file, it can’t just read it

And then there’s “attempt to compare number with nil” at line 260. which is 

if (a \< b) then

The error is likely in the score module.  I suspect that’s the source of the problem.  Can you post your score.lua?

I think that too, anyway here it is

-- Score Module -- local M = {} -- create our local M = {} M.score = 0 function M.init( options ) local customOptions = options or {} local opt = {} opt.fontSize = customOptions.fontSize or 24 opt.font = customOptions.font or native.systemFontBold opt.x = customOptions.x or display.contentCenterX opt.y = customOptions.y or opt.fontSize \* 0.5 opt.maxDigits = customOptions.maxDigits or 6 opt.leadingZeros = customOptions.leadingZeros or false M.filename = customOptions.filename or "scorefile.txt" local prefix = "" if opt.leadingZeros then prefix = "0" end M.format = "%" .. prefix .. opt.maxDigits .. "d" M.scoreText = display.newText(string.format(M.format, 0), opt.x, opt.y, opt.font, opt.fontSize) return M.scoreText end function M.set( value ) M.score = value M.scoreText.text = string.format(M.format, M.score) end function M.get() return M.score end function M.add( amount ) M.score = M.score + amount M.scoreText.text = string.format(M.format, M.score) end function M.save() local path = system.pathForFile( M.filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = tostring( M.score ) file:write( contents ) io.close( file ) return true else print("Error: could not read ", M.filename, ".") return false end end function M.load() local path = system.pathForFile( M.filename, system.DocumentsDirectory) local contents = "" local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) local score = tonumber(contents); io.close( file ) return score end print("Could not read scores from ", M.filename, ".") return nil end return M

If you have never called score.save(), it won’t have a save file to read from.  I suspect that when you call score.load() it’s returning nil and you are trying to then compare your score to nil giving you that error.

What I would do is I would do:

local b = score.load()

if b == nil then

    b = 0

end

That way it will cover our base for not having has a previously saved score.

Great it works!! Thanks a lot!

Some runtime error maybe occures?

Have you looked at your device’s console log to look for errors?

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Thank you both, yes, I’ve took a look a the device’s console log and it says “Attempt to compare number with nil” on 

The code I’m using now is the following at the error is on “(if a<b)then…newRecord.alpha = 1 end”

function onCollision( event ) if ( event.phase == "began" ) then if event.object1.myName == "ground" and event.object2.myName == "spaceShip" then local a = score.get() print(a) local b = score.load() print(b) if (a \< b) then best.alpha = 1 scoreToBeat.text = score.load() scoreToBeat.alpha = 1 else score.save() newRecord.alpha = 1 end timer.cancel(tmrScore) gameOver.alpha = 1 tapToReplay.alpha = 1 replay.alpha = 0.01 fade.alpha = 0 timer.cancel(tmrIS) spaceShip.alpha = 1 if(playEffects) then media.playEventSound( "sounds/gameover.mp3" ) playEffects = false end speed = 0 end end end Runtime:addEventListener( "collision", onCollision )

The fact is that if I print both a and b they aren’t nil!

Can you post more of the stack trace?

Sorry I didn’t catch what you mean with “stack trace”. Is it the log file?

Yes, there should be more lines to the error message.  It would help to have a few lines before too.

Rob

Yes, I’ll post the complete log file as soon as I can (few hours)