I have extensive mainframe coding skills but am new to Lua and Corona (and the whole non-mainframe environments). I have searched and tried extensively on how to get passed my issue.
I have an option to allow the user to create a new game. If the user pushes the New Game button from Create.Lua then it executed the “buildgame.lua” code below. It then prompts the user to enter a name. This works fine. But then I execute a external Lua program called “buildhome.lua”. This has complicated logic that dynamically creates a universe using many random functions to create a unique universe that is different each time. This logic works fine. It randomly positioning stars and planets, on a huge galaxy map. However, once I execute this external lua code and return back to “buildgame.lua” it doesn’t know what to do. The scenes no longer work and I am stuck in the TextListener function. I have tried to remove the newTextField and this doesn’t work. I final decided to just jump to a new lua program by forcing the composer.remove and composer.gotoScene thinking it would get my scenes working again. But I still have junk left over from my “buildhome” scenes. Even the newTextField is still there.
What am I doing wrong? Below the code is the output from corona to show the process it takes.
Below is my code:
[lua]
–
– buildgame.lua
local widget = require( “widget” ) --Require Widget library
local composer = require( “composer” ) --Require Composer
local myData = require( “scripts.mydata” ) --Require Global Saves
local scene = composer.newScene()
local function textListener(event)
if (event.phase == “began”) then
print (“Begin editing”, event.target.text)
elseif (event.phase == “ended” or event.phase == “submitted”) then
native.setKeyboardFocus(nil)
myData.newgame = string.sub(event.target.text, 1, 15)
if namedesc ~= nil then
namedesc:removeSelf()
namedesc = nil
end
if usernameField ~= nil then
usernameField:removeSelf()
usernameField = nil
end
if usernameGroup ~= nil then
usernameGroup:removeSelf()
usernameGroup = nil
end
errmsg = display.newText( “Building Star Map”, 625, 1900, “fonts/nasalization free”, 55)
errmsg.anchorX = 0
errmsg:setFillColor( 145/255, 231/255, 255/255 )
local buildhome = require (“scripts.buildhome”)
composer.removeScene( “buildgame” )
composer.gotoScene( “scripts.showmap”, “fade”, 800 )
elseif (event.phase == “editing”) then
print (event.phase)
local valid_letter = false
if ((event.newCharacters >= “a”) and (event.newCharacters <= “z”)) or
((event.newCharacters >= “A”) and (event.newCharacters <= “Z”)) then
valid_letter = true
else
errval = display.newText( “Only letters a-z are allowed - please remove”, 225, 1400, “fonts/nasalization free”, 85)
errval.anchorX = 0
errval:setFillColor( 145/255, 231/255, 255/255 )
end
local txt = event.text
if(string.len(txt)>15)then
txt=string.sub(txt, 1, 15)
print("txt: ", txt)
errsiz = display.newText( “Name can be 1 to 15 characters - please shorten”, 325, 1200, “fonts/nasalization free”, 85)
errsiz.anchorX = 0
errsiz:setFillColor( 145/255, 231/255, 255/255 )
else
myData.text=txt
end
end
end
function onEventListener( event ) --When user taps button this is called
print( “Going to showmap” )
composer.removeScene( “buildgame” )
composer.gotoScene( “scripts.buildgame”, “fade”, 800 ) --Fade and go to Load
end
function scene:create( event ) --Function to create the scene
print( “buildgame created” )
local sceneGroup = self.view
local background = display.newImageRect( “images/starfield.jpg”, 2732, 2048 ) --2732, 2048
background.x = display.contentCenterX
background.y = display.contentCenterY
local nebula = display.newImageRect( “images/Nebula1.png”, 2732, 2048 ) --2732, 2048
nebula.x = display.contentCenterX
nebula.y = display.contentCenterY
sceneGroup:insert( background )
sceneGroup:insert( nebula)
local usernameGroup = self.view
local namedesc = display.newText( "User Name: ", 800, 1000, “fonts/nasalization free”, 55)
namedesc.anchorX = 0
namedesc:setFillColor( 145/255, 231/255, 255/255 )
local usernameField = native.newTextField( 1400, 1000, 500, 85 )
usernameField.font = native.newFont( native.systemFontBold, 55 )
usernameField.text = “”
usernameField:setTextColor( 0.0, 0.0, 0.0 )
usernameField:addEventListener( “userInput”, textListener )
usernameGroup:insert( namedesc )
usernameGroup:insert( usernameField )
end
function scene:show( event )
local phase = event.phase
if phase == “will” then
print(“buildgame will show.”)
display.setDefault( “background”, 0.0, 0.0, 0.0 )
elseif phase == “did” then
print(“buildgame did show.”)
end
end
function scene:hide( event )
local phase = event.phase
if phase == “will” then
print(“buildgame will hide.”)
elseif phase == “did” then
print(“buildgame did hide.”)
end
end
function scene:destroy( event )
print( “Destroying buildgame’s view!” )
end
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
[/lua]
Corona Output:
New Game
Destroying create’s view!
create will hide.
buildgame created
buildgame will show.
Begin editing
create did hide.
buildgame did show.
Destroying create’s view!
editing (typed J)
editing (typed o)
editing (typed h)
editing (typed n)
buildhome myData.newgame: John (name of new game to be created)
buildgame will hide.
showmap created
showmap will hide.
showmap did hide.
showmap will show.
showmap will show.
showmap did show.
buildgame did hide.
showmap did show.
Destroying buildgame’s view!