Reputation: 199
I want to create a bunch of sprites and remove them one at a time when I touch them. So far what happens is that when I add the code, the last sprite gets removed and not the sprite I touch.
var sprite = SKSpriteNode?()
var touchLocation : CGPoint?
for touch in touches {
let location = touch.locationInNode(self)
touchLocation = location
addASprite()
}
removeSprite()
}
func addASprite(){
sprite = SKSpriteNode(color: UIColor.orangeColor(), size: CGSize(width: 100, height: 100))
sprite!.position = touchLocation!
self.addChild(sprite!)
}
func removeSprite(){
if ((sprite?.containsPoint(touchLocation!) != nil)){
sprite?.removeFromParent()
}
}
Upvotes: 1
Views: 721
Reputation: 974
To remove touched node:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let touchedNode = nodeAtPoint(location)
touchedNode.removeFromParent()
}
Upvotes: 2