swift - 如何在 SpriteKit 的自上而下 View 中检测游戏结束无尽的 runner 游戏

标签 swift sprite-kit collision-detection game-physics

我正在使用 Swift SpriteKit 开发游戏。一切都很顺利,但唯一让我害怕的是检测到游戏结束。我的游戏如下图所示,它是一款自上而下的游戏。 enter image description here

棕色是木头蓝色(看起来像水)是河流。如果玩家踩到河流而不是木头,那么游戏就结束了。

问题是我怎样才能检测到它踏上了河流。我尝试使用 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/

相关文章:

arrays - 将非可选数组转换为可选数组的简单方法

ios - 如何获取特定时间的 UTC 时间戳?

ios - Swift 中 SKScene 中的社交框架

ios - SpriteKit 和 Swift : Creating nodes via didBeginContact messes up the positioning

javascript - 如何获得发生碰撞的直线的百分比?

objective-c - 如何检测两个图像的碰撞?

c++ - 在D3D11中锁定

ios - 渐变层在 iOS 中不起作用

ios - 更改 JSQMessagesViewController 的框架

swift - SKAction 运行操作中的代码未执行