Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Incrementing Integer
Started by shaqaruden Aug 07 2013 08:16 AM

- - - - -
6 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

shaqaruden

[GLOBAL: userInfoPane.html]
shaqaruden
  • Observer

  • 6 posts
  • Corona SDK

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?

 



[TOPIC: post.html]
#2

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Corona Geek

  • 1,077 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

shaqaruden

[GLOBAL: userInfoPane.html]
shaqaruden
  • Observer

  • 6 posts
  • Corona SDK

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)


[TOPIC: post.html]
#4

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Corona Geek

  • 1,077 posts
  • Corona SDK

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)


[TOPIC: post.html]
#5

shaqaruden

[GLOBAL: userInfoPane.html]
shaqaruden
  • Observer

  • 6 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Corona Geek

  • 1,077 posts
  • Corona SDK

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


[TOPIC: post.html]
#7

shaqaruden

[GLOBAL: userInfoPane.html]
shaqaruden
  • Observer

  • 6 posts
  • Corona SDK

ahhhh Ok I see now thanks!




[topic_controls]
[/topic_controls]