The following function could run several hundred times with no problem but will occasionally cause a EXC_BAD_ACCESS (SIGBUS)
. The last time it was a KERN_PROTECTION_FAILURE
perhaps meaning it is trying to write to a read only memory but previously it was KERN_INVALID_ADDRESS
. I have tried debugging tools to no avail. Can anyone see any reason why the following function could be causing this. Just in case the function is a red herring the only other thing that was running is a AVQueuePlayer
that contains 11 tracks, but the crash happens mid song making it unlikely.
The function includes running other functions so below is all related functions that happen as a result of a swipe gesture.
func swipeLeft() {
var edgenumber = 0
var squares2move = 0
let Square = positions.firstIndex(of: currentNode.position)! + 1
if Square < 6 {
edgenumber = 1
}
if Square < 11 && Square > 5 {
edgenumber = 6
}
if Square < 16 && Square > 10 {
edgenumber = 11
}
if Square < 21 && Square > 15 {
edgenumber = 16
}
if Square < 26 && Square > 20 {
edgenumber = 21
}
if Square == edgenumber {
moveinprogress = 0
return
}
if tiles[Square - 2].name != "" {
squares2move = 0
completeLeft()
return
}
if tiles[Square - 2].name == "" && (Square - 1) == edgenumber {
squares2move = 1
moveLeft()
return
}
if tiles[Square - 3].name != "" {
squares2move = 1
completeLeft()
return
}
if tiles[Square - 3].name == "" && (Square - 2) == edgenumber {
squares2move = 2
moveLeft()
return
}
if tiles[Square - 4].name != "" {
squares2move = 2
completeLeft()
return
}
if tiles[Square - 4].name == "" && (Square - 3) == edgenumber {
squares2move = 3
moveLeft()
return
}
if tiles[Square - 5].name != "" {
squares2move = 3
completeLeft()
return
}
if tiles[Square - 5].name == "" && (Square - 4) == edgenumber {
squares2move = 4
moveLeft()
return
}
squares2move = 4
moveLeft()
func completeLeft() {
let add1 = Int(tiles[Square - (squares2move + 2)].name ?? "0") ?? 0
let add2 = Int(currentNode.name ?? "0") ?? 0
let add = add1 + add2
if add.isMultiple(of: 9) == false {
if squares2move == 0 {
self.moveinprogress = 0
return
}
moveLeft1()
}
if add.isMultiple(of: 9) {
squares2move += 1
moveLeft2()
}
func moveLeft1() {
var duration = TimeInterval(0.1)
duration = Double(squares2move) * duration
self.soundEffect = "swipe"
self.playEffects(Volume: self.effectsVolume)
let move = SKAction.move(to: positions[Square - (squares2move + 1)], duration: duration)
currentNode.run(move, completion: {
self.tiles[Square - (squares2move + 1)].name = self.currentNode.name
self.tiles[Square - 1].name = ""
self.currentNode.position = self.positions[Square - (squares2move + 1)]
self.newNumber()
})
}
func moveLeft2() {
var duration = TimeInterval(0.1)
duration = Double(squares2move) * duration
self.soundEffect = "swipe"
self.playEffects(Volume: self.effectsVolume)
let move = SKAction.move(to: positions[Square - (squares2move + 1)], duration: duration)
currentNode.zPosition = currentNode.zPosition - 1
currentNode.run(move, completion: {
self.currentNode.position = self.positions[Square - (squares2move + 1)]
self.tiles[Square - 1].name = ""
self.currentNode.name = "\(add)"
self.tiles[Square - (squares2move + 1)].name = "\(add)"
if add > self.score {
self.score = add
}
if add >
self.currentgoal {
self.levelComplete()}
else {
self.playEffects2(soundEffect: "Tink", Volume: self.effectsVolume, Type: "caf")}
if self.currentNode.childNode(withName: "Label") == nil {
self.currentNode.texture = SKTexture(imageNamed: "0.png")
let Label = SKLabelNode(fontNamed: "CHALKBOARDSE-BOLD")
Label.text = "\(add)"
Label.name = "Label"
let numberofdigits = Label.text!.count
if numberofdigits == 1 {
Label.fontSize = 45 * self.hR}
if numberofdigits == 2 {
Label.fontSize = 40 * self.hR}
if numberofdigits == 3 {
Label.fontSize = 32 * self.hR}
if numberofdigits == 4 {
Label.fontSize = 25 * self.hR}
Label.horizontalAlignmentMode = .center
Label.verticalAlignmentMode = .center
Label.fontColor = .systemBlue
Label.zPosition = 2
self.currentNode.addChild(Label)
self.currentNode.zPosition = self.currentNode.zPosition + 1
var nodes = self.nodes(at: self.positions[Square - (squares2move + 1)])
nodes = nodes.filter { $0.name != "Tile" }
nodes = nodes.filter { $0 != self.currentNode }
nodes = nodes.filter { $0 != self.currentNode.childNode(withName: "Label") }
for v in nodes {
v.removeFromParent()}
}
else {
let Lbl = self.currentNode.childNode(withName: "Label") as? SKLabelNode
Lbl!.text = "\(add)"
let numberofdigits = Lbl!.text!.count
if numberofdigits == 1 {
Lbl!.fontSize = 45 * self.hR}
if numberofdigits == 2 {
Lbl!.fontSize = 40 * self.hR}
if numberofdigits == 3 {
Lbl!.fontSize = 32 * self.hR}
if numberofdigits == 4 {
Lbl!.fontSize = 25 * self.hR}
self.currentNode.zPosition = self.currentNode.zPosition + 1
var nodes = self.nodes(at: self.positions[Square - (squares2move + 1)])
nodes = nodes.filter { $0.name != "Tile" }
nodes = nodes.filter { $0 != self.currentNode }
nodes = nodes.filter { $0 != self.currentNode.childNode(withName: "Label")
}
for v in nodes {
v.removeFromParent()}
}
self.moveinprogress = 0
})
}
}
func moveLeft() {
var duration = TimeInterval(0.1)
duration = Double(squares2move) * duration
self.soundEffect = "swipe"
self.playEffects(Volume: self.effectsVolume)
let move = SKAction.move(to: positions[Square - (squares2move + 1)], duration: duration)
currentNode.run(move, completion: {
self.tiles[Square - (squares2move + 1)].name = self.currentNode.name
self.tiles[Square - 1].name = ""
self.currentNode.position = self.positions[Square - (squares2move + 1)]
self.newNumber()
})
}
}