Hope someone can help because it’s driving me nuts. When I run MrPig and collect a coin then die and the game resets. It truly does not reset because I have to wait for the collect coin timer to finish before the coin will reappear. I can figure out how to remove that node when collected so it doesn’t regenerate but if I die the coin doesn’t come back at all. How can I change this so the coin can only be collect once per game and truly reset its position when I die and restart? Thanks in advance for any help. By the way I have very little experience so please take it easy on me if this is super simple. Thanks
@chrislanguage can you take a look at the question above?
Hi there,
The auto re-spawn was actually a feature added in order to make the game “endless”. :] All you need to do is iterate through the coins under the Coins node, and set all the hidden coins back to visible.
Add the following function to the ViewController.swift class:
func resetCoins() {
let coinsNode = gameScene.rootNode.childNode(withName: "Coins", recursively: true)!
for node in coinsNode.childNodes {
for child in node.childNodes {
child.isHidden = false
}
}
}
Make sure to add a call to resetCoins() inside startGame() so that all hidden coins are made visible again before the game starts again.
Hope that helps!
@chrislanguage Thanks for taking the time to look at this for me. However, that function didn’t work. I thought maybe I was missing something on my version of MrPig so I opened the final build from chapter 23 and plugged it in, and it still does not reset the coins. Not sure if it makes a difference where inside the startGame() function I make the call to resetCoins() but I tried, first, last and in the middle all with the same result. The coins just do not reappear. Any other thoughts?
(the reappear after the timer as intended, just not immediately after game restart.)
Hi there,
Not that I think it will make a difference, but I added the function to the FINAL project from Chapter 24. I’ve included a screenshot of my code, maybe it will make more sense?
Good luck!
Hey Chris,
Thank you so much for taking the time to try and help me out with this… Not exactly sure what I am doing different than you that it works for you but not me but still no go. Copied everything exactly the same in the chapter 24 build. I even made sure to copy the function right before the startGame() function and called the resetCoins() function first just like you did and still doesn’t work. Now it resets the coins if the game is completely close and restarted but it always worked that way. It’s just after the game over and it returns to the splash screen that it doesn’t work. Anyways, I don’t really expect any other solution seeings that this one works for you but not me. Again, I appreciate you even taking the time to try and help!
Thanks,
Nelson
Thank you @chrislanguage for taking care of this one - much appreciated! :]