我正在使用 Swift SpriteKit 开发游戏。一切都很顺利,但唯一让我害怕的是检测到游戏结束。我的游戏如下图所示,它是一款自上而下的游戏。
棕色是木头
,蓝色(看起来像水)是河流
。如果玩家踩到河流而不是木头,那么游戏就结束了。
问题是我怎样才能检测到它踏上了河流。我尝试使用 SpriteKit Contact,它是 func didBeginContact
。但它只在启动时被调用,之后它不再调用,即使我踩到它。
最佳答案
我想到了两种可能的方法。
1。 在您的 touchesBegan 中,您会执行此操作。
NOTE: This will not work if the player is able to Fall into the river without interaction. In addition to that it calls Game Over a bit to early.
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if wood.containsPoint(location) {
} else {
// Call Game Over func
}}
2。 在你的更新函数中(这应该一直有效)
override func update(currentTime: NSTimeInterval) {
super.update(currentTime)
if wood.containsPoint(Player.position) {
} else {
// Call Game Over func
}}
UPDATE: The code should be working
关于swift - 如何在 SpriteKit 的自上而下 View 中检测游戏结束无尽的 runner 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022561/