Okay I have moved the subtraction piece around to several different places. I’m not getting an error now but it is still not working. Basically my screen comes up and I can enter numbers in the first two fields and dismiss the keyboard and nothing happens. How should the box be where I want the number to appear? Should it be a textfield also? I don’t want a person to enter anything on fieldNumberFour. I just want the answer to appear there. Lastly if I don’t have a text field, how do I identify the font and color when the answer does come up? Please take a look at my code and let me know what’s wrong.
[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 physics = require (“physics”)
physics.start ()
local ui = require(“ui”)
local tHeight – forward reference
local background = display.newImage (“blackbackground.png”)
background.x = 240
localGroup:insert(background)
–> This sets the background
local screen6 = display.newImage (“screen6.png”)
screen6.x = 240
localGroup:insert(screen6)
–> This sets Screen6
– General event handler for fields
– You could also assign different handlers for each textfield
local function fieldHandler( event )
if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
– In some cases you may want to adjust the interface when the keyboard appears.
elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard
– Hide keyboard
native.setKeyboardFocus( nil )
end
end
– Predefine local objects for use later
local numberField, numberFieldTwo, numberFieldThree, numberFieldFour, difference, textFunction
local fields = display.newGroup()
– Create a Background touch event
– Tapping screen dismisses the keyboard
– Needed for the Number textField since there is
– no return key to clear focus.
local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end
– Determine if running on Corona Simulator
local isSimulator = “simulator” == system.getInfo(“environment”)
– Native Text Fields not supported on Simulator
if isSimulator then
msg = display.newText( “Native Text Fields not supported on Simulator!”, 0, 280, “Verdana-Bold”, 12 )
msg.x = display.contentWidth/2 – center title
msg:setTextColor( 255,255,0 )
end
– Add listener to background for user “tap”
screen6:addEventListener( “tap”, listener )
local house = display.newImage (“house.png”)
house.x = 45
house.y = 35
house.xScale = .5
house.yScale = .5
localGroup:insert(house)
–>This places the House
local function touchedHouse (event)
if (“ended” == event.phase) then
director:changeScene (“titlescreen”)
media.playEventSound( “click_x.caf” )
end
end
house:addEventListener (“touch”, touchedHouse)
– Handlers for login dialog UI
local function onnumberFieldTwo( event )
if ( “began” == event.phase ) then
– Note: this is the “keyboard appearing” event
– In some cases you may want to adjust the interface while the keyboard is open.
elseif ( “ended” == event.phase ) then
– Automatically tab to numberFieldThree if user taps screen (convenient!)
native.setKeyboardFocus( numberFieldThree )
numberFieldTwo:addEventListener(“onChange”, textFunction)
end
end
local function onnumberFieldThree( event )
– Hide keyboard when the user taps the screen
if ( “ended” == event.phase ) then
native.setKeyboardFocus( nil )
end
end
local function onnumberFieldFour( event )
– Hide keyboard when the user taps the screen
if ( “ended” == event.phase ) then
native.setKeyboardFocus( nil )
local function textFunction()
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
end
end
local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 425
smallbrain.y = 275
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain
local function touchedSmallbrain (event)
if (“ended” == event.phase) then
director:changeScene (“screen7a”)
media.playEventSound( “click_x.caf” )
end
end
smallbrain:addEventListener (“touch”, touchedSmallbrain)
local backbutton = display.newImage (“backbutton.png”)
backbutton.x = 60
backbutton.y = 280
backbutton.xScale = .4
backbutton.yScale = .4
localGroup:insert(backbutton)
local function touchedBackbutton (event)
if (“ended” == event.phase) then
director:changeScene (“screen5”)
media.playEventSound( “click_x.caf” )
end
end
backbutton:addEventListener (“touch”, touchedBackbutton)
–>This places the backbutton
– *** Create native input textfields ***
– Note: currently this feature works in device builds or Xcode simulator builds only
– Note: currently this feature works in device builds only
local isAndroid = “Android” == system.getInfo(“platformName”)
local inputFontSize = 30
local inputFontHeight = 30
tHeight = 50
if isAndroid then
– Android text fields have more chrome. It’s either make them bigger, or make the font smaller.
– We’ll do both
inputFontSize = 30
inputFontHeight = 30
tHeight = 50
end
numberFieldTwo = native.newTextField( 180, 125, 120, tHeight, fieldHandler )
numberFieldTwo.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldTwo.inputType = “number”
numberFieldThree = native.newTextField( 180, 180, 120, tHeight, fieldHandler )
numberFieldThree.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldThree.inputType = “number”
numberFieldFour = native.newTextField( 180, 245, 120, tHeight, fieldHandler )
numberFieldFour.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldFour.inputType = “number”
– Add fields to our new group
fields:insert(numberFieldTwo)
fields:insert(numberFieldThree)
fields:insert(numberFieldFour)
–>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: 13448 reply_id: 49725[/import]