I tried to make something that allows you to reset your game. I tried to make it but the button would not reset. No error message in console
resetButton = display.newImage("image/reset.png")
resetButton.x = 300
resetButton.y = 512
resetButton.isVisible = false
resetButton.alpha = 0
resetButton.scene = "menu"
function changeScene(e)
if(e.phase == "ended") then
director:changeScene(e.target.scene)
end
end
resetButton:addEventListener("touch", changeScene)
local onCollision = function (self, event)
if event.phase == "began" then
if event.other.name == "spikes" then
resetButton.isVisible = true
transition.to(resetButton, {time=500, alpha = 1})
self:removeSelf()
self = nil
end
end
end
that is my code. How would I make the reset work. I am trying tutoials but nting is wokring [import]uid: 55737 topic_id: 10044 reply_id: 310044[/import]
Did you define that somewhere in your code?
If you’re trying to reset the game/level then I have two ways of doing things that I can suggest to you.
create a function that will reset all the variables back to
their initial values.
example:
[lua]-- initial values
local gameScore = 0
local bullet = 20
local life = 5
– this is the reset function, call it when you want to reset game
local resetGame = function ()
gameScore = 0
bullet = 20
life = 0
– etc…
end
– the touch function
local resetTouch = function (e)
if e.phase == “ended” then
– call reset function
resetGame()
end
end[/lua]
if you’re using director class ver. 1.2 then you will need to create a loading.lua
example:
level1.lua --> loading.lua --> level1.lua
[lua]local resetTouch = function (e)
if e.phase == “ended” then
– change to loading
director:changeScene(“loading”, “crossfade”)
end
end[/lua]
the harder part is inside your loading.lua
You might have to create a global variable to store the scene you loaded from so loading.lua can load back to that same level.
I hope this make sense. [import]uid: 12455 topic_id: 10044 reply_id: 36628[/import]
Going based on teex84’s reply, I used the 2nd method. I have a database in my game so I created a new column called “currentlevel” and I just wrote which level I was on to that column, then I transitioned to a temporary screen (loading.lua). In loading.lua I would read that value from the database then transition back to that level. The only way I got this to work was to put a very small delay in the loading.lua before transitioning back to my level.
I tried the first method he mentioned and I could never get it working properly. [import]uid: 31262 topic_id: 10044 reply_id: 36636[/import]