what's wrong with this code - creating text insider listener

If you double/triple click on the myObject here the text does NOT disappear. Why is this not working when there are multiple events being fired?

That is, are there actually multiple “text” objects, with some existing but no longer having a reference to them held by the local “myText” variable? Do I have to manually removeSelf() on the local “myText” field before assigning it another “display.newText(…)”?

[code]
display.setStatusBar( display.HiddenStatusBar )

local myText

local function hideMyText(event)
print (“hideMyText”)
myText.isVisible = false
end

local function showTextListener(event)
if event.phase == “began” then
print(“showTextListener”)
myText = display.newText(“Hello World!”, 0, 0, native.systemFont, 30)
timer.performWithDelay(1000, hideMyText, 1 )
end
end

– Display object to press to show text
local myObject = display.newImage( “inventory_button.png”, display.contentWidth/2, display.contentHeight/2)
myObject:addEventListener(“touch”, showTextListener)

[/code] [import]uid: 140210 topic_id: 25345 reply_id: 325345[/import]

PS. Question 2 - Also why is it the case that if I add a line BEFORE “myText = …” of:

a) “if myText then myText:removeSelf() end” => THIS FIXES THINGS, whereas

b) “if myText then myText=nil end” => DOES NOT FIX THINGS

Interested in hearing how Lua works here re the answer…
[import]uid: 140210 topic_id: 25345 reply_id: 102366[/import]

A) works because you are checking if myText exists and if it does you are removing it, so there wont be two "myText"s floating around.

B) doesn’t work because setting something as nil is not the same as removing. You would remove and then set to nil. [import]uid: 52491 topic_id: 25345 reply_id: 102388[/import]

tks Peach - but then what happens to the floating object then? (i.e. just assuming I don’t clean it up) Can I ask:

a) does Corona Lua have a garbage collector that would eventually delete it?

b) or would there still be a variable that would hold a reference to the first floating one? I guess it must as it would still be displayed on the screen wouldn’t it?

c) following on from b) what actual “print” statement could I put in place to actual prove to myself that there is this initial text object floating around? i.e. prove to myself then how Lua works with it…

d) is there any Lau short cuts that handle re-assigning a new object to a variable? (e.g. in Ruby there is, and it automatically removes the previous value & puts in the new one)
[import]uid: 140210 topic_id: 25345 reply_id: 102400[/import]

A) Corona does use garbage collect regularly on its own, however you can also call it yourself if you want to using: collectgarbage(“collect”)

B/C) The thing is you aren’t currently removing the text, at least in the sample code above - which means that every time you press the button another text object called myText is being created and you are just setting the newest one to be invisible.

You might consider adding something like local textCount = 0 and then adding one each time you show new text and subtracting one when you remove one. (Not just hide one, that wont work.) you could print that.

I’m not sure on quite what D) means, or what you are asking - you want to automatically give the new text unique handles, perhaps?

Let me know :slight_smile: [import]uid: 52491 topic_id: 25345 reply_id: 102405[/import]

thanks Peach - probably the only remaining question I wouldn’t mind asking (sorry - just like to try to understand things was c), i.e.


c) what actual “print” statement could I put in place to actual prove to myself that there is this initial text object floating around? i.e. prove to myself then how Lua works with it…

make sense? like would the floating initial “text” object still be somewhere where one could see it, for example a global under “_G” or something? or is it really just in the background using up a bit of memory, and still being displayed on the screen, but with no-way to then get at it from a programatic sense? [import]uid: 140210 topic_id: 25345 reply_id: 102409[/import]

Honestly I’m not actually sure how you would check it as you’ve reassigned the name; I tend to be a little OCD about best practices and so it’s not something I’ve had much experience with - I’ll send an email off to Tom (another member of the team) as I imagine he can give me some insight to pass on :wink: [import]uid: 52491 topic_id: 25345 reply_id: 102420[/import]

To answer your question about how to check to see if the text messages are still around, you can do that by printing out the texture memory used. Text objects are converted to bitmaps when displayed and use texture memory. This is different from Lua memory used by global variables.

Your local assignment of creating text messages in the listener will create “orphan” text messages after multiple touches since the variable they are assigned to will be replaced with new text pointers. The pointer to text object will be lost and text can’t be removed.

Here is how you can display the texture memory used by your app. Texture memory is used for images and text objects.

print( "Texture memory used = " .. system.getInfo( "textureMemoryUsed" )

ok thanks [import]uid: 140210 topic_id: 25345 reply_id: 102802[/import]