Clear Text Field

I’m using several nativeText Fields with the numbers keyboard. Is there any type of coding that you can attach to a button that will clear a text field of the data that is in it? I know i can back it out with the keyboard but I wanted to see if there was a way to do it with code after a button is pushed or tapped.

I would attach it to this code:

[lua]local playbutton = display.newImage (“play.png”)
localGroup:insert(playbutton)
playbutton.x = 100
playbutton.y = 55
playbutton.xScale = .5
playbutton.yScale = .5

local function playbuttonfn (event)
director:changeScene( “titlescreen”, “flip” )
media.playEventSound( “bloop_x.caf” )
end

playbutton:addEventListener (“tap”, playbuttonfn)[/lua] [import]uid: 72372 topic_id: 14134 reply_id: 314134[/import]

try setting the text property to nil. :slight_smile:
[lua]numberField.text = nil [/lua]
[import]uid: 71210 topic_id: 14134 reply_id: 52025[/import]

Won’t that effect if I have a global variable that is supplying the output to that textfield? I don’t want to stop what is coming to that textfield. I just want to keep that data from populating the new game the player starts. [import]uid: 72372 topic_id: 14134 reply_id: 52146[/import]

Setting the text property of the textfield won’t affect your global, it will just clear the view. [import]uid: 8692 topic_id: 14134 reply_id: 52163[/import]

Okay so where do I set this at? In the textfield section or in the play again area. this is my code below.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()

local inputFontSize = 80
local inputFontHeight = 80
local tHeight = 100
local ui = require(“ui”)

local number
if _G.numberSix ~= nil then
number = _G.numberSix
elseif _G.numberTen ~= nil then
number = _G.numberTen
else
number = 0
end

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

–>This is how we reset the level
local playbutton = display.newImage (“play.png”)
localGroup:insert(playbutton)
playbutton.x = 100
playbutton.y = 55
playbutton.xScale = .5
playbutton.yScale = .5

local function playbuttonfn (event)
director:changeScene( “titlescreen”, “flip” )
media.playEventSound( “bloop_x.caf” )
end

playbutton:addEventListener (“tap”, playbuttonfn)
local rateit = display.newImage (“rateit.png”)
rateit.x = 225
rateit.y = 55
rateit.xScale = .5
rateit.yScale = .5
localGroup:insert(rateit)
–>This places the rateit button

local function doRating(event)
if event.phase == “ended” then
local url = “itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa”
url = url … “/wa/viewContentsUserReviews?”
url = url … “type=Purple+Software&id=”
url = url … “453641732”

system.openURL(url)
end
end

rateit:addEventListener(“touch”, doRating)
–> Adds the Rate It functionality. Be sure to change the APP ID to your OWN;

local cleanUp = function()
rateitbutton:removeEventListener(“touch”, doRating)
play:removeEventListener (“touch”, touchedPlay)
end

l
– show the reminder in a textfield
numberFieldReminder = native.newTextField( 180, 315, 85, tHeight, onnumberFieldReminder)
numberFieldReminder.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldReminder.inputType = “number”
numberFieldReminder.align = “center”
numberFieldReminder.text = divisionBal
localGroup:insert(numberFieldReminder)
–>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: 14134 reply_id: 52177[/import]

I wanted to see if there was a way to do it with code after a button is pushed or tapped.

From what you say above I’d say put it in your button press handler function. [import]uid: 8692 topic_id: 14134 reply_id: 52181[/import]

Okay I tried the code below but it didn’t work.

[lua]–>This is how we reset the level
local playbutton = display.newImage (“play.png”)
localGroup:insert(playbutton)
playbutton.x = 100
playbutton.y = 55
playbutton.xScale = .5
playbutton.yScale = .5

local function playbuttonfn (event)
numberFieldReminder = nil
director:changeScene( “titlescreen”, “flip” )
media.playEventSound( “bloop_x.caf” )

end

playbutton:addEventListener (“tap”, playbuttonfn)[/lua] [import]uid: 72372 topic_id: 14134 reply_id: 52195[/import]

You need to set the text property of the display object, not set the object to nil. [import]uid: 8692 topic_id: 14134 reply_id: 52210[/import]

I don’t understand can you post for me? [import]uid: 72372 topic_id: 14134 reply_id: 52215[/import]

Instead of numberFieldReminder = nil, do this:

numberFieldReminder.text = nil

Okay it’s still not working. What am I doing wrong. Just so I can refresh what is happening. I have a game that you have two paths you can choose to play the game and for each path it gives a result on the last screen. You can then choose the play again button which takes you back to the first screen. I am finding that if you choose path one every time the game works fine but if you choose Path One and then hit the play again button and go to path two the result will give you the answer you had from path one.

I am trying to reset the game so that every time you play or play again you get a fresh start. I am adding the text field = nil but that’s not working. Here is my code.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
local inputFontSize = 80
local inputFontHeight = 80
local tHeight = 100
local ui = require(“ui”)

local number
if _G.numberSix ~= nil then
number = _G.numberSix
elseif _G.numberTen ~= nil then
number = _G.numberTen
else
number = 0
end

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

–>This is how we reset the level
local playbutton = display.newImage (“play.png”)
localGroup:insert(playbutton)
playbutton.x = 100
playbutton.y = 55
playbutton.xScale = .5
playbutton.yScale = .5

local function playbuttonfn (event)
numberFieldReminder.text = nil
director:changeScene( “titlescreen”, “flip” )
media.playEventSound( “bloop_x.caf” )

end

playbutton:addEventListener (“tap”, playbuttonfn)
local rateit = display.newImage (“rateit.png”)
rateit.x = 225
rateit.y = 55
rateit.xScale = .5
rateit.yScale = .5
localGroup:insert(rateit)
–>This places the rateit button

local function doRating(event)
if event.phase == “ended” then
local url = “itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa”
url = url … “/wa/viewContentsUserReviews?”
url = url … “type=Purple+Software&id=”
url = url … “453641732”

system.openURL(url)
end
end

rateit:addEventListener(“touch”, doRating)
–> Adds the Rate It functionality. Be sure to change the APP ID to your OWN;

local cleanUp = function()
rateitbutton:removeEventListener(“touch”, doRating)
play:removeEventListener (“touch”, touchedPlay)
end

local function onnumberFieldReminder( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then

end
end

– show the reminder in a textfield
numberFieldReminder = native.newTextField( 180, 315, 85, tHeight, onnumberFieldReminder)
numberFieldReminder.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldReminder.inputType = “number”
numberFieldReminder.align = “center”
numberFieldReminder.text = divisionBal

localGroup:insert(numberFieldReminder)

–>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: 14134 reply_id: 52284[/import]

Looks like you need to clear your divisionBal variable when you come back into this screen since you’re setting it in this module at the bottom. [import]uid: 8692 topic_id: 14134 reply_id: 52368[/import]

Yes that’s what I’m trying to do. How do I do that? I would rather clear it as I tap the play it again button.

I don’t want to clear when I’m entering the screen or the result the player is looking for will not show up. It would mess up the game that way. [import]uid: 72372 topic_id: 14134 reply_id: 52371[/import]

I would rather clear it as I tap the play it again button.

That sounds like the place to do it. [import]uid: 8692 topic_id: 14134 reply_id: 52373[/import]

Yes I know where to put it but what should I put? This is what I tried but it didn’t work.

[lua]–>This is how we reset the level
local playbutton = display.newImage (“play.png”)
localGroup:insert(playbutton)
playbutton.x = 100
playbutton.y = 55
playbutton.xScale = .5
playbutton.yScale = .5

local function playbuttonfn (event)
divisionBal = nil
director:changeScene( “titlescreen”, “flip” )
media.playEventSound( “bloop_x.caf” )

end

playbutton:addEventListener (“tap”, playbuttonfn)[/lua]
[import]uid: 72372 topic_id: 14134 reply_id: 52375[/import]

Where is divisionBal set? Maybe you’re setting it somewhere else when you come back into this screen. [import]uid: 8692 topic_id: 14134 reply_id: 52376[/import]

It has to be set when it comes back to the screen or you will not be able to get the answer for that session of the game.

I want it cleared when you leave the screen but to work when you come back on the screen.

The problem is that it is maintaing the answer from a previous game so I need to clear it every time.

Right now the only way to completely clear it is to go Path one or to go to the homescreen of the device. [import]uid: 72372 topic_id: 14134 reply_id: 52377[/import]

I think the textField stuff is working properly, you just need to figure out your game state and preserve/clear the value appropriately based on what screen you are in. [import]uid: 8692 topic_id: 14134 reply_id: 52379[/import]

Yes I agree which is what I was posting this for to figure out how to do it. There is not a problem with any of the code I just don’t know how to clear it out. I’ll repost for others to comment. [import]uid: 72372 topic_id: 14134 reply_id: 52380[/import]

I see, I was responding based on your original question as to how to clear the text field in code. You’ll probably want to make a separate post with details on your question on logic specific to your game. :slight_smile: [import]uid: 8692 topic_id: 14134 reply_id: 52381[/import]