Swift致命错误(启动时崩溃)

时间:2015-04-08 02:18:29

标签: ios swift sprite-kit

我收到错误

fatal error: unexpectedly found nil while unwrapping an Optional value

这只有当我把这一行放在

时才会发生
    override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if wall.position.x == 0 {
        score = score + 1
        scoreLabel.text = "\(score)"
    }


}
}

错误说明的行是"如果wall.posistion.x == 0 {"我的意思是它表示线程错误" EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0 )"。我试图创建一个得分,所以当墙穿过那个x点时,得分会上升1

整个代码

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var movingGround: CSMovingGround!
var hero: CSHero!
var cloudGenerator: CSCloudGenerator!
var wallGenerator: CSWallGenerator!
var wall: CSWall!
var scoreLabel = SKLabelNode()
var score: Int = 0
var isStarted = false

var heroCategory: UInt32 = 1<<1
var wallCategory: UInt32 = 1<<2
var groundCategory: UInt32 = 1<<2




override func didMoveToView(view: SKView) {
    backgroundColor = UIColor(red: 159.0/255.0, green: 201.0/255, blue: 244.0/255.0, alpha: 1.0)

    /*
    let backgroundTexture = SKTexture(imageNamed: "background.png")
    let backgroundImage = SKSpriteNode(texture: backgroundTexture, size: view.frame.size)
    backgroundImage.position = view.center
    addChild(backgroundImage)
    */



    // add ground
    movingGround = CSMovingGround(size: CGSizeMake(view.frame.width, kCSGroundHeight))
    movingGround.position = CGPointMake(0, view.frame.size.height/2)

    self.addChild(movingGround)

    // add hero
    hero = CSHero()
    hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
    hero.physicsBody = SKPhysicsBody(rectangleOfSize: hero.size)
    hero.physicsBody?.dynamic = true
    hero.physicsBody?.allowsRotation = false
    hero.physicsBody!.collisionBitMask = heroCategory | wallCategory
    hero.physicsBody!.contactTestBitMask = wallCategory | heroCategory | groundCategory
    self.addChild(hero)
    hero.breathe()


    // add cloud generator
    cloudGenerator = CSCloudGenerator(color: UIColor.clearColor(), size: view.frame.size)
    cloudGenerator.position = view.center
    addChild(cloudGenerator)
    cloudGenerator.populate(7)
    cloudGenerator.startGeneratingWithSpawnTime(5)

    // add wall generator

    wallGenerator = CSWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
    wallGenerator.position = view.center
    wallGenerator.physicsBody = SKPhysicsBody(edgeLoopFromRect : wallGenerator.frame)
    wallGenerator.physicsBody?.dynamic = false
    wallGenerator.physicsBody!.collisionBitMask = wallCategory | heroCategory

    self.addChild(wallGenerator)


    let ground1 = SKSpriteNode(color: UIColor.clearColor(), size: CGSizeMake(view.frame.size.width, 20))
    ground1.position = view.center
    ground1.physicsBody = SKPhysicsBody(rectangleOfSize: ground1.size)
    ground1.physicsBody!.dynamic = false
    ground1.physicsBody!.affectedByGravity = false
    ground1.physicsBody!.categoryBitMask = groundCategory
    ground1.physicsBody!.collisionBitMask = groundCategory | heroCategory
    self.addChild(ground1)

    let ground2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(view.frame.size.width, 20))
    ground2.position = CGPointMake(284, 98)
    ground2.physicsBody = SKPhysicsBody(rectangleOfSize: ground2.size)
    ground2.physicsBody!.dynamic = false
    ground2.physicsBody!.affectedByGravity = false
    ground2.physicsBody!.categoryBitMask = groundCategory
    ground2.physicsBody!.collisionBitMask = groundCategory | heroCategory
    self.addChild(ground2)


    physicsWorld.contactDelegate = self

    scoreLabel.fontName = "score"
    scoreLabel.fontColor = UIColor.blackColor()
    scoreLabel.fontSize = 40
    scoreLabel.text = "0"
    scoreLabel.position = CGPoint(x: 50, y: 275)
    self.addChild(scoreLabel)


}



func start() {
    isStarted = true
    hero.stop()
    hero.startRunning()
    movingGround.start()

    wallGenerator.startGeneratingWallsEvery(1)
}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    if !isStarted {
        start()
    } else {
        hero.flip()
    }


}


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 & UInt32(heroCategory)) != 0 && (secondBody.categoryBitMask & UInt32(wallCategory)) != 0 {
        wallGenerator.removeFromParent()
        let reveal = SKTransition.flipHorizontalWithDuration(0.5)
        let scene = GameOverScene(size: self.size, won: false)
        self.view?.presentScene(scene, transition: reveal)
    }
}


override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if wall.position.x < 70 {
        score++
        scoreLabel.text = "\(score)"
    }


}
}


import Foundation

导入SpriteKit

类CSWall:SKSpriteNode {

let WALL_WIDTH: CGFloat = 30.0
let WALL_HEIGHT: CGFloat = 50.0
let WALL_COLOR = UIColor.blackColor()

override init() {
    super.init(texture: nil, color: WALL_COLOR, size: CGSizeMake(WALL_WIDTH, WALL_HEIGHT))
    startMoving()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func startMoving() {
    let moveLeft = SKAction.moveByX(-300, y: 0, duration: 1)
    runAction(SKAction.repeatActionForever(moveLeft))
}


}

1 个答案:

答案 0 :(得分:0)

因此,错误似乎非常有信心您正在尝试访问零值的属性。 wall.position.x可能未初始化。如果您的值不存在,您可以检查nil并且不执行任何操作但是您可能需要弄清楚为什么它首先没有初始化,因为它似乎很重要。

if let wallPosition = wall.position.x {
     if (wallPosition == 0){
         score ++
         scoreLabel.text = "\(score)"
     }
}