添加SKSpriteNode时,Swift中的EXC_BAD_ACCESS

时间:2014-08-16 18:24:00

标签: macos swift exc-bad-access nszombie

我正在Swift中为OS X创建一个小游戏来练习,但是我遇到了一个错误的错误。当我尝试创建两个新的SKSpriteNodes时出现:

var battleBegan:Bool = false
let beginButton = SKSpriteNode(imageNamed: "button")
var backGroundMenu = SKSpriteNode(imageNamed: "background")
var backgroundMusicPlayer:AVAudioPlayer = AVAudioPlayer()

var firstPokemon:Pokemon = Pokemon(name: "Pikachu", level: 25, HP: 100, power: 1)
var secondPokemon:Pokemon = Pokemon(name: "Aquali", level: 25, HP: 100, power: 1)
//var firstPokemonSKSN = SKSpriteNode(imageNamed: "Pikachu")
//var secondPokemonSKSN = SKSpriteNode(imageNamed: "Aquali") //No error

但是当我取消注释两个新的SKSN时:

var battleBegan:Bool = false
let beginButton = SKSpriteNode(imageNamed: "button")
var backGroundMenu = SKSpriteNode(imageNamed: "background")
var backgroundMusicPlayer:AVAudioPlayer = AVAudioPlayer()

var firstPokemon:Pokemon = Pokemon(name: "Pikachu", level: 25, HP: 100, power: 1)
var secondPokemon:Pokemon = Pokemon(name: "Aquali", level: 25, HP: 100, power: 1)
var firstPokemonSKSN = SKSpriteNode(imageNamed: "Pikachu") //Errors of type EXC_BAD_ACCESS
var secondPokemonSKSN = SKSpriteNode(imageNamed: "Aquali") //Errors of type EXC_BAD_ACCESS

我是法国人,我理解英语,但我发现的解释对我来说太具体和技术性。我读过我必须激活NSZombie才能找到我的错误,并且因为内存问题而出现问题。

所以这是我的问题:为什么在创建新的SKSN时出现此错误,它的含义是什么?如何在Swift中激活NSZombies以取消它?

谢谢!

错误的屏幕截图:a busy cat http://img11.hostingpics.net/pics/758626ScreenShot20140816at202953.png

修改

在尝试像Mike S建议的断点之后,似乎AVAudioPlayer出现错误:

import SpriteKit
import AVFoundation

class GameScene: SKScene
{

var battleBegan:Bool = false
let beginButton = SKSpriteNode(imageNamed: "button")
var backGroundMenu = SKSpriteNode(imageNamed: "background")
var backgroundMusicPlayer:AVAudioPlayer = AVAudioPlayer()

var firstPokemon:Pokemon = Pokemon(name: "Pikachu", level: 25, HP: 100, power: 1)
var secondPokemon:Pokemon = Pokemon(name: "Aquali", level: 25, HP: 100, power: 1)
var firstPokemonSKSN:SKSpriteNode? = SKSpriteNode(imageNamed: "Pikachu")
var secondPokemonSKSN:SKSpriteNode? = SKSpriteNode(imageNamed: "Aquali")



override func didMoveToView(view: SKView)
{

    var musicBGURL:NSURL = NSBundle.mainBundle().URLForResource("theme", withExtension: "mp3")
    backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: musicBGURL, error: nil)
    backgroundMusicPlayer.numberOfLoops = -1
    backgroundMusicPlayer.prepareToPlay() //BREAKPOINT Pops
    backgroundMusicPlayer.volume = 0.2
    backgroundMusicPlayer.play() //BREAKPOINT Pops

    self.anchorPoint = CGPointMake(0.5, 0.5)

    backGroundMenu.position = CGPointMake(0, 0)
    backGroundMenu.size = self.frame.size
    self.addChild(backGroundMenu)

    beginButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    beginButton.size = CGSize(width: 100, height: 100)
    let action = SKAction.rotateByAngle(-CGFloat(6), duration:2)
    let action2 = SKAction.fadeInWithDuration(1)
    let action3 = SKAction.fadeOutWithDuration(1)
    beginButton.runAction(SKAction.repeatActionForever(SKAction.group([action,SKAction.sequence([action2,action3])])))
    self.addChild(beginButton)

}
[...]

以下是link

2 个答案:

答案 0 :(得分:1)

尝试这样做:

var firstPokemonSKSN: SKSpriteNode? = SKSpriteNode(imageNamed: "Pikachu")

如果让节点成为可选节点(并且在使用它之前强迫自己检查nil)会使你的错误消失,那么你就知道问题是(ObjC)初始化器返回nil,这是一个普通的Swift的情况实例无法有效处理。

对于C类{...}

var c: C = <blah>
保证永远不会是零,而OTOH

var c: C! = <blah>

可能是零,但你可以抓住机会而不是在使用前检查,而

var c: C? = <blah>

可能是零,你必须在使用前检查

if c != nil { .. use c .. } 

答案 1 :(得分:0)

好的,所以我修好了但是我不知道为什么这个工作而不是另一个:

我刚刚改变了这个

firstPokemonSKSN = SKSpriteNode(imageNamed:"pikachu")

通过

firstPokemonSKSN = SKSpriteNode(texture: SKTexture(image: NSImage(named: "pikachu")))
和pof,它有效......我不明白为什么,这很烦人。我有任何想法......