Please Help: Problem removing old text from screen, when new text is added on screen the old text is still displayed under the new text.

I have an issue with my code, the way it works is when a button is pressed then a function executes and then displays a value on the screen, and it does that. The problem is when the button is pressed again and a new value is displayed the old value does not go away. I have tried,

object:removeSelf() object = nil

but I get “attempt to call method ‘remove Self’ (a nil value)”

Here is the function 

function resulter (d,c,s,dl,cl,sl) --rText.text = "" dbText:removeSelf() local r d = d \* dl c = c \* cl s = s \* sl r = (d + c) + s local resultIs print (r) resultIs = r if (resultIs) then dbText:removeSelf() dbText = nil end dbText = ("db Lossasass: " .. resultIs) local rText = display.newText (dbText, 0, 0, native.systemFont, 42) rText:setTextColor(255,255,255) rText.x = \_W \* .5 rText.y = \_H -100 end

and here is the sample text that is displayed before the function gets called. 

--Result Text dbText = ("butt") rText = display.newText (dbText,0, 0,native.systemFont, 42) rText:setTextColor(255,255,255) rText.x = \_W \* .5 rText.y = \_H -100

When the function is called, the current text is supposed to be erased and the new text is supposed to replace it, but rather than replacing it, it simply appears over the old text.

Not sure what to do here.

Is there a reason why you are removing and creating the text all over again?

If you do

rText.text = dbText

it will update your text directly and you will not have to remove it and re-create it. I can actually see you have part of that code in your function but it’s at the top and commented out.

To answer your question, you are removing dbText which only contains the string of what you actually wanted display, rText is the real text ‘element’ that you need to remove so it deletes the old text.

That worked like a charm thank you, that was driving me crazy… Such an easy fix and I was making it more complicated than it needed to be.

I think what “Hate thinking of names” is saying is why not just change the “text” rather than destroying the object and making a new one. Just replace the old text with new, which seems to be what you’re doing.

if you do rText.text = “new text” it will replace the old text displayed with the “new text”

Or am I missing it?

Is there a reason why you are removing and creating the text all over again?

If you do

rText.text = dbText

it will update your text directly and you will not have to remove it and re-create it. I can actually see you have part of that code in your function but it’s at the top and commented out.

To answer your question, you are removing dbText which only contains the string of what you actually wanted display, rText is the real text ‘element’ that you need to remove so it deletes the old text.

That worked like a charm thank you, that was driving me crazy… Such an easy fix and I was making it more complicated than it needed to be.

I think what “Hate thinking of names” is saying is why not just change the “text” rather than destroying the object and making a new one. Just replace the old text with new, which seems to be what you’re doing.

if you do rText.text = “new text” it will replace the old text displayed with the “new text”

Or am I missing it?