In this sample code:
local livesLeft = display.newText( "3", centerX, centerY, native.systemFont, 24 )
local screenTapped = function(event)
if event.phase == "began" and tonumber(livesLeft.text) \> 0 then
livesLeft.text = livesLeft.text - 1
end
end
Since Lua is dynamically typed I can do this on line 5:
livesLeft.text = livesLeft.text - 1
…and treat the string as a number. But look at the line right above that, line 4 – when I tried to do this:
if event.phase == "began" and tonumber(livesLeft.text) \> 0 then
… I ended up having to use tonumber() to get things to work.
Why was I able to treat livesLeft.text as a number in one place, but had to cast it as a number in the other?
Is it because line 4 is a logical operation and line 5 is a mathematical operation? I guess I could understand that, except if Lua is dynamically typed and I’m using a number on the right side of the logical operation, shouldn’t the left side dynamically change to a number?
Jay
[import]uid: 9440 topic_id: 7173 reply_id: 307173[/import]