I apologize upfront for the long code. I am simply clueless as to how to handle the issue.
First, the code compiles for the xcode simulator and works. However, I am getting an error in the Corona Simulator.
The only way I can figure to get rid of all the scene objects and still have everything work, is to move my enterButton to the Show() function and use a forward reference.
Here is my error (again it doesn’t interfere with the code working:
capture.lua:147: attempt to index upvalue ‘enterButton’ (a nil value)
message
stack traceback:
?: in function <?:218>
Here is the code:
local composer = require( “composer” )
[lua]-- Forward References
local username
local enterButton
local gotoMain
– create and open SQL Database
local path = system.pathForFile(“data.db”, system.DocumentsDirectory)
local db = sqlite3.open(path)
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY autoincrement, capture);]]
db:exec( tablesetup )
– Don’t forget to close the DataBase at some point. Not in the Capture Module though.
local widget = require(“widget”)
local scene = composer.newScene()
function scene:create( event )
local sceneGroup = self.view
local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
background:setFillColor(255,255,255)
local capturetitle = display.newText(“Capture Page”, 0, 40, native.systemFont, 80)
capturetitle:setFillColor(0,0,0)
capturetitle.x = _W * 0.5
capturetitle.y = 80
gotoMain = display.newText(“Return to Main Page”, 0, 40, native.systemFont, 80)
gotoMain:setFillColor(0,0,0)
gotoMain.x = _W * 0.5
gotoMain.y = _H - 80
function gotoMain:touch(e)
if(e.phase == “began”) then
--“scene:hide()”
--print(“Began under gotoMain”)
composer.gotoScene( “scenet” )
end
return true
end
background:addEventListener(“tap”, background)
– Return to Main menu text button
gotoMain:addEventListener(“touch”, gotoMain)
– Hide keyboard upon tapping the background
function background:tap(e)
native.setKeyboardFocus(nil)
end
--sceneGroup:insert( )
sceneGroup:insert( background )
sceneGroup:insert( capturetitle )
--sceneGroup:insert( enterButton )
sceneGroup:insert( gotoMain )
--sceneGroup:insert( username )
end
– “scene:show()”
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == “did” ) then
– Called when the scene is now on screen.
– Insert code here to make the scene come alive.
– Example: start timers, begin animation, play audio, etc.
– Create, Position and instantiate Input Textfield
username = native.newTextField(0,0, _W * 0.8, 100)
local font = “HelveticaNeue” or system.nativeFont
local hintText = “Enter an item to capture”
username.inputType = “default”
username.font = native.newFont(font,24)
username.text = hintText
username.align = “center”
username.x = _W * 0.5
username.y = _H * 0.2
enterButton = widget.newButton{
id = “enter button”,
onRelease = buttonTapped,
defaultFile = “images/buttontest.png”,
overFile = “images/buttontest_touch.png”}
enterButton.x = _W * 0.5
enterButton.y = _H * 0.4
– Input Textfield function
function username:userInput(e)
if(e.phase == “began”) then
e.target.text = “”
--print("Began " … e.target.text)
elseif(e.phase == “editing”) then
--print("Editing " … e.target.text)
elseif(e.phase == “ended”) then – ended happens when the enterButton is touched
--print("Ended " … e.target.text)
local text = username.text
print(“tapped Enter Button :” … text)
local sql = [[INSERT INTO test VALUES(NULL, ‘]] … text … [[’);]]
db:exec(sql)
--print(“tapped Enter Button :” … username.text)
e.target.text = “You may enter another Event”
elseif(e.phase == “submitted”) then
print("Submitted " … e.target.text)
local sql = [[INSERT INTO test VALUES(NULL, ‘]] … username.text … [[’);]]
db:exec(sql)
e.target.text = “” – This is empty because we don’t re-enter the began phase to clear the textfield
end
end
username:addEventListener(“userInput”, username)
– listener for textfield
enterButton:addEventListener(“tap”, enterButton)
– blur the keyboard on background touch
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Called when the scene is on screen (but is about to go off screen).
– Insert code here to “pause” the scene.
– Example: stop timers, stop animation, stop audio, etc.
enterButton:removeSelf()
username:removeSelf()
composer.removeScene( “capture”, false )
elseif ( phase == “did” ) then
– Called immediately after scene goes off screen.
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene[/lua]