Losing Lives in Game causes text error

 

The function for losing lives in my game generates an overlap in the text.

  • I have been working on this error for a while now, but my simple mind can’t comprehend it. I have a system that causes the player to lose a life if they select a wrong answer from a set of three. (the two options on the right are wrong purely for practical reasons when coding). However, when the wrong answer is selected, the text that displays the lives duplicates, creating an overlap that looks like the image linked.

– Setup and start physics 

local physics = require(“physics”) 

physics.start() 

physics.setGravity(0,1)

– most commonly used screen coordinates

centerX = display.contentCenterX

centerY = display.contentCenterY

screenLeft = display.screenOriginX

screenWidth = display.contentWidth - screenLeft * 2

screenRight = screenLeft + screenWidth

screenTop = display.screenOriginY

screenHeight = display.contentHeight - screenTop * 2

screenBottom = screenTop + screenHeight

display.contentWidth = screenWidth

display.contentHeight = screenHeight

–Variables

ballRadius=20

balls={}

numberOfBalls = 0

tick=400

text = {}

numberOfText = 0

local deltaX = screenWidth/4

ball1x = deltaX

bally = screenTop - 60

ball2x = deltaX*2

ball3x = deltaX*3

tick = 400

gameloop = 0

livesCounter = 0

lives = {}

loss = 0 

on = true

lives = 3 

display.setDefault( “background”, 1, 1, 0.9 )

local bottom = display.newRect(centerX, screenBottom, screenWidth,40)

bottom:setFillColor(1, 1, 0.9)

physics.addBody(bottom,“static”)

local function DisplayLives()

    local DisplayLives = display.newText("lives: "…lives, screenLeft + 50, screenTop + 50, native.systemFont, 16 )

       DisplayLives:setFillColor(1,0,0)

end

–  Math Equation

local function Equation()

    local v1 = math.random(1,30)

    local v2 = math.random(1,30)

    ans = v1 + v2

    local questions = (v1…" + "…v2)

     questionText = display.newText((questions), centerX, centerY, native.systemFont, 16 )

    questionText:setFillColor( 1, 0, 0 )

end

– Creates Balls 1 to 3

local function CreateBall1()

     ball1 = display.newCircle(ball1x, bally, 30 )

     ballText = display.newText(ans, ball1x, bally)

    ball1:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255)

    physics.addBody( ball1,{ “dynamic”, density=1.0, friction=0.3, bounce=0.2, radius=25 } )

    physics.addBody( ballText,{ “dynamic”, density=1.0, friction=0.3, bounce=0.2, radius=25 } )

    

end

local function CreateBall2()

    ball2 = display.newCircle(ball2x, bally, 30 )

    ball2Text = display.newText(ans + math.random(1,6), ball2x, bally)

    ball2:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255)

    physics.addBody( ball2,{ “dynamic”, density=1.0, friction=0.3, bounce=0.2, radius=25 } )

    physics.addBody( ball2Text,{ “dynamic”, density=1.0, friction=0.3, bounce=0.2, radius=25 } )

end

local function CreateBall3()

    ball3 = display.newCircle(ball3x, bally, 30 )

    ball3Text = display.newText(ans - math.random(1,6), ball3x, bally)

    ball3:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255)

    physics.addBody( ball3,{ “dynamic”, density=1.0, friction=0.3, bounce=0.2, radius=25 } )

    physics.addBody( ball3Text,{ “dynamic”, density=1.0, friction=0.3, bounce=0.2, radius=25 } )

end

– Destroys the Balls

local function EndBall1()

    ball1:removeSelf()

    ballText:removeSelf()

end

local function EndBall2()

    ball2:removeSelf()

    ball2Text:removeSelf()

end

 local function EndBall3()

    ball3:removeSelf()

    ball3Text:removeSelf()

end

– Ends the Equation

local function EquationEnd()

    questionText:removeSelf()

end

local function OnTap1(event)

    local correct = display.newText(“CORRECT”, centerX, centerY, native.systemFont, 36 )

    correct:setFillColor( 0,0.7, 0 )

     EndBall1()

    EndBall2()

    EndBall3()

    EquationEnd()

    Equation()

    CreateBall1()

    CreateBall2()

    CreateBall3()

    DisplayLives()

    correct:removeSelf()

end 

local function gameover()

    end

local function resetlives()

    lives = 3

end

local function loseLife()

    lives = lives - 1 

    EndBall1()

    EndBall2()

    EndBall3()

    EquationEnd()

    Equation()

    CreateBall1()

    CreateBall2()

    CreateBall3()

    DisplayLives()

    if lives == 0 then

        resetlives()

        gameover()

    end

end

– If the wrong one is pressed

local function OnTap2(event)

    local incorrect = display.newText(“INCORRECT”, centerX, centerY, native.systemFont, 36 )

    incorrect:setFillColor( 0.7,0, 0 ) 

    EndBall1()

    EndBall2()

    EndBall3()

    EquationEnd()

    Equation()

    CreateBall1()

    CreateBall2()

    CreateBall3()

    DisplayLives()

    loseLife()

    incorrect:removeSelf()

end

local function reset()

    EndBall1()

    EndBall2()

    EndBall3()

    EquationEnd()

    Equation()

    CreateBall1()

    CreateBall2()

    CreateBall3()

    DisplayLives()

end

– If the correct one is pressed

– Restart the Game

– Creating Box 

local function Box()

    local QBox = display.newRect(centerX,centerY,200,75)

    QBox.strokeWidth = 3

    QBox:setFillColor(1)

    QBox:setStrokeColor(0)

end

– Starts the Game

local function GameStart()

  if gameloop == 0 then

    Box()  

    Equation()

    CreateBall1()

    CreateBall2()

    CreateBall3()

    reset()

end

end

local function onCollision(event)

    reset()

    OnTap2()

end

– Loops the game and the event listeners

local function gameLoop(event)

        GameStart()

        gameloop = 1

        ball3:addEventListener(“tap”, OnTap2)

        ball2:addEventListener(“tap”, OnTap2)

        ball1:addEventListener(“tap”, OnTap1)

    end

 – game loop timer

    local timer1 = timer.performWithDelay( tick, gameLoop, 0)

 

  • I am running coronaSDK -2016.2992 on a 64x Windows 10 home PC

Please post using the code button.

OnTap2 is the listener function for two balls. Is that correct?

OnTap1, OnTap2 and reset all call DisplayLives. Is that correct?

Try placing the number of lives 50 pixels lower each time and see what shows up.

The code button that @Horacebury is referring to is the blue <> button in the editing controls with Bold, Italic, etc. Click that, paste your code into the popup window. This is important to help us read your code.

Your problem is in the function DisplayLives(). Each time you call that you are creating a new display text object and drawing it on top of the previous version.  You could make sure to delete the previous object before creating a new one, but that’s very inefficient. 

Instead you should create your text object outside of that function. In fact I would recommend creating two objects:

Somewhere near the top where you declare your variables do:

local DisplayLivesLabel = display.newText("lives: ", screenLeft + 50, screenTop + 50, native.systemFont, 16 ) DisplayLivesLabel:setFillColor(1,0,0) local DisplayLives = display.newText( tostring( lives ), screenLeft + 75, screenTop + 50, native.systemFont, 16 ) DisplayLives:setFillColor(1,0,0)

Now you have an object you can manipulate.

Then in your DisplayLives function do:

local function DisplayLives() DisplayLives.text = tostring(lives) end

Rob

Please post using the code button.

OnTap2 is the listener function for two balls. Is that correct?

OnTap1, OnTap2 and reset all call DisplayLives. Is that correct?

Try placing the number of lives 50 pixels lower each time and see what shows up.

The code button that @Horacebury is referring to is the blue <> button in the editing controls with Bold, Italic, etc. Click that, paste your code into the popup window. This is important to help us read your code.

Your problem is in the function DisplayLives(). Each time you call that you are creating a new display text object and drawing it on top of the previous version.  You could make sure to delete the previous object before creating a new one, but that’s very inefficient. 

Instead you should create your text object outside of that function. In fact I would recommend creating two objects:

Somewhere near the top where you declare your variables do:

local DisplayLivesLabel = display.newText("lives: ", screenLeft + 50, screenTop + 50, native.systemFont, 16 ) DisplayLivesLabel:setFillColor(1,0,0) local DisplayLives = display.newText( tostring( lives ), screenLeft + 75, screenTop + 50, native.systemFont, 16 ) DisplayLives:setFillColor(1,0,0)

Now you have an object you can manipulate.

Then in your DisplayLives function do:

local function DisplayLives() DisplayLives.text = tostring(lives) end

Rob