just a small question i how do i make a button that removes even listeners and pause physics
To remove listeners and pause physics you would just make a function that does those things and add a button event listener to that function.
When the game starts the isPlay is set to true so when you press the button it see’s that the game is in play and pauses the game and sets isPlay to false. Now the next time you press the button it see’s that isPlay is false and starts the game back up.
local isPlay = true local function pauseFunction(event) if event.phase == "ended" then if isPlay == true then local isPlay = false --Do things like pause physics here. elseif isPlay == false then local isPlay = true --Do things like start physics here. end end end button:addEventListener( "touch", pauseFunction )
–SonicX278
Your function won’t work SonicX278, because you have used the keyword “local” inside the function, therefore creating new variables called “isPlay”, rather than setting the value of the existing one.
local isPlay = true local function pauseFunction(event) if event.phase == "ended" then if isPlay == true then local isPlay = false --wrong isPlay = false --correct --Do things like pause physics here. elseif isPlay == false then local isPlay = true --wrong isPlay = true --correct end end end button:addEventListener( "touch", pauseFunction )
That can be simplified even further to a function which toggles the bool between true and false:
local function pauseFunction(event) if event.phase == "ended" then isPlay = not isPlay end end
In order to actually pause your game, you could either remove event listeners in the pauseFunction when isPlay is true, or you could use the isPlay bool inside your listeners to control what they do. You will also need to pause and resume any physics, luckily there are functions built into the physics engine for that:
local isPlay = true local function pauseFunction(event) if event.phase == "ended" then isPlay = not isPlay if isPlay then physics.resume() else physics.pause() end end end button:addEventListener( "touch", pauseFunction ) local function myEnterFrameListener(event) if isPlay then animateEnemies() increaseScore() doSomeOtherStuff() else --do nothing end end Runtime:addEventListener("enterFrame", myEnterFrameListener)
That might be a bit crude, but it should give you somewhere to start.
Ahh sorry. I was in a bit of a rush.
–SonicX278
To remove listeners and pause physics you would just make a function that does those things and add a button event listener to that function.
When the game starts the isPlay is set to true so when you press the button it see’s that the game is in play and pauses the game and sets isPlay to false. Now the next time you press the button it see’s that isPlay is false and starts the game back up.
local isPlay = true local function pauseFunction(event) if event.phase == "ended" then if isPlay == true then local isPlay = false --Do things like pause physics here. elseif isPlay == false then local isPlay = true --Do things like start physics here. end end end button:addEventListener( "touch", pauseFunction )
–SonicX278
Your function won’t work SonicX278, because you have used the keyword “local” inside the function, therefore creating new variables called “isPlay”, rather than setting the value of the existing one.
local isPlay = true local function pauseFunction(event) if event.phase == "ended" then if isPlay == true then local isPlay = false --wrong isPlay = false --correct --Do things like pause physics here. elseif isPlay == false then local isPlay = true --wrong isPlay = true --correct end end end button:addEventListener( "touch", pauseFunction )
That can be simplified even further to a function which toggles the bool between true and false:
local function pauseFunction(event) if event.phase == "ended" then isPlay = not isPlay end end
In order to actually pause your game, you could either remove event listeners in the pauseFunction when isPlay is true, or you could use the isPlay bool inside your listeners to control what they do. You will also need to pause and resume any physics, luckily there are functions built into the physics engine for that:
local isPlay = true local function pauseFunction(event) if event.phase == "ended" then isPlay = not isPlay if isPlay then physics.resume() else physics.pause() end end end button:addEventListener( "touch", pauseFunction ) local function myEnterFrameListener(event) if isPlay then animateEnemies() increaseScore() doSomeOtherStuff() else --do nothing end end Runtime:addEventListener("enterFrame", myEnterFrameListener)
That might be a bit crude, but it should give you somewhere to start.
Ahh sorry. I was in a bit of a rush.
–SonicX278