Score table and touch is not working in my game. Also It is not showing any error- Corona SDK version 3.0

Score table and touch is not working in my game. Also It is not showing any error- Corona SDK version 3.0. Would you please help me to find an error

[lua]


– main.lua


local background = display.newImage(“clouds.jpg”)

background:scale(2, 2)

display.setStatusBar(display.HiddenStatusBar)

local physics = require(“physics”);

local physics = require(“physics”);

physics.setDrawMode(“normal”)

system.activate(“multitouch”);

_H = display.contentHeight;

_W = display.contentWidth;

balloons = 0;

numBalloons = 100;

startTime = 20;

totalTime = 20;

timeLeft = true;

playerReady = false;

Random = math.random;

local music = audio.loadStream(“music.mp3”);

local balloonPop = audio.loadSound(“balloonPop.mp3”);

local screenText = display.newText("…Loading Balloons…", 0, 0, native.systemFont, 16*2);

screenText.xScale = 2

screenText.yScale = 2;

screenText.anchorX = 0.5;

screenText.anchorY = 0.5

– Place the text on screen

screenText.x = _W / 2 - 210;

screenText.y = _H - 20;

local timeText = display.newText("Time: "…startTime, 0, 0, native.systemFont, 16*2);

timeText.xScale = 0.5;

timeText.yScale = 0.5;

timeText.anchorX = 0.5;

timeText.anchorY = 0.5;

timeText.x = _W / 2;

timeText.y = _H - 20;

physics.start()

physics.setGravity(0, -0.4)

local function startGame()

  local myBalloon = display.newImageRect(“balloon.png”, 55, 55);

  --myBalloon:setReferencePoint(display.CenterReferencePoint);

  myBalloon.anchorX = 2;

  myBalloon.anchorY = 2;

  myBalloon.x = Random(50, _W-50);

  myBalloon.y = (_H+10);

  physics.addBody(myBalloon, “dynamic”, {density=0.1, friction=0.0, bounce=0.9, radius=10});

end

gameTimer = timer.performWithDelay(20, startGame, numBalloons);

local leftWall = display.newRect (0, 0, 1, display.contentHeight);

local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight);

local ceiling = display.newRect (0, 0, display.contentWidth, 1);

physics.addBody (leftWall, “static”,  { bounce = 0.1 } );

physics.addBody (rightWall, “static”, { bounce = 0.1 } );

physics.addBody (ceiling, “static”,   { bounce = 0.1 } );

local gameTimer;

– Did the player win or lose the game?

local function gameOver(condition)

  – If the player pops all of the balloons they win

  if (condition == “winner”) then

    screenText.text = “Amazing!”;

    – If the player pops 70 or more balloons they did okay

  elseif (condition == “notbad”) then

    screenText.text = “Not too shabby.”

    – If the player pops less than 70 balloons they didn’t do so well

  elseif (condition == “loser”) then

    screenText.text = “You can do better.”;

  end

end

– Remove balloons when touched and free up the memory they once used

local function removeBalloons(obj)

  obj:removeSelf();

  – Subtract a balloon for each pop

  balloons = balloons - 1;

  – If time isn’t up then play the game

  if (timeLeft ~= false) then

    – If all balloons were popped

    if (balloons == 0) then

      timer.cancel(gameTimer);

      gameOver(“winner”)

    elseif (balloons <= 30) then

      gameOver(“notbad”);

    elseif (balloons >=31) then

      gameOver(“loser”);

    end

  end

end

local function countDown(e)

  – When the game loads, the player is ready to play

  if (startTime == totalTime) then

    – Loop background music

    audio.play(music, {loops =- 1});

    playerReady = true;

    screenText.text = “Hurry!”

  end

  – Subtract a second from start time

  startTime = startTime - 1;

  timeText.text = "Time: "…startTime;

  – If remaining time is 0, then timeLeft is false

  if (startTime == 0) then

    timeLeft = false;

  end

end

local myBalloon = display.newImage(“balloon.png”)

– Allow the user to touch the balloons

function touch(e)

  – If time isn’t up then play the game

  if (timeLeft ~= false) then

    – If the player is ready to play, then allow the balloons to be popped

    if (playerReady == true) then

      if (e.phase == “ended”) then

        – Play pop sound

        audio.play(balloonPop);

        – Remove the balloons from screen and memory

        removeBalloons(self);

      end

    end

  end

end

– Increment the balloons variable by 1 for each balloon created

balloons = balloons + 1;

– Add event listener to balloon

myBalloon:addEventListener(“touch”, myBalloon);

if (balloons == numBalloons) then

  gameTimer = timer.performWithDelay(1000, countDown, totalTime);

else

  – Make sure timer won’t start until all balloons are loaded

  playerReady = false;

end

[/lua]

There’s a few mistakes here.

  1. You are creating balloons in two different places - in startGame() and around line 110-130. Remove the code to create balloons outside of startGame.

  2. line 127 is useless - this code will only ever be run once. It needs to be put into startGame.

  3. You only add a touchListener to the one orphan balloon created in line 110. You need to add the touch listener in startGame to each new balloon.

  4. The way you are adding the touch listener is wrong. It should be:

[lua]

myBalloon:addEventListener(“touch”, touch)

[/lua]

  1. removeBalloons(self) needs to be changed to removeBalloons(e.target).

  2. The code in lines 131-136 will only run once before any balloons are created, so playerReady will never evaluate to true. This needs to be inside of an enterFrame listener or function fired continuously by a timer.

These changes should get you closer to a working game, albeit with some bad practise such as unnecessary global variables and functions.

I think this shows the danger of coding all the logic blindly without checking each step works as intended first. Read and re-read code, work out what each line is doing and whether that matches your intention. If you ask the computer to do something bonkers but with correct syntax, it will do it. No errors doesn’t necessarily mean the program works.

For example, before coding what should happen when a balloon is touched, just print ‘touched’ to the log in the listener to check it’s even being fired. That way you would have noticed something was wrong and more importantly, because you checked everything else was working first, you know it’s the new code you put in, not something you wrote an hour or even a week ago.

There’s a few mistakes here.

  1. You are creating balloons in two different places - in startGame() and around line 110-130. Remove the code to create balloons outside of startGame.

  2. line 127 is useless - this code will only ever be run once. It needs to be put into startGame.

  3. You only add a touchListener to the one orphan balloon created in line 110. You need to add the touch listener in startGame to each new balloon.

  4. The way you are adding the touch listener is wrong. It should be:

[lua]

myBalloon:addEventListener(“touch”, touch)

[/lua]

  1. removeBalloons(self) needs to be changed to removeBalloons(e.target).

  2. The code in lines 131-136 will only run once before any balloons are created, so playerReady will never evaluate to true. This needs to be inside of an enterFrame listener or function fired continuously by a timer.

These changes should get you closer to a working game, albeit with some bad practise such as unnecessary global variables and functions.

I think this shows the danger of coding all the logic blindly without checking each step works as intended first. Read and re-read code, work out what each line is doing and whether that matches your intention. If you ask the computer to do something bonkers but with correct syntax, it will do it. No errors doesn’t necessarily mean the program works.

For example, before coding what should happen when a balloon is touched, just print ‘touched’ to the log in the listener to check it’s even being fired. That way you would have noticed something was wrong and more importantly, because you checked everything else was working first, you know it’s the new code you put in, not something you wrote an hour or even a week ago.