compare scores

Hello again to fellow corona users

i’ve declare “local score = 0” and add some score to it by button and i need to fire some function if score reaches, lets say, 5…

here is my code, reckon what i did wrong… its seems to be easy, but i think i missed something

[lua]local score = 0

local btn = display.newRect(150,150,30,30)

local function addscore(event)
if(event.phase == “ended”) then
score = score + 1
end
end

function iffive()
if score > 5 then
print(“5”)
end
end

btn:addEventListener(“touch”, addscore)
iffive()[/lua]

any help appreciated
[import]uid: 16142 topic_id: 12418 reply_id: 312418[/import]

You can do it two ways. You can add an event listener or you can add timer.

[lua] local score = 0;

local btn = display.newRect(150,150,30,30)

local function addscore(event)
if(event.phase == “ended”) then
score = score + 1
print(“I work!”)
end
end

local function five()
if score >= 5 then
print(“5”)
end
end

btn:addEventListener(“touch”, addscore)
–Add an event listener
Runtime:addEventListener(“enterFrame”, five)
–Or a timer
–timer.performWithDelay(100, five, 0);
[import]uid: 17138 topic_id: 12418 reply_id: 45286[/import]

The Third Way…

[lua]local score = 0
local btn = display.newRect(150,150,30,30)

local function gotFive()
print(“5”)
end

local function addscore(event)
if(event.phase == “ended”) then
score = score + 1
if score > 5 then
gotFive()
end
end
end

btn:addEventListener(“touch”, addscore)[/lua] [import]uid: 71210 topic_id: 12418 reply_id: 45311[/import]

Pshhhh…Mine are better :wink: . Haha [import]uid: 17138 topic_id: 12418 reply_id: 45314[/import]

Khaodik,
Do you really think so ?

:stuck_out_tongue:

[import]uid: 71210 topic_id: 12418 reply_id: 45322[/import]

No, Haha mine both hog more, already limited ram. [import]uid: 17138 topic_id: 12418 reply_id: 45332[/import]

@khaodik,
the method that you are using seems like ActionScript, where the enterFrame is used, Why would anyone use an enterFrame to increase score?

This method seems similar to programming prior to event based programming, where you had to have an infiniteloop as the heartbeat, checking everything.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12418 reply_id: 45336[/import]

@Jayantv
You are completely right. I don’t know what I was thinking… [import]uid: 17138 topic_id: 12418 reply_id: 45344[/import]

For clarity, using the enterFrame method is perfectly valid if you are creating a “game loop”. enterFrame fires every frame and is useful for all things that need to be monitored every frame.

Whether the checking of the score for the target value needs to be done every frame is personal choice (I don’t think it does).

I would go with the method outlined by @technowand. [import]uid: 5317 topic_id: 12418 reply_id: 45347[/import]

thats all very helpful, thank you very much [import]uid: 16142 topic_id: 12418 reply_id: 45387[/import]