UberJump教程SpriteKit错误

时间:2016-06-27 13:47:05

标签: ios swift sprite-kit swift2

我正在使用Ray Wenderlich的UberJump教程(https://www.raywenderlich.com/87232/make-game-like-mega-jump-sprite-kit-swift-part-2),我遇到了一个逻辑错误。用于删除对象的代码不起作用,我无法弄清楚原因。我下载了他的源代码,他的代码也没有用。

以下是我遇到的问题:

     override func update(currentTime: NSTimeInterval) {

    //new max height?
    //1
    if Int(player.position.y) > maxPlayerY {
        //2
        GameState.sharedInstance.score += Int(player.position.y) - maxPlayerY!
        //3
        maxPlayerY = Int(player.position.y)
        //4
        lblScore.text = String(format: "%d", GameState.sharedInstance.score)
    }


    // Remove game objects that have passed by
    foregroundNode.enumerateChildNodesWithName("NODE_PLATFORM", usingBlock: {
        (node, stop) in
        let platform = node as! PlatformNode
        platform.checkNodeRemoval(self.player.position.y)
    })

    foregroundNode.enumerateChildNodesWithName("NODE_STAR", usingBlock: {
        (node, stop) in
        let star = node as! StarNode
        star.checkNodeRemoval(self.player.position.y)
    })


    //calculate player y offset

    if player.position.y > 200.0 {
        backgroundNode.position = CGPoint(x: 0.0, y: -((player.position.y -   200.0)/10))
        midgroundNode.position = CGPoint(x: 0.0, y: -((player.position.y - 200.0)/4))
        foregroundNode.position = CGPoint(x: 0.0, y: -(player.position.y-200.0))
    }




}

“删除已经过的游戏对象”代码无效。它应该在角色跳跃时移除平台。我写错了代码吗?感谢。

编辑:这里也是checkNodeRemovalFunction。

class GameObjectNode: SKNode {
func collisionWithPlayer(player: SKNode) -> Bool {
    // Award score

    return false
}

func checkNodeRemoval(playerY: CGFloat) {
    if playerY > self.position.y + 300.0 {
        self.removeFromParent()
    }
}

1 个答案:

答案 0 :(得分:0)

您的checkNodeRemoval功能似乎存在问题,具体取决于您从GameObjectNode继承的方式。您可以尝试的是取出checkNodeRemoval功能并直接检查更新方法。

// Remove game objects that have passed by
foregroundNode.enumerateChildNodesWithName("NODE_PLATFORM", usingBlock: {
    (node, stop) in
    print("found platform node")
    if self.player.position.y > node.position.y + 300.0 {
        print("attempt to remove platform")
        platform.removeFromParent()
    }
})

foregroundNode.enumerateChildNodesWithName("NODE_STAR", usingBlock: {
    (node, stop) in
   print("found star node")
    if self.player.position.y > node.position.y + 300.0 {
        print("attempt to remove star")
        star.removeFromParent()
    }
})