destory self on button release

Hey guys how do I make it so when a button is released it will destroy previous text and display new text when the other is destoryed. It displays the right stuff but it stays their I want it so it will destroy itself then display new text. My code Below

_----------------------------------------------------------------------------------------

– main.lua


– code below is acutally from local score = require(“score”)
local widget = require (“widget”)

    

local rectangle = display.newRect(160,200,275,400)
 

local buy = widget.newButton
{
    left = -45,
    top = 350,
    id = “buybtn”,
    label = “Buy”,
    
}

local newcard = widget.newButton
{

    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New Card”,

     

}

local function onObjectTouch( event )
     if event.phase == “began” then
        print( "Touch event began on: " )
        local price = display.newText(math.random(100,10000),50,50,native.systemFont,16)
          price:setTextColor(1,0,0)
          
         
     end
     return true
 end

           
newcard:addEventListener( “touch”, onObjectTouch )_
 

Hi jacob,

An easy way to do is to put outside the local price and delete it every time you released the button:

local price --create the button local function onObjectTouch( event ) if event.phase == "began" then print( "Touch event began on: " ) display.remove(price) price = display.newText(math.random(100,10000),50,50,native.systemFont,16) price:setTextColor(1,0,0) end return true end newcard:addEventListener( "touch", onObjectTouch )

if you have more than one object to delete every time use table or group to manage it cleaner

newtext = math.random(100,10000)

price.text = newtext

I think does much the same as you currently are doing without the delete and new create - just change the text.

T.

Hi jacob,

An easy way to do is to put outside the local price and delete it every time you released the button:

local price --create the button local function onObjectTouch( event ) if event.phase == "began" then print( "Touch event began on: " ) display.remove(price) price = display.newText(math.random(100,10000),50,50,native.systemFont,16) price:setTextColor(1,0,0) end return true end newcard:addEventListener( "touch", onObjectTouch )

if you have more than one object to delete every time use table or group to manage it cleaner

newtext = math.random(100,10000)

price.text = newtext

I think does much the same as you currently are doing without the delete and new create - just change the text.

T.