why doesn't this display/delete code work correctly?

i have this code set off by a widget button named goBtn

it keeps displaying the new randomly generated number over the old one for some reason rendering it unreadable most of the time

thanks

[lua]

local playedOnce = false

local function ongoBtnPress()

    local color = math.random( 5 )

    

    if playedOnce == false then

    

        local displayedColor = display.newText(color, 300, 20, native.systemFont, 40)

        displayedColor:setTextColor(255, 255, 255)

        

        local playedOnce = true

    else

    

        displayedColor:removeSelf()

    

        local displayedColor = display.newText(color, 300, 20, native.systemFont, 40)

        displayedColor:setTextColor(255, 255, 255)

    

        return true

    end

end

[/lua]

Remove the ‘local’ from playedOnce in your function. When you put local in the function it is different from the ‘local’ outside the code block.

Try this.

local playedOnce = false local displayedColor = display.newText("0", 300, 20, native.systemFont, 40) displayedColor:setTextColor(255, 255, 255) local function ongoBtnPress() local color = math.random( 5 ) if playedOnce == false then displayedColor.text = color playedOnce = true else displayedColor.text = color return true end end

Good Luck!

burhan

That did the trick!  Thank you every so much.

Remove the ‘local’ from playedOnce in your function. When you put local in the function it is different from the ‘local’ outside the code block.

Try this.

local playedOnce = false local displayedColor = display.newText("0", 300, 20, native.systemFont, 40) displayedColor:setTextColor(255, 255, 255) local function ongoBtnPress() local color = math.random( 5 ) if playedOnce == false then displayedColor.text = color playedOnce = true else displayedColor.text = color return true end end

Good Luck!

burhan

That did the trick!  Thank you every so much.