Where or how to remove TextField in storyboard scene?

Hi,

I have a scene where I add a native text field to be used. In the docs it says I need to use the object.removeSelf(). Where do I do this to remove the text field when I leave the scene? Because if I do a storyboard.gotoScene the text field stays there on the screen. If the text field is called field1 do I say field1.selfRemove()? And if so where in the storyboard do I do this? I tried different places and the app stops running or doesn’t go to the new scene. I thought it would go in scene:exitScene or scene:destroyScene functions but doesn’t work there.

Here is how I add it to the current scene:

local field1 = native.newTextField( 20, 550, 260, 35 )  
field1.size = 14  
field1.font = native.newFont( "Helvetica-Bold", 14 )  
field1.text = ""  
field1:setTextColor( 51, 102, 149 )  

Thanks!!
[import]uid: 184193 topic_id: 34729 reply_id: 334729[/import]

I’ve found it best to either add native.newTextField()'s either in the enterScene() function or set up a willEnterScene() function. Since these are native objects they are not managed by groups which means storyboard cannot manage them.

You should create them every time the scene is entered and the logical place to remove them is during the exitScene() phase.

[import]uid: 199310 topic_id: 34729 reply_id: 138002[/import]

I said above that I tried to remove it in the exitScene() phase and that did not work. Do I remove it by calling field1.removeSelf()?

Anyone know how to remove this in a scene?

Thanks,

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138082[/import]

It should be field1:removeSelf() – the : is a lua shortcut for saying (self) and must be used in this case.

However, if you define local field1 within createScene or enterScene, it won’t be visible in exitScene because it’s localized to the other place. So what you should do is pre-declare the variable.

[code]local field 1 – it doesn’t mean anything, but it’s now visible by *everything* below it in the file

function scene:enterScene(event)
field1 = native.newTextField(stuff) – if this was local here, it would only be visible until the “end” word
end

function scene:exitScene(event)
field1:removeSelf()
end[/code] [import]uid: 41884 topic_id: 34729 reply_id: 138084[/import]

Thanks for the reply. It worked with one problem. When I click to leave the scene and go to another the text field goes away as needed. But when i got back to the page the text field will not show again. Why is that? I have added the field1:removeSelf to the scene:exitScene function which works now. But no control when i go back to the scene. Any idea? Maybe I am not purging that scene when I’m at the new scene page so it is showing the way it was again?

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138087[/import]

By default storyboard doesn’t destroy your scene unless (a) you purge it or (b) it runs low on memory. So if you create field1 in createScene(), createScene is only called if the scene is brand new, whereas enterScene is called every time you return.

So the easiest solution here (if you always remove field1 on exit) is to just make it on enterScene instead. (if the delay bugs you, there is also “willEnterScene” as well, I think) [import]uid: 41884 topic_id: 34729 reply_id: 138088[/import]

I did what you said and create the field1 in enterScene and is there when I run it. When i try to leave the page with a button that called to go to another scene it doesn’t do anything with field1:removeSelf() in the exitScene. If I remove it from the exitScene() it goes to the other scene but the control is still there.

Sorry for all of this. Just trying to see where to add and remove the control in the right places.

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138110[/import]

What do you mean by “doesn’t do anything?” Does the text display go away? [import]uid: 41884 topic_id: 34729 reply_id: 138113[/import]

When I click my Cancel button I have on the scene that calls to go to another scene, it does not work. The code was not changed there at all. The code never changed. It just does not work when i have the field1:removeSelf() in the exit scene. If I remove it the cancel button works and it goes to the other scene. I had put to remove the field1 in the cancel code below before and worked but it never came back when I returned to the scene. I don’t think I had the field added in the enterScene though.

local onbtnCancelEvent = function ( event )  
 if event.phase == "release" then  
 storyboard.gotoScene( "SceneFacebook", "fade", 300 )  
 end  
 return true  
end  

[import]uid: 184193 topic_id: 34729 reply_id: 138120[/import]

That code seems fine. I guess I’ll have to try later at home to see what might happen… [import]uid: 41884 topic_id: 34729 reply_id: 138124[/import]

I have tried it several different ways. The best way so far is declaring it at the top and not in the enter scene because I cannot access the text of the control otherwise. And I remove it in the cancel button. It’s just that it does not come back and show the next time.

You mentioned if I purged the scene that it would come back the next time. Where would I purge it? I tried adding storyboard.purgeScene( “sceneFacebook2” ) which is the scene that has the text field. I added this in the scene I am going to and added it everywhere but it must not be purging. I added it in the enterScene which seems like it would purge it.

UPDATE: While typing this I looked up the purging and had it on the wrong place. I ama lso using removeAll which works well. I now have it working!

Thanks a lot for your help!!

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138154[/import]

I’ve found it best to either add native.newTextField()'s either in the enterScene() function or set up a willEnterScene() function. Since these are native objects they are not managed by groups which means storyboard cannot manage them.

You should create them every time the scene is entered and the logical place to remove them is during the exitScene() phase.

[import]uid: 199310 topic_id: 34729 reply_id: 138002[/import]

I said above that I tried to remove it in the exitScene() phase and that did not work. Do I remove it by calling field1.removeSelf()?

Anyone know how to remove this in a scene?

Thanks,

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138082[/import]

It should be field1:removeSelf() – the : is a lua shortcut for saying (self) and must be used in this case.

However, if you define local field1 within createScene or enterScene, it won’t be visible in exitScene because it’s localized to the other place. So what you should do is pre-declare the variable.

[code]local field 1 – it doesn’t mean anything, but it’s now visible by *everything* below it in the file

function scene:enterScene(event)
field1 = native.newTextField(stuff) – if this was local here, it would only be visible until the “end” word
end

function scene:exitScene(event)
field1:removeSelf()
end[/code] [import]uid: 41884 topic_id: 34729 reply_id: 138084[/import]

Thanks for the reply. It worked with one problem. When I click to leave the scene and go to another the text field goes away as needed. But when i got back to the page the text field will not show again. Why is that? I have added the field1:removeSelf to the scene:exitScene function which works now. But no control when i go back to the scene. Any idea? Maybe I am not purging that scene when I’m at the new scene page so it is showing the way it was again?

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138087[/import]

By default storyboard doesn’t destroy your scene unless (a) you purge it or (b) it runs low on memory. So if you create field1 in createScene(), createScene is only called if the scene is brand new, whereas enterScene is called every time you return.

So the easiest solution here (if you always remove field1 on exit) is to just make it on enterScene instead. (if the delay bugs you, there is also “willEnterScene” as well, I think) [import]uid: 41884 topic_id: 34729 reply_id: 138088[/import]

I did what you said and create the field1 in enterScene and is there when I run it. When i try to leave the page with a button that called to go to another scene it doesn’t do anything with field1:removeSelf() in the exitScene. If I remove it from the exitScene() it goes to the other scene but the control is still there.

Sorry for all of this. Just trying to see where to add and remove the control in the right places.

Warren
[import]uid: 184193 topic_id: 34729 reply_id: 138110[/import]

What do you mean by “doesn’t do anything?” Does the text display go away? [import]uid: 41884 topic_id: 34729 reply_id: 138113[/import]

When I click my Cancel button I have on the scene that calls to go to another scene, it does not work. The code was not changed there at all. The code never changed. It just does not work when i have the field1:removeSelf() in the exit scene. If I remove it the cancel button works and it goes to the other scene. I had put to remove the field1 in the cancel code below before and worked but it never came back when I returned to the scene. I don’t think I had the field added in the enterScene though.

local onbtnCancelEvent = function ( event )  
 if event.phase == "release" then  
 storyboard.gotoScene( "SceneFacebook", "fade", 300 )  
 end  
 return true  
end  

[import]uid: 184193 topic_id: 34729 reply_id: 138120[/import]

That code seems fine. I guess I’ll have to try later at home to see what might happen… [import]uid: 41884 topic_id: 34729 reply_id: 138124[/import]