[SOLVED] Clicking a SKSprite Node in swift


I am really really frustrated by this and I have been trying to get it to work for almost two full days now 🙁

How do I go about adding a click event to a SKSpriteNode in swift

  let InstantReplay:SKSpriteNode = SKSpriteNode(imageNamed: "InstantReplay")
  InstantReplay.position = CGPoint(x: size.width + InstantReplay.size.width/2, y: size.height/2)
  InstantReplay.size = CGSize(width: size.width/1.4, height: size.height/8)

  InstantReplay.runAction(SKAction.moveToX(size.width/2, duration: NSTimeInterval(1)))

All I want to happen is when “InstantReplay” is clicked for it to run a function called “InstantReplay_Clicked”, How would I go about Implementing this?

Any help much appreciated 🙂


Give your SKSpriteNode a name so we can identify it in the touchesBegan or touchesEnded method of your SKScene.

Also set userInteractionEnabled of your SKSpriteNode to false, so that it doesn’t trap touch events for itself, and not pass them up to the your Scene.

override init() {

    let instantReplay = SKSpriteNode(imageNamed: "InstantReplay")
    instantReplay.position = CGPoint(x: size.width + instantReplay.size.width/2, y: size.height/2)
    instantReplay.size = CGSize(width: size.width/1.4, height: size.height/8)
    instantReplay.name = "InstantReplay"; // set the name for your sprite
    instantReplay.userInteractionEnabled = false; // userInteractionEnabled should be disabled

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let location = touches.anyObject()?.locationInNode(self)
    let node = self.nodeAtPoint(location!)
    if (node.name == "InstantReplay") {
        println("you hit me with your best shot!")

(oh – i also renamed your instantReplay variable to use lowerCamelCase, in line with Swift best practices.

Answered By – Craig Grummitt

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *