Jason Tavarez
Jason Tavarez

Reputation: 253

Subclassed SKSpriteNode touch event is ignored

So I'm pulling my hairs about this, I'm working on a little game using SpriteKit and Swift, and I've subclassed SKSpriteNode, added a few extra properties and overrode touchesBegan(), as such.

class Digit : SKSpriteNode {
  var userEntered = 0
  var index = 0
  ... (game properties)

  override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    println("Digit.touchesBegan()"
    ...
    (game logic)
  }
  override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {}
  override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {}
  override func touchesCancelled(touches: NSSet, withEvent event: UIEvent) {}

}

There is an array of these objects that I added to the base SKScene class ("GameScene"), which of course has its own logic and touchesBegan() function, which is there but doesn't have any code in it at the moment.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  println("GameScene.touchesBegan()")
  ...
}

Now I have logic related to the Digit class that needs to run when tapped, and the weird thing is that the app works perfectly fine in the simulator - Digit.touchesBegan() is called, however when running directly on the phone, everything but that works. The digit sprites are ignored and the base gameScene.touchesBegan() is called instead, as if the taps are bypassing the sprites. userInteractionEnabled is set to true, so I'm not sure if I'm missing something here or if this is because I have the target environment in build settings to iOS 7.1 (same as my actual phone). I admit I'm still learning some of this so any help is highly appreciated.

UPDATE

I think all of my issues are stemming from the fact that on the actual device, the nodes I've been adding are for some reason not being recognized as children (and hence nodeAtPoint() and overriding touchesBegan() aren't working, because the nodes aren't part of the scene tree). Not sure if this is a bug or not or something do to with AnyObject implementation with swift - since .children returns AnyObject but .parent does not, but I learned the following today:

self is the base SKScene class ("gameScene") and hud.hudNode is one node added as a child. Adding the following code:

println("self.children")
println(self.children)

println("hud.hudNode.parent")
println(hud.hudNode.parent)

results in the following in the debugger

self.children
nil
hud.hudNode.parent
<SKScene> name:'gameScene' frame:{{0, 0}, {320, 568}}

Which seems illogical, especially since this is only when testing on the actual device. In the simulator all child nodes show up as expected. Just in case anyone is running into this same issue, but still wondering why this is happening...

Upvotes: 3

Views: 859

Answers (1)

user1671097
user1671097

Reputation:

On SKSpriteNode class, you have to define only attributes.

SKSpriteNode class call on scene class and create action on touch function.

as an Example:

import UIKit
import SpriteKit

class Digit: SKSpriteNode {
    var isAlive:Bool = false {
        didSet {
            if isAlive {
                self.hidden = false
            }else {
                self.hidden = true
            }
        }
    }
   var numLivingNeighbors = 0
}

On Scene Class:

let classDigit = Digit(imageNamed: "hard_upper")
classDigit.hidden = false 
........

Upvotes: 0

Related Questions