Hi I am a beginner ,and made a calculator app it is running fine but one problem is there than when I am using it second time the second answer got superimposed on the other ,is there any way so that previous ans gets disappeared before first one like we have CE and C button.
Either remove the existing text object before creating the new answer - https://docs.coronalabs.com/guide/media/displayObjects/index.html#remove - or better yet, just update the object’s text property - https://docs.coronalabs.com/api/library/display/newText.html#text-required
local bkgd=display.newImage("bkgd.png",0,0) local firstNumber=native.newTextField(150,75, 220, 36) firstNumber.inputType="number" local secondNumber=native.newTextField(150,135, 220, 36) secondNumber.inputType="number" local result local resultText local addButton=display.newImage("add.png",50,display.contentHeight-80) local minusButton=display.newImage("minus.png",50,display.contentHeight-30) local multiplyButton=display.newImage("multiply.png",50,display.contentHeight-140) local divideButton=display.newImage("divide.png",50,display.contentHeight-200) local ceButton=display.newImage("ce.png",display.contentWidth-50,display.contentHeight-30) local function addButtontap( event ) local result=tonumber(firstNumber.text) +tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end local function minusButtontap( event ) local result=tonumber(firstNumber.text) -tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end local function multiplyButtontap( event ) local result=tonumber(firstNumber.text) \*tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end local function divideButtontap( event ) local result=tonumber(firstNumber.text)/tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end ------------------------------------------------------------------------ ------------------------------------------------------------------------------- local function ceButtontap( event ) resultText:removeSelf(resultText.text)-------------------While clicking it is giving error end ------------------------------------------------------------this is the CE functions Sir -------------------------------------------------------------------- -------------------------------------------------------------------- local listener = function (event) native.setKeyboardFocus(nil) end addButton:addEventListener("tap", addButtontap) minusButton:addEventListener("tap", minusButtontap) multiplyButton:addEventListener("tap", multiplyButtontap) divideButton:addEventListener("tap", divideButtontap) bkgd:addEventListener("tap", listener) ceButton:addEventListener("tap", ceButtontap)
Sir I tried to implement what you said but that went in vain can please explain it more clear fully ( I['m beginner) ,I have given the code this time ,but didn’t give the images ,
In that code you’re calling the “removeSelf” function of the text object and then passing in a string property.
The removeSelf function wouldn’t need a parameter, let alone a string object.
Without knowing what the error message is I can only guess but it’s quite possibly a scope issue, as most are, so ensure that the resultText object is still in scope in that method and then instead call this:
display.remove( resultText ) resultText = nil
But again, this is simply destroying the text object. A better solution would be to simply update its text property to an empty string.
As a beginner you’re going to come up against lots of little problems like this and the best thing to do will be to ensure you’ve read, and understood, the documentation around whatever you are doing and then try to make the simplest example of what you want ( in this case clearing a text object ) in a blank project.
What will often happen is when you are working on that little project you’ll work out what went wrong in your main project.
Sir still the problem persist ,no error is shown but the second answer is superimposed on first one
I got the solution,thank for your guidance and ignore my previous reply
One problem is you are making resultText local at the top of the file (correct), but also resultText is local within each of your functions. This means that resultText within the functions is a completely different object to resultText outside.
This is an example of what I mean:
[lua]
local myVariable = “My name is Tony”
local myOtherVariable = “My name is Steve”
print (“START”, myVariable, myOtherVariable)
local changeNames = function ()
local myVariable = “My name is Geoff”
myOtherVariable = “My name is Ryan”
print (“WITHIN FUNCTION”, myVariable, myOtherVariable)
end
changeName()
print (“AFTER FUNCTION”, myVariable, myOtherVariable)
[/lua]
Either remove the existing text object before creating the new answer - https://docs.coronalabs.com/guide/media/displayObjects/index.html#remove - or better yet, just update the object’s text property - https://docs.coronalabs.com/api/library/display/newText.html#text-required
local bkgd=display.newImage("bkgd.png",0,0) local firstNumber=native.newTextField(150,75, 220, 36) firstNumber.inputType="number" local secondNumber=native.newTextField(150,135, 220, 36) secondNumber.inputType="number" local result local resultText local addButton=display.newImage("add.png",50,display.contentHeight-80) local minusButton=display.newImage("minus.png",50,display.contentHeight-30) local multiplyButton=display.newImage("multiply.png",50,display.contentHeight-140) local divideButton=display.newImage("divide.png",50,display.contentHeight-200) local ceButton=display.newImage("ce.png",display.contentWidth-50,display.contentHeight-30) local function addButtontap( event ) local result=tonumber(firstNumber.text) +tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end local function minusButtontap( event ) local result=tonumber(firstNumber.text) -tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end local function multiplyButtontap( event ) local result=tonumber(firstNumber.text) \*tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end local function divideButtontap( event ) local result=tonumber(firstNumber.text)/tonumber(secondNumber.text) local resultText = display.newText(result, display.contentWidth/2,display.contentHeight/2+100, native.systemFont, 36) resultText:setTextColor(255,255,255) end ------------------------------------------------------------------------ ------------------------------------------------------------------------------- local function ceButtontap( event ) resultText:removeSelf(resultText.text)-------------------While clicking it is giving error end ------------------------------------------------------------this is the CE functions Sir -------------------------------------------------------------------- -------------------------------------------------------------------- local listener = function (event) native.setKeyboardFocus(nil) end addButton:addEventListener("tap", addButtontap) minusButton:addEventListener("tap", minusButtontap) multiplyButton:addEventListener("tap", multiplyButtontap) divideButton:addEventListener("tap", divideButtontap) bkgd:addEventListener("tap", listener) ceButton:addEventListener("tap", ceButtontap)
Sir I tried to implement what you said but that went in vain can please explain it more clear fully ( I['m beginner) ,I have given the code this time ,but didn’t give the images ,
In that code you’re calling the “removeSelf” function of the text object and then passing in a string property.
The removeSelf function wouldn’t need a parameter, let alone a string object.
Without knowing what the error message is I can only guess but it’s quite possibly a scope issue, as most are, so ensure that the resultText object is still in scope in that method and then instead call this:
display.remove( resultText ) resultText = nil
But again, this is simply destroying the text object. A better solution would be to simply update its text property to an empty string.
As a beginner you’re going to come up against lots of little problems like this and the best thing to do will be to ensure you’ve read, and understood, the documentation around whatever you are doing and then try to make the simplest example of what you want ( in this case clearing a text object ) in a blank project.
What will often happen is when you are working on that little project you’ll work out what went wrong in your main project.
Sir still the problem persist ,no error is shown but the second answer is superimposed on first one
I got the solution,thank for your guidance and ignore my previous reply
One problem is you are making resultText local at the top of the file (correct), but also resultText is local within each of your functions. This means that resultText within the functions is a completely different object to resultText outside.
This is an example of what I mean:
[lua]
local myVariable = “My name is Tony”
local myOtherVariable = “My name is Steve”
print (“START”, myVariable, myOtherVariable)
local changeNames = function ()
local myVariable = “My name is Geoff”
myOtherVariable = “My name is Ryan”
print (“WITHIN FUNCTION”, myVariable, myOtherVariable)
end
changeName()
print (“AFTER FUNCTION”, myVariable, myOtherVariable)
[/lua]