Swift 2:使用SKAction时崩溃

时间:2016-02-11 17:21:06

标签: swift sprite-kit

当我尝试运行此代码时,我收到此错误:

CUICatalog:无效请求:请求子类型而不指定习语 致命错误:在解包可选值时意外发现nil

任何人都知道如何解决这个问题?

override func didMoveToView(view: SKView) {
 _ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("makeEnemiesTowards"), userInfo: nil, repeats: true)
}

func makeEnemiesTowards() {

    let spawnEnemiesTowards = SKAction.waitForDuration(0.5)
    let removeEnemiesTowards = SKAction.removeFromParent()
    let moveAndRemoveEnemiesTowards = SKAction.sequence([spawnEnemiesTowards, removeEnemiesTowards])

    let enemyTowardsTexture = SKTexture(imageNamed: "player")
    let enemyTowards = SKSpriteNode(texture: enemyTowardsTexture)
    enemyTowards.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    enemyTowards.physicsBody!.affectedByGravity = false
    enemyTowards.zPosition = 5
    enemyTowards.runAction(spawnEnemiesTowards)

    self.addChild(enemyTowards)

}

2 个答案:

答案 0 :(得分:2)

我认为这不是问题,但是在SpriteKit中,如果你想永远调用一个方法,你应该使用NSTimerSKAction

用此替换didMoveToView

override func didMoveToView(view: SKView) {
    let addEnemy = SKAction.runBlock { [weak self] in self?.makeEnemiesTowards() }
    let wait = SKAction.waitForDuration(1)
    let sequence = SKAction.sequence([addEnemy, wait])
    SKAction.repeatActionForever(sequence)
}

答案 1 :(得分:1)

所以基本上错误是因为你试图打开一个可选类型意味着某些东西(String?)。但它是零,所以这是不可能的。

我相信这是导致你失败​​的一条线。 physicsBody很可能是null。   enemyTowards.physicsBody!.affectedByGravity = false

希望有所帮助