我有一个游戏,当你死后,你点击游戏应该重新启动,但每当我点击重新启动时,它都会给我这个(见上文)错误。提前致谢(:
这是我的代码:
class MCTFruitGen: SKSpriteNode {
var generationTimer: NSTimer!
var fruits = [MCTFruit]()
var fruitTracker = [MCTFruit]()
func startGeneratingFruitEvery(seconds: NSTimeInterval) {
generationTimer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: self, selector: "generateFruit", userInfo: nil, repeats: true)
}
func stopGenerating() {
generationTimer?.invalidate()
}
func generateFruit() {
var scale: CGFloat
let rand = arc4random_uniform(2)
if rand == 0 {
scale = -1.0
} else {
scale = 1.0
}
let strawberry = MCTFruit()
strawberry.position.x = size.width/2 + strawberry.size.width/2
strawberry.position.y = scale * (NMCGroundHeight/160 + strawberry.size.height)
self.fruits.append(strawberry)
fruitTracker.append(fruits)
addChild(strawberry) // line that gives me the error
}
func stopFruit() {
stopGenerating()
for fruit in fruits {
fruit.stopFruitMoving()
}
}
}
最佳答案
尝试使用 SKAction
而不是 NSTimer
来生成水果或 SpriteKit 中的任何计时事件。例如
class MCTFruitGen: SKSpriteNode {
var fruits = [MCTFruit]()
var fruitTracker = [MCTFruit]()
func startGeneratingFruitEvery(seconds: NSTimeInterval) {
let callGenerateAction = SKAction.runBlock { () -> Void in
self.generateFruit()
}
let waitOneSecond = SKAction.waitForDuration(seconds)
let sequenceAction = SKAction.sequence([callGenerateAction, waitOneSecond])
let fruitGeneratorAction = SKAction.repeatActionForever(sequenceAction)
self.runAction(fruitGeneratorAction, withKey: "fruitGenerator")
}
func stopGenerating() {
self.removeActionForKey("fruitGenerator");
}
func generateFruit() {
// Your Code
}
func stopFruit() {
stopGenerating()
// Your Code
}
}
关于Swift SpriteKit : Thread 1: EXC_BAD_ACCESS on addChild? 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32418589/