Native Alert

I have a condition that I’m checking for and what I would like to do is bring up an alert if the condition is reached. It will basically tell the player they have did something incorrect. I see the native alert instructions that they have on the site but I’m not sure how to use those in conjunction with a keyboard dismissal.

So basically what I want is when a player is finish entering the number in numberFieldEight they tap the screen to dismiss the keyboard. I then want my coding to run to check to see if the player did the entry correct and if not the alert comes up. I have my coding below but not sure how to tie the alert to the coding and where.

[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 ui = require(“ui”)
– Predefine local objects for use later
local numberFieldSeven, numberFieldEight
local fields = display.newGroup()
local inputFontSize = 30
local inputFontHeight = 30
local tHeight = 50

local screen5 = display.newImage (“screen5.png”)
localGroup:insert(screen5)
–> This sets Screen5

local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 270
smallbrain.y = 430
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain

local function touchedSmallbrain (event)
if (“ended” == event.phase) then
_G.numberEight = numberFieldEight.text
director:changeScene (“screen6”)
media.playEventSound( “click_x.caf” )

end
end

smallbrain:addEventListener (“touch”, touchedSmallbrain)
– Tapping screen dismisses the keyboard
local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end
Runtime:addEventListener( “tap”, listener )
—>code to determine if numbers match
local function sortnum(number)
local numbersin = {}
local reminder
local returnnum = 0
if number ~=nil then
while number ~=0 do
reminder = number % 10
number = (number - (number % 10)) /10
table.insert(numbersin,reminder)
end
local mult = 1
table.sort(numbersin)

for i=1,#numbersin,1 do
returnnum = returnnum + (numbersin[i] * mult )
mult = mult * 10
end
end
return returnnum
end

local number1 = 2455
local number2 = 5245
if sortnum(number1) == sortnum(number2) then
print(“same”)
else
print(“not same”)
end
—> end of number match code

numberFieldSeven = native.newTextField( 50, 80, 85, tHeight, onnumberFieldSeven )
numberFieldSeven.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldSeven.inputType = “number”
numberFieldSeven.align = “center”
numberFieldSeven.text = _G.numberOne

numberFieldEight = native.newTextField( 180, 80, 85, tHeight, onnumberFieldEight )
numberFieldEight.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldEight.align = “center”
numberFieldEight.inputType = “number”
– Add fields to our new group

localGroup:insert(numberFieldSeven)
localGroup:insert(numberFieldEight)
–>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: 14056 reply_id: 314056[/import]

your listener functions do not exist?

but in them, just place the function you want called above them, and then call them: e.g

[lua]local z = function(x)
print(‘well done’)
end

local onFieldNumberSeven = function()
z(x)
end[/lua]
[import]uid: 24641 topic_id: 14056 reply_id: 51794[/import]

Which listener for the keyboard tap? I have that shown above which is what I want to activate the alert. Is that what you mean? Or do I have to have separate listeners. I’m sorry if this is a simple answer but I am very new to this and just trying to come up to speed. With what you have shown do I need to add something like below and then put yours there? That’s what was confusing me when I looked at the tutorials and I don’t know if below is correct.
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Return to gameboard
end
end
end

– Show alert with one button
local alert = native.showAlert(“UH-OH”,“You entered a different digit than your original number. You can only use digits from your original number to rearrange.”,{“Try Again”}) [import]uid: 72372 topic_id: 14056 reply_id: 51805[/import]

here’s an example:

[lua]
local function sortnum(x)

– if certain condition is reached, do a certain thing

else

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Return to gameboard
end
end
end

local alert = native.showAlert(“Error!”,“Your numbers do not tally!”, { " Ok " }, onComplete )

end
local function fieldHandler( event )

if ( “began” == event.phase ) then

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
native.setKeyboardFocus( nil )
sortnum(100)
end

end

defaultField = native.newTextField( 40, 155, 240, 35, fieldHandler )
localGroup:insert(defaultField)[/lua]

does that help at all? [import]uid: 24641 topic_id: 14056 reply_id: 51806[/import]

Yes it absolutely does! Thank you! [import]uid: 72372 topic_id: 14056 reply_id: 51808[/import]

do remove
local number1 = 2455
local number2 = 5245

from your code…
it was just added for testing… :slight_smile: [import]uid: 71210 topic_id: 14056 reply_id: 51829[/import]

Okay, I’m still trying to get the alert to work if my numbers from one block don’t match the numbers of the next block. For instance if you have 500 in one block you can have 500, or 005 or 050 something like that in the next box but you couldn’t have a number that didn’t have those digits like 605. If you entered 605 the native alert should show after you dismissed the keyboard. This is the code I was trying but it didn’t work. What’s wrong with the 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 ui = require(“ui”)
– Predefine local objects for use later
local numberFieldSeven, numberFieldEight
local fields = display.newGroup()
local inputFontSize = 30
local inputFontHeight = 30
local tHeight = 50
local screen5 = display.newImage (“screen5.png”)
localGroup:insert(screen5)
–> This sets Screen5

local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 270
smallbrain.y = 430
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain

local function touchedSmallbrain (event)
if (“ended” == event.phase) then
_G.numberEight = numberFieldEight.text
director:changeScene (“screen6”)
media.playEventSound( “click_x.caf” )

end
end

smallbrain:addEventListener (“touch”, touchedSmallbrain)

– Tapping screen dismisses the keyboard
local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end
Runtime:addEventListener( “tap”, listener )

– This cleanUp function will remove all listeners from the scene since
– Director can’t remove listeners itself

local cleanUp = function()
Runtime:removeEventListener(“tap”, listener)
smallbrain:removeEventListener(“touch”, touchedSmallbrain)
end
– Determine if running on Corona Simulator – Native Text Fields not supported on Simulator
local isSimulator = “simulator” == system.getInfo(“environment”)
if isSimulator then
msg = display.newText( “Native Text Fields not supported on Simulator!”, 0, 280, nil, 12 )
msg.x = display.contentWidth/2 – center title
msg:setTextColor( 255,255,0 )
end

—>code to determine if numbers match
local function sortnum(number)
local numbersin = {}
local reminder
local returnnum = 0
if number ~=nil then
while number ~=0 do
reminder = number % 10
number = (number - (number % 10)) /10
table.insert(numbersin,reminder)
end
local mult = 1
table.sort(numbersin)

for i=1,#numbersin,1 do
returnnum = returnnum + (numbersin[i] * mult )
mult = mult * 10
end
end
return returnnum
end
–>end of number match detection

–>this starts the native alert
if sortnum(number1) == sortnum(number2) then
print(“same”)
else
local alert = native.showAlert(“Error!”,“Your numbers do not tally!”, { " Ok " }, onComplete )
end

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Return to gameboard
end
end
end
—>This ends the native alert

numberFieldSeven = native.newTextField( 50, 80, 85, tHeight, onnumberFieldSeven )
numberFieldSeven.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldSeven.inputType = “number”
numberFieldSeven.align = “center”
numberFieldSeven.text = _G.numberOne

numberFieldEight = native.newTextField( 180, 80, 85, tHeight, onnumberFieldEight )
numberFieldEight.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldEight.align = “center”
numberFieldEight.inputType = “number”
– Add fields to our new group

localGroup:insert(numberFieldSeven)
localGroup:insert(numberFieldEight)
–>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: 14056 reply_id: 51869[/import]

your oncomplete function needs to be above the native alert [import]uid: 24641 topic_id: 14056 reply_id: 51870[/import]

Okay I switched the onComplete below but it’s still not working activating the native alert. I want that native alert to active when the keyboard is dismissed and the number detection is complete

[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 ui = require(“ui”)

– Predefine local objects for use later
local numberFieldSeven, numberFieldEight
local fields = display.newGroup()
local inputFontSize = 30
local inputFontHeight = 30
local tHeight = 50

local screen5 = display.newImage (“screen5.png”)
localGroup:insert(screen5)
–> This sets Screen5

local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 270
smallbrain.y = 430
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain

local function touchedSmallbrain (event)
if (“ended” == event.phase) then
_G.numberEight = numberFieldEight.text
director:changeScene (“screen6”)
media.playEventSound( “click_x.caf” )

end
end

smallbrain:addEventListener (“touch”, touchedSmallbrain)

– Tapping screen dismisses the keyboard
local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end
Runtime:addEventListener( “tap”, listener )

– This cleanUp function will remove all listeners from the scene since
– Director can’t remove listeners itself

local cleanUp = function()
Runtime:removeEventListener(“tap”, listener)
smallbrain:removeEventListener(“touch”, touchedSmallbrain)
end

– Determine if running on Corona Simulator – Native Text Fields not supported on Simulator
local isSimulator = “simulator” == system.getInfo(“environment”)
if isSimulator then
msg = display.newText( “Native Text Fields not supported on Simulator!”, 0, 280, nil, 12 )
msg.x = display.contentWidth/2 – center title
msg:setTextColor( 255,255,0 )
end

—>code to determine if numbers match
local function sortnum(number)
local numbersin = {}
local reminder
local returnnum = 0
if number ~=nil then
while number ~=0 do
reminder = number % 10
number = (number - (number % 10)) /10
table.insert(numbersin,reminder)
end
local mult = 1
table.sort(numbersin)

for i=1,#numbersin,1 do
returnnum = returnnum + (numbersin[i] * mult )
mult = mult * 10
end
end
return returnnum
end
–>end of number match detection

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Return to gameboard
end
end
end

–>this starts the native alert
if sortnum(number1) == sortnum(number2) then
print(“same”)
else
local alert = native.showAlert(“Error!”,“Your numbers do not tally!”, { " Ok " }, onComplete )
end

—>This ends the native alert

numberFieldSeven = native.newTextField( 50, 80, 85, tHeight, onnumberFieldSeven )
numberFieldSeven.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldSeven.inputType = “number”
numberFieldSeven.align = “center”
numberFieldSeven.text = _G.numberOne

numberFieldEight = native.newTextField( 180, 80, 85, tHeight, onnumberFieldEight )
numberFieldEight.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldEight.align = “center”
numberFieldEight.inputType = “number”

– Add fields to our new group

localGroup:insert(numberFieldSeven)
localGroup:insert(numberFieldEight)

–>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: 14056 reply_id: 51871[/import]