Not a problem at all; always happy to help!
I’m assuming the issue is that your function isn’t being called, because you have removeEventListener instead of addEventListener.
Hi, sorry I posted the wrong line.
It should be this:
Runtime:addEventListener(“enterFrame”, updateScore )
Can you give a bit more info on your code? What is the gameActive value? Normally an if/then statement looks more like this:
if gameActive == true then
Also, unless you have another function removing your scoreTxt.text object, you’re going to get overlapping objects.
Thats about all the code I have for this.
I thought ‘if gameActive then’ was equivalent to if gameActive == true then
Is that wrong then?
Is that creating a new text object everytime then? Sorry very new to this.
I do have a local reference at the top of the file.
local score = 0
local scoreTxt
You are correct, but I wasn’t sure if that was being defined by gameActive. I have numerical values qualifying my if/then statements.
You might want it to look more like this:
local scoreTxt = display.newText("",0,0,nativeFont, 8) local score = 0 local function updateScore() if gameActive == true then print("SCORE UPDATING") score = score + 5 scoreTxt.text = ("Score: " .. score) scoreTxt:setReferencePoint(display.cl); scoreTxt.x = 5 end end Runtime:addEventListener("enterFrame", updateScore)
Correct, it would create a new text object every time. Can you post a bit more of your code to evaluate?
This is most of the code. The eventlisteners are invoked when the game starts.
local function updateScore(num)
if gameActive then
score = score + num
scoreTxt.text = "Score: " … score
scoreTxt:setReferencePoint(display.cl);
scoreTxt.x = 5
end
end
local function updateDistance(num)
if gameActive then
distanceToTravel = distanceToTravel - speed *2
distanceToTravelTxt.text = "Distance to Travel: " … distanceToTravel
distanceToTravelTxt:setReferencePoint(display.cl);
distanceToTravelTxt.x = 5
updateScore(speed)
end
end
local function pauseGame(event)
if gameActive then
gameActive=false
btnPause:setFrame( 1 )
else
gameActive=true
btnPause:setFrame( 2 )
end
end
function gameListeners(event)
if event == “add” then
Runtime:addEventListener(“accelerometer” , movement )
balloon:addEventListener(“touch” , increaseSpeed )
Runtime:addEventListener(“enterFrame”, updateDistance )
btnPause:addEventListener(“tap”, pauseGame)
elseif event == “remove” then
Runtime:removeEventListener(“accelerometer” , movement )
balloon:removeEventListener(“touch” , increaseSpeed )
Runtime:removeEventListener(“enterFrame”, updateDistance )
btnPause:removeEventListener(“tap”, pauseGame)
end
end
Thanks for looking.
Mark
is the speed variable defined elsewhere in the code?
Yes sorry. At the top of the file
local score = 0
local scoreTxt
local speed = 8
I also have a hud function thats invoked before the listeners:
local hud = function()
utils:log(“play.lua”, “hud()”);
– init score
scoreTxt = display.newText(“Score: 0”, 5, 0, “Helvetica”, 14)
scoreTxt:setTextColor(0, 0, 0, 150)
gameGroup:insert(scoreTxt)
– init distance
distanceToTravelTxt = display.newText("Distance to Travel: ", 5, 0, “Helvetica”, 14)
distanceToTravelTxt:setTextColor(0, 0, 0, 150)
distanceToTravelTxt.y = screenBottom - 12
gameGroup:insert(distanceToTravelTxt)
end
Am i duplicating object then with this?
Thanks.
I’m not that familiar with the “remove” and “add” events you’ve used in your gamelisteners function, nor do I see where the gamelisteners function is being invoked. Was that also left off?
Sorry its quite a big file.
This is what invokes it:
local function gameStart()
hud()
initPause()
initButtons()
end
gameStart()
gameListeners(“add”)
gameListeners(“remove”) is used in the exit scene.
Thanks,
Mark
You paste parts of the code which are dependent. If you can it would be better to post whole file (if not longer than tens of lines)
It is certainly possible that I don’t know enough about lua syntax to help you any further, so keep that in mind when I make my suggestions:
I don’t believe an (event) function will fire unless listening to one of the properties associated with it. Maybe you can have and event function with a property of just “event == ‘add’”, but I haven’t found it.
You might be better served by creating separate add and remove functions for your Runtime listeners. It might be a bit more intuitive to make something like this:
function gameListenersON() Runtime:addEventListener("accelerometer" , movement ) balloon:addEventListener("touch" , increaseSpeed ) Runtime:addEventListener("enterFrame", updateDistance ) btnPause:addEventListener("tap", pauseGame) end function gameListenersOFF() Runtime:removeEventListener("accelerometer" , movement ) balloon:removeEventListener("touch" , increaseSpeed ) Runtime:removeEventListener("enterFrame", updateDistance ) btnPause:removeEventListener("tap", pauseGame) end
And then you can just call each when necessary. Again, I’m no Lua expert but this might be the best way to start.
ok thanks. Going back to my original question then
Maybe all I need to do is to stop the listener below when gameActive == false. As this only needs to run when gameActive is true.
Runtime:removeEventListener(“enterFrame”, updateScore )
Thanks for your help.
Mark
It looks like updateScore is only being called in the updateDistance Runtime function, so if you remove updateDistance, it should remove updateScore.
Oh yes i see that now. Thanks for helping.
Regards,
Mark
Quick postscript to this that I didn’t notice earlier…
You don’t need to explicitly remove touch/tap functions that are associated with objects, such as
balloon:removeEventListener("touch" , increaseSpeed )
Because if you are removing the balloon object at the end of the level/game, it will also remove the associated listeners. That should make things a bit more simple as well.
Brilliant, thanks for that. I didn’t know that. I just need to check I’m removing the object! I’ve only posted a few times so apologies if I caused you an headache.