(Solved)Subtracting Numbers

I am producing a game where on one screen I will have a subtraction problem. So basically I have the screen designed where I have a rectangle to enter the first number and below that a rectangle to enter the second number. Lastly I have a line drawn beneath that and the last box that will display the answer.

I am very new to Corona and not sure what type of code to use to subtract these two numbers. Also I need to know how to make the box active where you can first type the numbers using the keyboard on the I-phone.
[import]uid: 72372 topic_id: 13109 reply_id: 313109[/import]

newnumber = number1 - number2

Dealing with the keyboard is going to be your problem. You either need to create your own or use the native.newTextField() API call.

While the later is hyper easy to do, your design will involve a one line text entry box which will clobber your design.

What may be more practical (and more work) would be to take your first number block, and add a touch event handler to it. When that tap happens, a numeric keypad pops up, the player taps in their numbers and taps the done button, and you will have to handle building your own numbers manage the backspace button etc.

To build that keyboard, you would need to create a new group, insert a 3x4 grid of squares which you would make buttons by setting up event handlers for each button.

This method isn’t an easy feat for someone new to programming.

If you go the native testfield route, your code would be something like this:

[lua]local numberOne
local numberTwo
local difference
local numberOneField
local numberTwoField
local numberOneText = display.newText("", display.contentWidth/2, 10, native.SystemFont,20)

local numberTwoText = display.newText("", display.contentWidth/2, 50, native.SystemFont,20)

local function enterNumberOne(event)
if event.phase == “ended” or event.phase == “submitted” then
numberOne = tonumber(numberOneField.text)
numberOneText.text = string.format("%d", numberOne)
native.setKeyboardFocus( numberTwoField ) – tab to the next field
end
return true;
end

local function enterNumberTwo(event)
if event.phase == “ended” or event.phase == “submitted” then
numberTwo = tonumber(numberTwoField.text)
native.setKeyboardFocus( nil ) – dismiss the window
numberTwoText.text = string.format("%d", numberTwo)
difference = numberOne - numberTwo
differenceText.text = string.format("%d", difference)
end
return true;
end
numberOneField = native.newTextField(50, display.contentWidth/2, display.content -100, 20, enterNumberOne)
numberOneField.inputType = “number”

numberTwoField = native.newTextField(50, display.contentWidth/2, display.content - 100, 70, enterNumberTwo)
numberTwoField.inputType = “number”[/lua]

Now I just made all that up. I’ve not tried to make it work, but keep in mind that using the native.newTextField() WILL NOT work in the Coronoa SKD simulator, you will have to build for device to make it work.

[import]uid: 19626 topic_id: 13109 reply_id: 48149[/import]

robmiracle,

Thank you! I will try this code as soon as I get home and let you know what happens. One question I still have though is where do I call out the png files I have for the boxes? I have three boxes box1.png , box2.png and box3.png. Two of these boxes you will enter your numbers and box 3 will show the answer.

Michelle [import]uid: 72372 topic_id: 13109 reply_id: 48193[/import]

Rob,

I tried some simple stuff at first. I have my three boxes showing but not sure what to actually do next to make them do the subtraction. I have made all three boxes text fields but I’m thinking maybe only two need to be because the 3rd box will be the output. I have loaded it on my device and the boxes show up fine. The two things I need are as follows:

  1. They don’t subtract

  2. When I go to the next screens the 3 boxes still show up.

[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
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 onnumberTwo( 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 ( “submitted” == event.phase ) then
– Automatically tab to password field if user clicks “Return” on iPhone keyboard (convenient!)
native.setKeyboardFocus( passwordField )
end
end

local function onnumberThree( event )
– Hide keyboard when the user clicks “Return” in this field
if ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end

local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 428
smallbrain.y = 280
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

numberField = native.newTextField( 180, 125, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “numberTwo”

numberField = native.newTextField( 180, 180, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “numberThree”

numberField = native.newTextField( 180, 245, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “numberFour”

– Add fields to our new group

fields:insert(numberField)


–>MUST return a display.newGroup()


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

return localGroup

end[/lua] [import]uid: 72372 topic_id: 13109 reply_id: 49358[/import]

Here are a few questions for thought?

What is supposed to happen when a number is entered? They call fieldHandler which all it does is dismiss the keyboard. What should go on there?

What is the 3rd input field for? You just need to input 2 numbers then “display” the results.

Where/When do you tell the app to perform the subtraction? Do you want it to automatically show the result after each number is input, or do you want a button that is pressed to generate the result?

what calls onnumberTwo and onnumberThree events?

Think through those questions and lets see where it gets you. [import]uid: 19626 topic_id: 13109 reply_id: 49478[/import]

Robmiracle

Thank you for your response. I have been playing around with the code but still have some holes. I answered some of your questions below.
What is supposed to happen when a number is entered? They call fieldHandler which all it does is dismiss the keyboard. What should go on there?

I want the person to enter a number in the numberFieldTwo and numberFieldThree then it automatically show the result in numberFieldFour or a space where numberFieldFour is located. I guess I just need regular box there?

What is the 3rd input field for? You just need to input 2 numbers then “display” the results.
Yes you are correct. I don’t need a box there, I just need to display the results.

Where/When do you tell the app to perform the subtraction? Do you want it to automatically show the result after each number is input, or do you want a button that is pressed to generate the result?

I want it to automatically show the results in the box located where numberFieldFour is at right now.

what calls onnumberTwo and onnumberThree events?
I don’t understand this question.

Current Code

[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
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 onnumberTwo( 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 )
end
end

local function onnumberThree( event )
– Hide keyboard when the user taps the screen
if ( “ended” == event.phase ) then
native.setKeyboardFocus( nil )
end
end

local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 428
smallbrain.y = 280
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: 13109 reply_id: 49482[/import]