Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 小行星战斗游戏持续崩溃_Ios_Sprite Kit_Swift2 - Fatal编程技术网

Ios 小行星战斗游戏持续崩溃

Ios 小行星战斗游戏持续崩溃,ios,sprite-kit,swift2,Ios,Sprite Kit,Swift2,因此,我在玩游戏的过程中,在“致命错误:在打开可选值时意外发现零”的过程中,在我的小行星战斗游戏中,我一直在随机出现这个错误。我会把代码贴在这里,如果有人能帮我弄明白,我会很感激的。如果你不懂任何部分,你可以问我 import SpriteKit import Darwin struct PhysicsCatagory { static let Asteroids : UInt32 = 1 //00000000000000000000000000000001 static le

因此,我在玩游戏的过程中,在“致命错误:在打开可选值时意外发现零”的过程中,在我的小行星战斗游戏中,我一直在随机出现这个错误。我会把代码贴在这里,如果有人能帮我弄明白,我会很感激的。如果你不懂任何部分,你可以问我

import SpriteKit
import Darwin

struct PhysicsCatagory {
    static let Asteroids : UInt32 = 1 //00000000000000000000000000000001
    static let Bullet : UInt32 = 2 //0000000000000000000000000000010
    static let Fighter : UInt32 = 3 //0000000000000000000000000000100

    }

class GameScene: SKScene, SKPhysicsContactDelegate {

var Score = Int()
var scoreLabel = UILabel()

var Fighter = SKSpriteNode(imageNamed: "Fighter")

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    physicsWorld.contactDelegate = self

    Fighter.position = CGPointMake(self.size.width / 2, self.size.height / 7)
    Fighter.physicsBody = SKPhysicsBody(rectangleOfSize: Fighter.size)
    Fighter.physicsBody?.affectedByGravity = false
    Fighter.physicsBody?.categoryBitMask = PhysicsCatagory.Fighter
    Fighter.physicsBody?.contactTestBitMask = PhysicsCatagory.Asteroids
    Fighter.physicsBody?.dynamic = false

    var Timer = NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: Selector("spawnBullets"), userInfo: nil, repeats: true)

    var asteriodsTimer = NSTimer.scheduledTimerWithTimeInterval(0.35 , target: self, selector: Selector("spawnAsteroids"), userInfo: nil, repeats: true)

    var furiousAsteriodsTimer = NSTimer.scheduledTimerWithTimeInterval(0.9 , target: self, selector: Selector("spawnFuriousAsteroids"), userInfo: nil, repeats: true)


    self.addChild(Fighter)

    scoreLabel.text = "\(Score)"
    scoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
    scoreLabel.backgroundColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.3)
    scoreLabel.textColor = UIColor.whiteColor()

    self.view?.addSubview(scoreLabel)

}

func didBeginContact(contact: SKPhysicsContact) {

    let firstBody : SKPhysicsBody = contact.bodyA
    let secondBody : SKPhysicsBody = contact.bodyB

    if ((firstBody.categoryBitMask == PhysicsCatagory.Asteroids) && (secondBody.categoryBitMask == PhysicsCatagory.Bullet) || (firstBody.categoryBitMask == PhysicsCatagory.Bullet) && (secondBody.categoryBitMask == PhysicsCatagory.Asteroids)){

        bulletHitAsteroids(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)

    }
}

func bulletHitAsteroids(Asteroids: SKSpriteNode, Bullet: SKSpriteNode){

    Asteroids.removeFromParent()
    Bullet.removeFromParent()

    Score++
    scoreLabel.text = "\(Score)"

}

func bulletHitFuriousAsteroids(FuriousAsteroids: SKSpriteNode, Bullet: SKSpriteNode){

    FuriousAsteroids.removeFromParent()
    Bullet.removeFromParent()

    Score = Score + 2
    scoreLabel.text = "\(Score)"
}


func spawnBullets(){

    let Bullet = SKSpriteNode(imageNamed: "Bullet")
    Bullet.zPosition = -5
    Bullet.position = CGPointMake(Fighter.position.x, Fighter.position.y)

    let action = SKAction.moveToY(self.size.height + 30, duration: 0.9)
    let actionDone = SKAction.removeFromParent()
    Bullet.runAction(SKAction.sequence([action, actionDone]))

    Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
    Bullet.physicsBody?.categoryBitMask = PhysicsCatagory.Bullet
    Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Asteroids
    Bullet.physicsBody?.affectedByGravity = false
    Bullet.physicsBody?.dynamic = false

    self.addChild(Bullet)

}



func spawnAsteroids(){
    let Asteroids = SKSpriteNode(imageNamed: "Asteroid")
    let minValue = self.size.width / 8
    let maxValue = self.size.width - 20
    let spawnPoint = UInt32(maxValue - minValue)
    Asteroids.position = CGPoint(x: CGFloat(arc4random_uniform(spawnPoint)) , y: self.size.height)

    Asteroids.physicsBody = SKPhysicsBody(rectangleOfSize: Asteroids.size)
    Asteroids.physicsBody?.categoryBitMask = PhysicsCatagory.Asteroids
    Asteroids.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
    Asteroids.physicsBody?.affectedByGravity = false
    Asteroids.physicsBody?.dynamic = true


    let action = SKAction.moveToY(-50, duration: 2.5)
    let actionDone = SKAction.removeFromParent()

    Asteroids.runAction(SKAction.sequence([action, actionDone]))

    self.addChild(Asteroids)

}

func spawnFuriousAsteroids(){
    let FuriousAsteroids = SKSpriteNode(imageNamed: "FuriousAsteroid")
    let minValue = self.size.width / 8
    let maxValue = self.size.width - 20
    let spawnPoint = UInt32(maxValue - minValue)
    FuriousAsteroids.position = CGPoint(x: CGFloat(arc4random_uniform(spawnPoint)) , y: self.size.height)

    FuriousAsteroids.physicsBody = SKPhysicsBody(rectangleOfSize: FuriousAsteroids.size)
    FuriousAsteroids.physicsBody?.categoryBitMask = PhysicsCatagory.Asteroids
    FuriousAsteroids.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
    FuriousAsteroids.physicsBody?.affectedByGravity = false
    FuriousAsteroids.physicsBody?.dynamic = true


    let action = SKAction.moveToY(-50, duration: 1.5)
    let actionDone = SKAction.removeFromParent()

    FuriousAsteroids.runAction(SKAction.sequence([action, actionDone]))

    self.addChild(FuriousAsteroids)

}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        Fighter.position.x = location.x
    }
}

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

    for touch in touches {
        let location = touch.locationInNode(self)

        Fighter.position.x = location.x
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
}
导入SpriteKit
导入达尔文
结构物理分类{
静态let小行星:UInt32=1//0000000000000000000000000 1
静态let项目符号:UInt32=2//00000000000000000010
静态let战斗机:UInt32=3//000000000000000000000000100
}
类游戏场景:SKScene,SKPhysicContactDelegate{
var得分=Int()
var scoreLabel=UILabel()
var Fighter=SKSpriteNode(图像名称:“Fighter”)
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
physicsWorld.contactDelegate=self
Fighter.position=CGPointMake(self.size.width/2,self.size.height/7)
Fighter.physicsBody=SKPhysicsBody(矩形尺寸:Fighter.size)
战斗机。物理身体?。受重力影响=错误
Fighter.physicsBody?.categoryBitMask=PhysicsCatagory.Fighter
Fighter.physicsBody?.contactTestBitMask=物理分类。小行星
Fighter.physicsBody?.dynamic=错误
var Timer=NSTimer.scheduledTimerWithTimeInterval(0.25,目标:self,选择器:选择器(“生成子弹”),userInfo:nil,repeats:true)
var asteriodsimer=NSTimer.scheduledTimerWithTimeInterval(0.35,目标:self,选择器:选择器(“生成小行星”),userInfo:nil,repeats:true)
var frequerasteriodsimer=NSTimer.scheduledTimerWithTimeInterval(0.9,目标:self,选择器:选择器(“生成小行星”),userInfo:nil,repeats:true)
赛尔夫·阿德奇尔德(战斗机)
scoreLabel.text=“\(分数)”
scoreLabel=UILabel(帧:CGRect(x:0,y:0,宽度:200,高度:40))
scoreLabel.backgroundColor=UIColor(红色:0.2,绿色:0.2,蓝色:0.2,阿尔法:0.3)
scoreLabel.textColor=UIColor.whiteColor()
self.view?.addSubview(scoreLabel)
}
func didBeginContact(联系人:SKPhysicsContact){
让第一个body:SKPhysicsBody=contact.bodyA
让第二个主体:SKPhysicsBody=contact.bodyB
如果((firstBody.categoryBitMask==PhysicCatagory.Asteroids)&&(secondBody.categoryBitMask==PhysicCatagory.Bullet)|(firstBody.categoryBitMask==PhysicCatagory.Bullet)&(secondBody.categoryBitMask==PhysicCatagory.Asteroids)){
Bullet小行星(第一个Body.node为!SKSpriteNode,第二个Body.node为!SKSpriteNode)
}
}
func Bulletthit小行星(小行星:SKSpriteNode,子弹:SKSpriteNode){
小行星。removeFromParent()
Bullet.removeFromParent()
得分++
scoreLabel.text=“\(分数)”
}
func Bullettenode小行星(小行星:SKSpriteNode,子弹:SKSpriteNode){
疯狂的小行星。从父级移除()
Bullet.removeFromParent()
分数=分数+2
scoreLabel.text=“\(分数)”
}
func{
让Bullet=SKSpriteNode(图像名为:“Bullet”)
Bullet.zPosition=-5
Bullet.position=CGPointMake(Fighter.position.x,Fighter.position.y)
let action=SKAction.moveToY(self.size.height+30,持续时间:0.9)
让actionDone=SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action,actionDone]))
Bullet.physicsBody=SKPhysicsBody(矩形尺寸:Bullet.size)
Bullet.physicsBody?.categoryBitMask=PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask=物理分类。小行星
Bullet.physicsBody?重力影响=假
Bullet.physicsBody?.dynamic=false
self.addChild(项目符号)
}
func生成小行星(){
让小行星=SKSpriteNode(图像名为“小行星”)
设minValue=self.size.width/8
设maxValue=self.size.width-20
让spawnPoint=UInt32(最大值-最小值)
小行星.position=CGPoint(x:CGFloat(arc4random_uniform(spawnPoint)),y:self.size.height)
小行星.physicsBody=SKPhysicsBody(矩形尺寸:小行星.size)
小行星.physicsBody?.categoryBitMask=PhysicsCatagory.Asteroids
小行星.physicsBody?.contactTestBitMask=PhysicsCatagory.Bullet
小行星。物理体?。受重力影响=假
小行星.physicsBody?动力学=真
let action=SKAction.moveToY(-50,持续时间:2.5)
让actionDone=SKAction.removeFromParent()
小行星.runAction(SKAction.sequence([action,actionDone]))
self.addChild(小行星)
}
func小行星(){
让狂暴的小行星=SKSpriteNode(图片名为:“狂暴的小行星”)
设minValue=self.size.width/8
设maxValue=self.size.width-20
让spawnPoint=UInt32(最大值-最小值)
位置=CGPoint(x:CGFloat(arc4random_uniform(spawnPoint)),y:self.size.height)
FrigureAsteroids.physicsBody=SKPhysicsBody(矩形尺寸:FrigureAsteroids.size)
疯狂的小行星.physicsBody?.categoryBitMask=PhysicsCatagory.Asteroids
疯狂的小行星.physicsBody?.contactTestBitMask=PhysicsCatagory.Bullet
疯狂的小行星。物理体?。受重力影响=错误
疯狂的小行星。物理体?。动力学=真
let action=SKAction.moveToY(-50,持续时间:1.5)
让actionDone=SKAction.removeFromParent()
运行操作(SKAction.sequence([action,actionDone]))
self.addChild(愤怒的小行星)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
接触{
let location=touch.locationInNode(自)
战斗机.position.x=位置.x
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
接触{
let location=touch.locationInNode(自)
战斗机.position.x=位置.x
}
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}

它正在崩溃,因为其中一个实体没有节点。当崩溃发生时,试着在
 if let firstNode = firstBody.node as? SKSpriteNode, let secondNode = secondBody.node as? SKSpriteNode {

   bulletHitAsteroids(firstNode, Bullet: secondNode)
}