Screen Freezes

Everybody,

I am having a problem with this screen. I have two text fields. One text field the is a global variable (numberFieldSeven) that brings a number over. In the other textfield (numberFieldEight) the player has to take that same number and rearrange it. I want to have an native alert come up if the player did not use the same digits from the global variable. Can you look at the code to see what is causing an error to keep my screen from being able to transition to the next screen? The terminal is not giving good information. It is just giving a run-time error.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local ui = require(“ui”)
– Predefine local objects for use later
local numberFieldSeven, numberFieldEight,onComplete
local fields = display.newGroup()
local inputFontSize = 30
local inputFontHeight = 30
local tHeight = 50
local screen5 = display.newImage (“screen5.png”)
localGroup:insert(screen5)
–> This sets Screen5
local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 270
smallbrain.y = 430
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain

local function touchedSmallbrain (event)
if (“ended” == event.phase) then
_G.numberEight = numberFieldEight.text

–>This starts the native alert
if sortnum(number1) == sortnum(number2) then
print(“same”)
director:changeScene (“screen6”)
media.playEventSound( “click_x.caf” )
else
–print(“not same”)
local alert = native.showAlert(“Error!”,“Your numbers do not tally!”, { " Ok " }, onComplete )
end
—> end of the native alert
end
end

smallbrain:addEventListener (“touch”, touchedSmallbrain)
– Tapping screen dismisses the keyboard
local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end
Runtime:addEventListener( “tap”, listener )

– This cleanUp function will remove all listeners from the scene since
– Director can’t remove listeners itself

local cleanUp = function()
Runtime:removeEventListener(“tap”, listener)
smallbrain:removeEventListener(“touch”, touchedSmallbrain)
end

—>code to determine if numbers match
local function sortnum(number)
local numbersin = {}
local reminder
local returnnum = 0
if number ~=nil then
while number ~=0 do
reminder = number % 10
number = (number - (number % 10)) /10
table.insert(numbersin,reminder)
end
local mult = 1
table.sort(numbersin)

for i=1,#numbersin,1 do
returnnum = returnnum + (numbersin[i] * mult )
mult = mult * 10
end
end
return returnnum
end
—>This ends the number match

function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Return to gameboard
end
end
end

numberFieldSeven = native.newTextField( 50, 80, 85, tHeight, onnumberFieldSeven )
numberFieldSeven.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldSeven.inputType = “number”
numberFieldSeven.align = “center”
numberFieldSeven.text = _G.numberOne

numberFieldEight = native.newTextField( 180, 80, 85, tHeight, onnumberFieldEight )
numberFieldEight.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldEight.align = “center”
numberFieldEight.inputType = “number”
– Add fields to our new group

localGroup:insert(numberFieldSeven)
localGroup:insert(numberFieldEight)
–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment

return localGroup

end[/lua] [import]uid: 72372 topic_id: 14196 reply_id: 314196[/import]

what is the runtime error? [import]uid: 24641 topic_id: 14196 reply_id: 52294[/import]

This is the error.

Runtime error
/Users/ebay/Desktop/MindTapper/screen5.lua:55: attempt to call global ‘sortnum’ (a nil value)
stack traceback:
[C]: in function ‘sortnum’
/Users/ebay/Desktop/MindTapper/screen5.lua:55: in function
?: in function <?:215> [import]uid: 72372 topic_id: 14196 reply_id: 52296[/import]

place the sortnum function above where you call it in touchedsmallbrain [import]uid: 24641 topic_id: 14196 reply_id: 52297[/import]

You mean the section that that is used to match the numbers? The code showing below?

[lua]—>code to determine if numbers match
local function sortnum(number)
local numbersin = {}
local reminder
local returnnum = 0
if number ~=nil then
while number ~=0 do
reminder = number % 10
number = (number - (number % 10)) /10
table.insert(numbersin,reminder)
end
local mult = 1
table.sort(numbersin)

for i=1,#numbersin,1 do
returnnum = returnnum + (numbersin[i] * mult )
mult = mult * 10
end
end
return returnnum
end
—>This ends the number match[/lua] [import]uid: 72372 topic_id: 14196 reply_id: 52298[/import]

I did move that section and the screen does move to the next slide but I guess my code is not correct because it’s not detecting the error or showing the native alert. Do you see anything else that may be wrong?

[import]uid: 72372 topic_id: 14196 reply_id: 52300[/import]