Score Overlapping

When I click on my object to increase the score the score increase but overlaps the score that was there before making it impossible to read it. Any idea why this is happening?

[lua]local score = 0
background = display.newRect(175,100,1000,1000)
background:setFillColor(.2,.5,.1)
pile = display.newImageRect(“bill.jpg”,200,400)
pile.x = display.contentCenterX
pile.y = display.contentCenterY + 200

function add(event)
if event.phase == “began” then
score = score + 1
scoreText = display.newText(score,display.contentCenterX,50,“helvetica”,20)
print(score)
end
return true
end

pile:addEventListener(“touch”,add)[/lua]

Try this ,

  1. local score = 0
  2. local scoreText
  3. background = display.newRect(175,100,1000,1000)
  4. background:setFillColor(.2,.5,.1)
  5. pile = display.newImageRect(“bill.jpg”,200,400)
  6. pile.x = display.contentCenterX
  7. pile.y = display.contentCenterY + 200
  8. function add(event)
  9. if event.phase == “began” then
  10. if score then

                    scoreText.text = " "

             end

  1. score = score + 1
  2. scoreText = display.newText(score,display.contentCenterX,50,“helvetica”,20)
  3. print(score)
  4. end
  5. return true
  6. end
  7. pile:addEventListener(“touch”,add)

That didn’t work either.  It comes back with this error…

2014-08-06 00:46:40.418 Corona Simulator[27277:507] Runtime error

main.lua:40: attempt to index global ‘scoreText’ (a nil value)

stack traceback:

main.lua:40: in function <main.lua:7>

?: in function <?:221>

Sorry For that post,

I have  test This works fine , try this.

        local score = 0

local scoreText

background = display.newRect(175,100,1000,1000)

background:setFillColor(.2,.5,.1)

pile = display.newImageRect(“bill.jpg”,200,400)

pile.x = display.contentCenterX

pile.y = display.contentCenterY + 200

function add(event)

if event.phase == “began” then

if score > 0 then

scoreText.text = " "

end

score = score + 1

scoreText = display.newText(score,display.contentCenterX,50,“helvetica”,20)

print(score)

end

return true

end

pile:addEventListener(“touch”,add)

why are you recreating the text object every time, rather than just updating its text? that is bad practise.

create it once, and just update its text property when you want to increase the score… that will also change the text that is visually shown on screen

i.e. myText.text = tostring( score )

Try this ,

  1. local score = 0
  2. local scoreText
  3. background = display.newRect(175,100,1000,1000)
  4. background:setFillColor(.2,.5,.1)
  5. pile = display.newImageRect(“bill.jpg”,200,400)
  6. pile.x = display.contentCenterX
  7. pile.y = display.contentCenterY + 200
  8. function add(event)
  9. if event.phase == “began” then
  10. if score then

                    scoreText.text = " "

             end

  1. score = score + 1
  2. scoreText = display.newText(score,display.contentCenterX,50,“helvetica”,20)
  3. print(score)
  4. end
  5. return true
  6. end
  7. pile:addEventListener(“touch”,add)

That didn’t work either.  It comes back with this error…

2014-08-06 00:46:40.418 Corona Simulator[27277:507] Runtime error

main.lua:40: attempt to index global ‘scoreText’ (a nil value)

stack traceback:

main.lua:40: in function <main.lua:7>

?: in function <?:221>

Sorry For that post,

I have  test This works fine , try this.

        local score = 0

local scoreText

background = display.newRect(175,100,1000,1000)

background:setFillColor(.2,.5,.1)

pile = display.newImageRect(“bill.jpg”,200,400)

pile.x = display.contentCenterX

pile.y = display.contentCenterY + 200

function add(event)

if event.phase == “began” then

if score > 0 then

scoreText.text = " "

end

score = score + 1

scoreText = display.newText(score,display.contentCenterX,50,“helvetica”,20)

print(score)

end

return true

end

pile:addEventListener(“touch”,add)

why are you recreating the text object every time, rather than just updating its text? that is bad practise.

create it once, and just update its text property when you want to increase the score… that will also change the text that is visually shown on screen

i.e. myText.text = tostring( score )