Swift错误线程1:EXC_BAD_INSTRUCITON

时间:2016-01-05 07:50:41

标签: ios swift compiler-errors crash exc-bad-instruction

嗨,我只是快速学习,需要一些帮助。当我运行我的应用程序时,它会崩溃。

我得到了错误
player1 = Player(name: "\(playerNames[0])", hp: 110, attackPwr: 10)

它说:

  

主题1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

我尝试将其放在if声明中,即

if playerNames.count == 2 {
    player1 = Player(name: "\(playerNames[0])", hp: 110, attackPwr: 10)
}

然后加载了我的应用程序,但是当我按下player1AttackBtn on该行时,我遇到了同样的错误。

@IBAction func player1AttackBtn(sender: AnyObject)

任何帮助将不胜感激!提前致谢!!!

父类

import Foundation

class Character {
    private var _hp: Int = 100
    private var _attackPwr: Int = 10

    var attackPwr: Int  {
        get {
            return _attackPwr
        }
    }

    var hp: Int {
        get {
            return _hp
        }
    }

    var isAlive: Bool {
        get {
            if hp <= 0 {
                return false
            } else {
                return true
            }
        }
    }

    init(startingHp: Int, attackPwr: Int) {
        self._hp = startingHp
        self._attackPwr = attackPwr
    }

    func attemptAttack(attackPwr: Int) -> Bool {
        self._hp -= attackPwr

        return true
    }

}

玩家类

import Foundation

class Player: Character {
    private var _name = "Player"

    var name: String {
        get {
            return _name
        }
    }

    convenience init(name: String, hp: Int, attackPwr: Int) {
        self.init(startingHp: hp, attackPwr: attackPwr)
        _name = name
    }


}

查看控制器

import UIKit
import AVFoundation

//var bgMusicAudio = AVAudioPlayer!
var player1: Player!
var player2: Player!


class ViewController: UIViewController {

    var playerNames = [String]()

    @IBOutlet weak var playerOne: UIImageView!
    @IBOutlet weak var playerTwo: UIImageView!
    @IBOutlet weak var nameLbl: UITextField!
    @IBOutlet weak var textBtn: UIButton!
    @IBOutlet weak var printLbl: UILabel!
    @IBOutlet weak var fightBtn: UIButton!
    @IBOutlet weak var player1Attack: UIButton!
    @IBOutlet weak var player2Attack: UIButton!
    @IBOutlet weak var attackLbl1: UILabel!
    @IBOutlet weak var attackLbl2: UILabel!
    @IBOutlet weak var player1HP: UILabel!
    @IBOutlet weak var player2HP: UILabel!

    var bgMusic = AVAudioPlayer!()

    override func viewDidLoad() {
        super.viewDidLoad()


        player1 = Player(name: "\(playerNames[0])", hp: 110, attackPwr: 10)
        player2 = Player(name: "\(playerNames[1])", hp: 110, attackPwr: 10)

        player1HP.text = "\(player1.hp)"
        player2HP.text = "\(player2.hp)"



        let bgMusicPath = NSBundle.mainBundle().pathForResource("8BitTrack", ofType: "wav")
        let bgMusicURL = NSURL(fileURLWithPath: bgMusicPath!)

        do {
            try bgMusic = AVAudioPlayer(contentsOfURL: bgMusicURL)
            bgMusic.prepareToPlay()

        } catch let err as NSError {
            print(err.debugDescription)
        }

        bgMusic.numberOfLoops = -1
        bgMusic.play()

        printLbl.text = "Player one, what is your name?"

}




    @IBAction func player1AttackBtn(sender: AnyObject) {

        if player2.attemptAttack(player1.attackPwr) {
            printLbl.text = "Attacked \(player2) for \(player1.attackPwr) HP"
            player2HP.text = "\(player2.hp)"

        } else {
            printLbl.text = "Attack was unsuccessful!"
        }

        if !player2.isAlive {
            player2HP.text = ""
            printLbl.text = "\(player1.name) killed \(player2.name)."
            playerTwo.hidden = true            
        } 
    }


    @IBAction func player2AttackBtn(sender: AnyObject) {
    }




    @IBAction func doneBtn(sender: AnyObject) {
        if let name = nameLbl.text {

            playerNames.append(name)

            nameLbl.text = ""

            printLbl.text = "Player two, what is your name?"

            if playerNames.count == 2 {
                nameLbl.hidden = true
                textBtn.hidden = true
                printLbl.text = "Welcome \(playerNames[0]) and \(playerNames[1])"
                fightBtn.hidden = false

            }

        }

    }

    @IBAction func fightBtnPressed(sender: AnyObject) {

        player1Attack.hidden = false
        player2Attack.hidden = false
        attackLbl1.hidden = false
        attackLbl2.hidden = false
        fightBtn.hidden = true
        printLbl.text = "Press attack to attack!"
        player1HP.hidden = false
        player2HP.hidden = false
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,您可以在viewDidLoad中调用它,但是您将名称附加到playerNames中的@IBAction func doneBtn(sender: AnyObject)。当你第一次调用它时(并且因为应用程序崩溃只有时间)数组为空。