respawn object on button touch

Hey guys how do I destroy an object then respawn it right after it is destoryed. I got it so it displays one on game startup then when you touch the button it displays a new one  but it only works once. Any Help?


_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 price = display.newText(math.random(100,10000),50,50,native.systemFont,16)
        price:setTextColor(1,0,0)

local function onObjectTouch( event )
    if event.phase == “began” then
       price:removeSelf()
       local price = display.newText(math.random(100,10000),50,50,native.systemFont,16)
        price:setTextColor(1,0,0)
       
    end
end
newcard:addEventListener(“touch”,onObjectTouch)_

 

@tpjacobson01, try this:

local onTouch local someNewText local centerX = display.contentCenterX local centerY = display.contentCenterY local text = display.newText(math.random(100,10000),centerX,centerY,nil,40) function someNewText() text.text = math.random(100,10000) transition.to(text, {time = 100, alpha = 1}) end function onTouch(event) if (event.phase == "began") then transition.to(event.target,{time = 100, alpha = 0, onComplete = someNewText} ) end end text:addEventListener("touch",onTouch)

So,  like you did, we spawn a text object that is a random number. When the user touches the number, we use a transition to fade the objects alpha to 0 (we can’t see it), over a period of 100ms. When the transition completes we run the function someNewText. someNewText simply changes the original text to a new number and then fades our alpha back in so we can see the new number. 

@tpjacobson01, try this:

local onTouch local someNewText local centerX = display.contentCenterX local centerY = display.contentCenterY local text = display.newText(math.random(100,10000),centerX,centerY,nil,40) function someNewText() text.text = math.random(100,10000) transition.to(text, {time = 100, alpha = 1}) end function onTouch(event) if (event.phase == "began") then transition.to(event.target,{time = 100, alpha = 0, onComplete = someNewText} ) end end text:addEventListener("touch",onTouch)

So,  like you did, we spawn a text object that is a random number. When the user touches the number, we use a transition to fade the objects alpha to 0 (we can’t see it), over a period of 100ms. When the transition completes we run the function someNewText. someNewText simply changes the original text to a new number and then fades our alpha back in so we can see the new number.