Name.Box popup error

I’m having trouble with my “Name Box” in trying to test run for iPad. When I test the app and click the Name Box (See screen shot), I get the error Message which points to line 168 of my users.lua, but I can’t seem to figure out what’s wrong. Anyone have an idea how to fix this as I’ve never had this problem before? I’m using Corona Simulator 2014_2478. Thanks!

~John Kim

Hi John,

I’m not clear on what object is “nameBox” in the screenshot. It appears to be a button, but then in your code, it’s a native text field. Is the text field actually that white box above the yellow button? You circled the yellow button, so it’s confusing.

As for the error, it appears to be a simple up-value syntax error. Where did you initially declare “nameBox”? Or are you trying to make it global? If so, globals should be avoided whenever possible.

Finally, I should mention that native objects can not be inserted into display groups, as you’re doing on line 174. Native objects are separate from the OpenGL “canvas” and thus you must treat them slightly differently.

Best regards,

Brent

This came up the other day.  The format for creating a native.newTextField() where you include the call back listener in the create call isn’t working.  You need to leave that off and set the listener via a second call.  See http://docs.coronalabs.com/api/library/native/newTextField.html

Rob

Hi Brent and Rob: Thanks for the prompt feedback. Brent, sorry about the white box on the image, my mistake. What I meant to say was that if the user tapped the yellow “Name” button, it would trigger a popup text field box to enter a childs name. Peach Pellen helped me with that code a few years back, so it’s probably outdated! Here is what it looks like:

–NEW FILE FUNCTION
–This function is called when a user clicks on an empty file
local function newFile ()
    --Creates a display group for popup images to be stored in for easy management
    local popupGroup = display.newGroup()
    
    --Mask the background with a semi transparent “layer”
    local safeMask = display.newRect( 0, 0, 1024, 768 )
    safeMask:setFillColor(0,0,0,180)
    
    --Prevent event propogation (users can’t click buttons if popup is open)
    local function blocker ()
        return true
    end
    --We assign touch and tap listeners so neither touches or taps can propogate
    safeMask:addEventListener(“tap”, blocker)
    safeMask:addEventListener(“touch”, blocker)
    
    --Popup box image
    local popupBox = display.newRect( 300, 300, 424, 168 )
    
    --When the user clicks “OK” save the new data, set their current level at 1
    --and clean up the popup group before changing to the level select screen
    local function submitPopup(event)
        --Modified to prevent names over 15 characters in length
        if event.phase == “editing” and string.len(nameBox.text) > 15 then
            nameBox.text = nameBox.text:sub(1,15)
        end
        if event.phase == “submitted” then
            saveValue(“file” …ID… “.data”, nameBox.text)
            _G.username = nameBox.text
            saveValue(""…_G.username…".data", 1)
            saveValue(""…_G.username…“ID.data”, ID)
            _G.currentLevel = 1
            popupGroup:removeSelf()
            popupGroup = nil
            native.setKeyboardFocus(nil)
            director:changeScene(“levels”)
        end
    end
    
    --Text field for user to enter their name
    --submitPopup is listened for when the user presses enter
    nameBox = native.newTextField(320, 320, 384, 60, submitPopup)
    nameBox.size = 24
    
    
    --Place all objects in the group so they can be easily and effectively cleaned up
    popupGroup:insert(safeMask)
    popupGroup:insert(popupBox)
    popupGroup:insert(nameBox)

end

–BUTTON FUNCTIONALITY (FOR FILE BUTTONS)
local function buttonHandler (event)
    if event.phase == “release” then
        --If empty call popup we coded above to create new file
        if status[event.id] == “Name” then
            ID = event.id
            newFile()
        else
            --If not empty load the data and set the username and current level
            --for later use
            _G.username = files[event.id]
            _G.currentLevel = loadValue(""…_G.username…".data")
            director:changeScene(“levels”)
        end
    end
end

–BUTTONS FOR FILES
–[[
Each button (there are currently 5) are set up the exact same way. We’re currently using
the UI library for these buttons.
Default = normal image shown
Over = image shown when touch is down on the button
id = the file number
text = grab the status/name of the button and set it as text
textColor = color of text in r, g, b
size = font size
emboss = embossed true or false, true looks nicer normally
x = x coordinate for button
y = y coordinate for button
–]]
local button1 = ui.newButton{
    default = “buttonYellow.png”,
    over = “buttonYellowOver.png”,
    id = 1,
    onEvent = buttonHandler,
    text = status[1],
    textColor = {51, 51, 51, 255},
    size = 22,
    emboss = true,
    x = 110,
    y = 360
}

local button2 = ui.newButton{
    default = “buttonYellow.png”,
    over = “buttonYellowOver.png”,
    id = 2,
    onEvent = buttonHandler,
    text = status[2],
    textColor = {51, 51, 51, 255},
    size = 22,
    emboss = true,
    x = 310,
    y = 360
}
 

— etc…button fields go up to ten.

I tried to edit using Rob’s link for native.newTextField for both “Standard” and “Numeric”, but that didn’t fix it. Not sure what you mean Rob by deleting the call listener and creating a second one. Sorry for being so dense, I’m still a newbie as coding goes. If you can please take a look and suggest what section area I need to work on, I’ll keep plugging away at it. Thanks!

John (-:slight_smile:

Rob/Brent: I forgot to ask, should I upgrade to Yosemite and X-Code 6.1? If I do, what version of Corona Simulator should I be using to build binaries for iTunes submission? Thanks!

John (-:slight_smile:

nameBox = native.newTextField(320, 320, 384, 60)

nameBox.size = 24

numericField:addEventListener( “userInput”, submitPopup )

Rob

Since you are a Pro subscriber and have access to Daily builds, if you upgrade to Yosemite and Xcode 6.1 you should be using the latest daily build.  If you feel you need to stay with the latest public build, I would not upgrade to Yosemite yet.

Rob

Rob: I edited using the “Standard” with changes you suggested…almost working, but not sure on line 187 (in red) should be “newTextField”. Also tried just “textField” but that didn’t work. Appreciate your help on this. Thanks! ~ John

  
    --Text field for user to enter their name
    --submitPopup is listened for when the user presses enter
    nameBox = native.newTextField(320, 320, 384, 60)
    nameBox.size = 24
    textField:addEventListener( “userInput”, submitPopup )
    
    
    --Place all objects in the group so they can be easily and effectively cleaned up
    popupGroup:insert(safeMask)
    popupGroup:insert(popupBox)
    popupGroup:insert(nameBox)

end

–BUTTON FUNCTIONALITY (FOR FILE BUTTONS)
local function buttonHandler (event)
    if event.phase == “release” then
        --If empty call popup we coded above to create new file
        if status[event.id] == “Name” then
            ID = event.id
            newTextField()
        else
            --If not empty load the data and set the username and current level
            --for later use
            _G.username = files[event.id]
            _G.currentLevel = loadValue(""…_G.username…".data")
            director:changeScene(“levels”)
        end
    end
end

local defaultField

local function textListener( event )

    if ( event.phase == “began” ) then
    
        --user begins editing text field
        print( event.text )
        
        elseif ( event.phase == “ended” or event.phase == “submitted” ) then
        
        --text field loses focus
        --do something with defaultField’s text
        print( "Submitted text: " … event.target.text )
        
        elseif ( event.phase == “editing” ) then
        
            print( event.newCharacters )
            print( event.oldText )
            print( event.startPosition )
            print( event.text )
    end        
end
 

What you have there (the red code) would call a function you wrote called “newTextField”.  I don’t know if you have a function name that.  I also don’t know why you’re trying to create a new text field in the middle of a button handler.  I don’t know what you’re trying to accomplish there.  If you want to create a new text field there, then you would need to call native.newTextField(), pass it all the parameters, setup everything, etc.

Rob

Hi John,

I’m not clear on what object is “nameBox” in the screenshot. It appears to be a button, but then in your code, it’s a native text field. Is the text field actually that white box above the yellow button? You circled the yellow button, so it’s confusing.

As for the error, it appears to be a simple up-value syntax error. Where did you initially declare “nameBox”? Or are you trying to make it global? If so, globals should be avoided whenever possible.

Finally, I should mention that native objects can not be inserted into display groups, as you’re doing on line 174. Native objects are separate from the OpenGL “canvas” and thus you must treat them slightly differently.

Best regards,

Brent

This came up the other day.  The format for creating a native.newTextField() where you include the call back listener in the create call isn’t working.  You need to leave that off and set the listener via a second call.  See http://docs.coronalabs.com/api/library/native/newTextField.html

Rob

Hi Brent and Rob: Thanks for the prompt feedback. Brent, sorry about the white box on the image, my mistake. What I meant to say was that if the user tapped the yellow “Name” button, it would trigger a popup text field box to enter a childs name. Peach Pellen helped me with that code a few years back, so it’s probably outdated! Here is what it looks like:

–NEW FILE FUNCTION
–This function is called when a user clicks on an empty file
local function newFile ()
    --Creates a display group for popup images to be stored in for easy management
    local popupGroup = display.newGroup()
    
    --Mask the background with a semi transparent “layer”
    local safeMask = display.newRect( 0, 0, 1024, 768 )
    safeMask:setFillColor(0,0,0,180)
    
    --Prevent event propogation (users can’t click buttons if popup is open)
    local function blocker ()
        return true
    end
    --We assign touch and tap listeners so neither touches or taps can propogate
    safeMask:addEventListener(“tap”, blocker)
    safeMask:addEventListener(“touch”, blocker)
    
    --Popup box image
    local popupBox = display.newRect( 300, 300, 424, 168 )
    
    --When the user clicks “OK” save the new data, set their current level at 1
    --and clean up the popup group before changing to the level select screen
    local function submitPopup(event)
        --Modified to prevent names over 15 characters in length
        if event.phase == “editing” and string.len(nameBox.text) > 15 then
            nameBox.text = nameBox.text:sub(1,15)
        end
        if event.phase == “submitted” then
            saveValue(“file” …ID… “.data”, nameBox.text)
            _G.username = nameBox.text
            saveValue(""…_G.username…".data", 1)
            saveValue(""…_G.username…“ID.data”, ID)
            _G.currentLevel = 1
            popupGroup:removeSelf()
            popupGroup = nil
            native.setKeyboardFocus(nil)
            director:changeScene(“levels”)
        end
    end
    
    --Text field for user to enter their name
    --submitPopup is listened for when the user presses enter
    nameBox = native.newTextField(320, 320, 384, 60, submitPopup)
    nameBox.size = 24
    
    
    --Place all objects in the group so they can be easily and effectively cleaned up
    popupGroup:insert(safeMask)
    popupGroup:insert(popupBox)
    popupGroup:insert(nameBox)

end

–BUTTON FUNCTIONALITY (FOR FILE BUTTONS)
local function buttonHandler (event)
    if event.phase == “release” then
        --If empty call popup we coded above to create new file
        if status[event.id] == “Name” then
            ID = event.id
            newFile()
        else
            --If not empty load the data and set the username and current level
            --for later use
            _G.username = files[event.id]
            _G.currentLevel = loadValue(""…_G.username…".data")
            director:changeScene(“levels”)
        end
    end
end

–BUTTONS FOR FILES
–[[
Each button (there are currently 5) are set up the exact same way. We’re currently using
the UI library for these buttons.
Default = normal image shown
Over = image shown when touch is down on the button
id = the file number
text = grab the status/name of the button and set it as text
textColor = color of text in r, g, b
size = font size
emboss = embossed true or false, true looks nicer normally
x = x coordinate for button
y = y coordinate for button
–]]
local button1 = ui.newButton{
    default = “buttonYellow.png”,
    over = “buttonYellowOver.png”,
    id = 1,
    onEvent = buttonHandler,
    text = status[1],
    textColor = {51, 51, 51, 255},
    size = 22,
    emboss = true,
    x = 110,
    y = 360
}

local button2 = ui.newButton{
    default = “buttonYellow.png”,
    over = “buttonYellowOver.png”,
    id = 2,
    onEvent = buttonHandler,
    text = status[2],
    textColor = {51, 51, 51, 255},
    size = 22,
    emboss = true,
    x = 310,
    y = 360
}
 

— etc…button fields go up to ten.

I tried to edit using Rob’s link for native.newTextField for both “Standard” and “Numeric”, but that didn’t fix it. Not sure what you mean Rob by deleting the call listener and creating a second one. Sorry for being so dense, I’m still a newbie as coding goes. If you can please take a look and suggest what section area I need to work on, I’ll keep plugging away at it. Thanks!

John (-:slight_smile:

Rob/Brent: I forgot to ask, should I upgrade to Yosemite and X-Code 6.1? If I do, what version of Corona Simulator should I be using to build binaries for iTunes submission? Thanks!

John (-:slight_smile:

nameBox = native.newTextField(320, 320, 384, 60)

nameBox.size = 24

numericField:addEventListener( “userInput”, submitPopup )

Rob

Since you are a Pro subscriber and have access to Daily builds, if you upgrade to Yosemite and Xcode 6.1 you should be using the latest daily build.  If you feel you need to stay with the latest public build, I would not upgrade to Yosemite yet.

Rob

Rob: I edited using the “Standard” with changes you suggested…almost working, but not sure on line 187 (in red) should be “newTextField”. Also tried just “textField” but that didn’t work. Appreciate your help on this. Thanks! ~ John

  
    --Text field for user to enter their name
    --submitPopup is listened for when the user presses enter
    nameBox = native.newTextField(320, 320, 384, 60)
    nameBox.size = 24
    textField:addEventListener( “userInput”, submitPopup )
    
    
    --Place all objects in the group so they can be easily and effectively cleaned up
    popupGroup:insert(safeMask)
    popupGroup:insert(popupBox)
    popupGroup:insert(nameBox)

end

–BUTTON FUNCTIONALITY (FOR FILE BUTTONS)
local function buttonHandler (event)
    if event.phase == “release” then
        --If empty call popup we coded above to create new file
        if status[event.id] == “Name” then
            ID = event.id
            newTextField()
        else
            --If not empty load the data and set the username and current level
            --for later use
            _G.username = files[event.id]
            _G.currentLevel = loadValue(""…_G.username…".data")
            director:changeScene(“levels”)
        end
    end
end

local defaultField

local function textListener( event )

    if ( event.phase == “began” ) then
    
        --user begins editing text field
        print( event.text )
        
        elseif ( event.phase == “ended” or event.phase == “submitted” ) then
        
        --text field loses focus
        --do something with defaultField’s text
        print( "Submitted text: " … event.target.text )
        
        elseif ( event.phase == “editing” ) then
        
            print( event.newCharacters )
            print( event.oldText )
            print( event.startPosition )
            print( event.text )
    end        
end
 

What you have there (the red code) would call a function you wrote called “newTextField”.  I don’t know if you have a function name that.  I also don’t know why you’re trying to create a new text field in the middle of a button handler.  I don’t know what you’re trying to accomplish there.  If you want to create a new text field there, then you would need to call native.newTextField(), pass it all the parameters, setup everything, etc.

Rob