これは、どのような任意の値について話しているのかわかりません。私は自分のスコア整数をチェックし、敵と接触するまでその値が0であることを宣言したことを確認した。シミュレータでは、カウンタは最初の4つまたは5つの敵をカウントし、クラッシュします。非オプションとデフォルトとして0を設定するなど致命的なエラー:予期せぬことに物理的な接触のオプション値をアンラッピングしている間にゼロが見つかりました
var score = Int?()
var scoreLabel = UILabel()
override func didMoveToView(view: SKView) {
scoreLabel.text = "\(score)"
scoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height:
20))
scoreLabel.textColor = UIColor.blackColor()
score = nil
if score == nil {
score = 0
scoreLabel.text = "\(score!)"
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory.bullet) &&
(secondBody.categoryBitMask == PhysicsCategory.enemy) ||
(firstBody.categoryBitMask == PhysicsCategory.enemy) &&
(secondBody.categoryBitMask == PhysicsCategory.bullet)) {
//i get the error next line
collisionWithBullet((firstBody.node as! SKSpriteNode),
bullet: (secondBody.node as! SKSpriteNode))
}
}
func collisionWithBullet(enemy: SKSpriteNode, bullet: SKSpriteNode){
score? += 1
scoreLabel.text = "\(score!)"
enemy.removeFromParent()
bullet.removeFromParent()
}
Objective c、 '[enemy removeFromParent] ' –
チェーンのロギングを開始します。 collisionWithBulletが呼び出されますか?もしそうなら、敵とは何か、それが呼ばれたら何が弾丸ですか? –
なぜあなたは 'score'をオプションとして宣言していますか?初期値が0の場合は、それを非オプションの 'var score = 0'として宣言します。 – vadian