Incrementing Integer

I have a event listener that listens for tap events on an object. When the event happens the score is suppose to increment by 1 (1,2,3,4,5,6,7,8,9 and so on). I also have a time that counts down to 0 from 60 and when score changes it doesn’t increase by 1 it seems to increase by the number that has been subtracted from the timer.

For example:

Tap at 59 seconds = 1 point

Tap st 50 seconds = 10 points

etc.

What puzzles me is when the object is tapped this is what SHOULD happen to the score.

score = score + 1 

which should increment only by 1 each time.

Whats going on here?

You’ll have to show us your code so we can help

local screenWidth = display.contentWidth local screenHeight = display.contentHeight local circleScale = 75 local xPos = math.random(circleScale \* 1.5, screenWidth - circleScale \* 1.5) local yPos = math.random(100 + circleScale \* 1.5, screenHeight - circleScale \* 1.5) local Red = math.random(0, 255) local Green = math.random(0, 255) local Blue = math.random(0, 255) local timeLimit = 60 local timeLeft = display.newText(timeLimit, 160, 100, native.systemFontBold, 50) local score = 0 local scoreText = display.newText(score, 450, 100, native.systemFontBold, 50) local circle = display.newCircle( xPos, yPos, circleScale ) circle:setFillColor(Red, Green, Blue) function circleTouched(event) if (timeLimit ~= 0) then circle.x = math.random(circleScale \* 1.5, screenWidth - circleScale \* 1.5) circle.y = math.random(100 + circleScale \* 1.5, screenWidth - circleScale \* 1.5) circle:setFillColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) end end function countScore(event) score = score + 1 --native.showAlert("Score", score, { "OK\_Btn\_Text", "Dismiss\_Btn\_Text", listener }) scoreText.text = score end local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" end circle:addEventListener("tap", circleTouched) circle:addEventListener("tap", countScore) end timer.performWithDelay(1000,timerDown,timeLimit)

You placed your addEventListener code within a function that is being called every 1 second by the timer.  Change this:

local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" end circle:addEventListener("tap", circleTouched) circle:addEventListener("tap", countScore) end timer.performWithDelay(1000,timerDown,timeLimit)

To this:

local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" end end circle:addEventListener("tap", circleTouched) circle:addEventListener("tap", countScore) timer.performWithDelay(1000,timerDown,timeLimit)

In doing that won’t the touch events work even though the time ran out?

Yes.  That’s why it’s your job to remove them.  

local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" circle:removeEventListener("tap", circleTouched) circle:removeEventListener("tap", countScore) end end

ahhhh Ok I see now thanks!

You’ll have to show us your code so we can help

local screenWidth = display.contentWidth local screenHeight = display.contentHeight local circleScale = 75 local xPos = math.random(circleScale \* 1.5, screenWidth - circleScale \* 1.5) local yPos = math.random(100 + circleScale \* 1.5, screenHeight - circleScale \* 1.5) local Red = math.random(0, 255) local Green = math.random(0, 255) local Blue = math.random(0, 255) local timeLimit = 60 local timeLeft = display.newText(timeLimit, 160, 100, native.systemFontBold, 50) local score = 0 local scoreText = display.newText(score, 450, 100, native.systemFontBold, 50) local circle = display.newCircle( xPos, yPos, circleScale ) circle:setFillColor(Red, Green, Blue) function circleTouched(event) if (timeLimit ~= 0) then circle.x = math.random(circleScale \* 1.5, screenWidth - circleScale \* 1.5) circle.y = math.random(100 + circleScale \* 1.5, screenWidth - circleScale \* 1.5) circle:setFillColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) end end function countScore(event) score = score + 1 --native.showAlert("Score", score, { "OK\_Btn\_Text", "Dismiss\_Btn\_Text", listener }) scoreText.text = score end local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" end circle:addEventListener("tap", circleTouched) circle:addEventListener("tap", countScore) end timer.performWithDelay(1000,timerDown,timeLimit)

You placed your addEventListener code within a function that is being called every 1 second by the timer.  Change this:

local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" end circle:addEventListener("tap", circleTouched) circle:addEventListener("tap", countScore) end timer.performWithDelay(1000,timerDown,timeLimit)

To this:

local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" end end circle:addEventListener("tap", circleTouched) circle:addEventListener("tap", countScore) timer.performWithDelay(1000,timerDown,timeLimit)

In doing that won’t the touch events work even though the time ran out?

Yes.  That’s why it’s your job to remove them.  

local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timeLeft.text = "Times Up" circle:removeEventListener("tap", circleTouched) circle:removeEventListener("tap", countScore) end end

ahhhh Ok I see now thanks!