当我按下运行程序时,我的游戏加载但它不会启动

时间:2016-05-04 12:04:22

标签: ios xcode swift

我正在使用本教程:https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor

当我在Xcode中按下运行程序时,模拟器加载游戏并弹出显示游戏的第一帧但它被冻结。当我点击时,玩家精灵应该移动到我点击的位置,AI精灵应该尝试抓住玩家,但没有任何反应。点击不起作用,我试过让它静置一段时间,看它是否还没有完成加载或其他东西,但是也没有用。

到目前为止,该程序的所有代码都在这里:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

  let playerSpeed: CGFloat = 150.0
  let zombieSpeed: CGFloat = 75.0

  var goal: SKSpriteNode?
  var player: SKSpriteNode?
  var zombies: [SKSpriteNode] = []

  var lastTouch: CGPoint? = nil

  override func didMoveToView(view: SKView) {
    physicsWorld.contactDelegate = self
    updateCamera()
  }

  override func touchesBegan(touches: Set<UITouch>, withEvent event:      UIEvent?) {
    handleTouches(touches)
  }

  override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    handleTouches(touches)
  }

  override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    handleTouches(touches)
  }

  private func handleTouches(touches: Set<UITouch>) {
    for touch in touches {
      let touchLocation = touch.locationInNode(self)
      lastTouch = touchLocation
    }
  }

  override func didSimulatePhysics() {
    if let _ = player {
      updatePlayer()
      updateZombies()
    }
  }

  private func shouldMove(currentPosition currentPosition: CGPoint, touchPosition: CGPoint) -> Bool {
    return abs(currentPosition.x - touchPosition.x) > player!.frame.width / 2 ||
      abs(currentPosition.y - touchPosition.y) > player!.frame.height/2
  }

  func updatePlayer() {
    if let touch = lastTouch {
      let currentPosition = player!.position
      if shouldMove(currentPosition: currentPosition, touchPosition: touch) {

        let angle = atan2(currentPosition.y - touch.y, currentPosition.x - touch.x) + CGFloat(M_PI)
        let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0)

        player!.runAction(rotateAction)

        let velocotyX = playerSpeed * cos(angle)
        let velocityY = playerSpeed * sin(angle)

        let newVelocity = CGVector(dx: velocotyX, dy: velocityY)
        player!.physicsBody!.velocity = newVelocity;
        updateCamera()
      } else {
        player!.physicsBody!.resting = true
      }
    }
  }

  func updateCamera() {
    if let camera = camera {
      camera.position = CGPoint(x: player!.position.x, y: player!.position.y)
    }
  }

  func updateZombies() {
    let targetPosition = player!.position

    for zombie in zombies {
      let currentPosition = zombie.position

      let angle = atan2(currentPosition.y - targetPosition.y, currentPosition.x - targetPosition.x) + CGFloat(M_PI)
      let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0.0)
      zombie.runAction(rotateAction)

      let velocotyX = zombieSpeed * cos(angle)
      let velocityY = zombieSpeed * sin(angle)

      let newVelocity = CGVector(dx: velocotyX, dy: velocityY)
      zombie.physicsBody!.velocity = newVelocity;
    }
  }

  func didBeginContact(contact: SKPhysicsContact) {
    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
      firstBody = contact.bodyA
      secondBody = contact.bodyB
    } else {
      firstBody = contact.bodyB
      secondBody = contact.bodyA
    }

    if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == zombies[0].physicsBody?.categoryBitMask {
        gameOver(false)
    } else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == goal?.physicsBody?.categoryBitMask {
        gameOver(true)
    }

    player = self.childNodeWithName("player") as? SKSpriteNode

    for child in self.children {
        if child.name == "zombie" {
            if let child = child as? SKSpriteNode {
                zombies.append(child)
            }
        }
    }
    goal = self.childNodeWithName("goal") as? SKSpriteNode
  }

  private func gameOver(didWin: Bool) {
    print("- - - Game Ended - - -")
    let menuScene = MenuScene(size: self.size)
    menuScene.soundToPlay = didWin ? "fear_win.mp3" : "fear_lose.mp3"
    let transition = SKTransition.flipVerticalWithDuration(1.0)
    menuScene.scaleMode = SKSceneScaleMode.AspectFill
    self.scene!.view?.presentScene(menuScene, transition: transition)
  }
}

程序中完成的其他事情,比如添加精灵等,都是在GameScene.sks中完成的,所以没有代码。

1 个答案:

答案 0 :(得分:1)

您的设置代码位置错误。将其从didBeginContact移至didMoveToView

player = self.childNodeWithName("player") as? SKSpriteNode
for child in self.children {
    if child.name == "zombie" {
        if let child = child as? SKSpriteNode {
            zombies.append(child)
        }
    }
}
goal = self.childNodeWithName("goal") as? SKSpriteNode

至少我在将您的代码与教程中的示例项目进行比较时看到了差异。