what's wrong with this Lua code (creating text inside listener in Corona)

Posted by Greg on Game Development See other posts from Game Development or by Greg
Published on 2012-04-21T03:59:43Z Indexed on 2012/06/08 10:47 UTC
Read the original article Hit count: 292

Filed under:
|

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(...)"?

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)

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...

© Game Development or respective owner

Related posts about lua

Related posts about corona-sdk