I'm trying to overload the touchesBegan function in Swift but I got a strange error that causes XCode to crash.
I think the problem is at line "for touch: AnyObject in touches {
" but I used it in other classes and it worked very well.
This is my code :
import SpriteKit
class PlayScene: SKScene {
let touchButton = SKSpriteNode(imageNamed:"Touch")
override func didMoveToView(view: SKView!) {
self.backgroundColor = UIColor(hex: 0xD64541)
self.touchButton.size = CGSizeMake(50, 50)
let touchButtonWidth = self.touchButton.size.width
let touchButtonHeight = self.touchButton.size.height
self.touchButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(self.touchButton)
}
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.touchButton {
println("button")
}
else {
println("ext")
}
}
}
}
Thank you
Try to check touches
for nil before iterate through it (works for me):
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
if let tchs = touches {
for touch: AnyObject in tchs {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.touchButton {
println("button")
}
else {
println("ext")
}
}
}
}