-1

それが話しているオプションの値、またはこのエラーが発生する理由を理解できないようです。スコア整数を確認し、敵と接触するまで値が 0 であることを宣言したことを確認しました。シミュレーターでは、カウンターは最初の 4 つまたは 5 つの敵をカウントしてからクラッシュします。

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 ()

}
4

1 に答える 1

1

スコアを変更

var score = 0

それ以外の

var score = Int? ()

そしてこの部分の代わりに

score = nil
if score == nil {
    score = 0
    scoreLabel.text = "\(score!)"
}

これだけ書いて

scoreLabel.text = "\(score)"

編集:この部分の代わりに

collisionWithBullet((firstBody.node as! SKSpriteNode), 
bullet: (secondBody.node as! SKSpriteNode))

このようなことをする

if let firstNode = firstBody.node as? SKSpriteNode,
 secondNode = secondBody .node as? SKSpriteNode {
collisionWithBullet((firstNode), 
    bullet: (secondNode))
}
于 2016-04-13T06:05:14.813 に答える