I'm trying to trigger a touch event on a SKSpriteNode where it's child node is tapped on. When the child node it touched, the event isn't triggered. I have found a hack work around using
.parent
but doesn't feel like the most efficient or elegant way of doing is.Please see code below:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch = touches.first! as UITouch let location = touch.locationInNode(self) let node = self.nodeAtPoint(location) if node is PlanListItem || node.parent is PlanListItem { for plan in planListItems as [PlanListItem] { plan.selected = false } // Some more code... } }
Help much appreciated.
score:5
Accepted answer
You can do this inside the node subclass:
class PlanListItem:SKSpriteNode {
var isSelected: Bool = false
override init(texture size etc) {
//your init
self.userInteractionEnabled = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("PlanListItem touched")
isSelected = !isSelected
}
}
Credit To: stackoverflow.com
More questions
- Touch detection of SKSpriteNode with child nodes
- How to get all child nodes with name in Swift with Scene Kit
- Physics body size not changing with SKSpriteNode size change in Touch moved method
- How to efficiently link child nodes from the SKS with code in the scene
- Face Detection with Camera
- Delegate methods in child class sometimes not called with Swift 5 compiler
- iOS Universal Device App with SpriteKit, how to scale nodes for all views?
- How: class that inherits SKSpriteNode with init, that can call init(color:, size:)
- Detect touch on child node of object in SpriteKit
- What is an example of drawing custom nodes with vertices in swift SceneKit?
- Increase touch target size of UISlider with Swift
- Using 3D Touch with storyboard's peek and pop
- UICollectionView with full page cells VS UIScrollView with child view controllers
- Issues with 3D Touch and state restoration
- Retrieve all values from a sub child in Firebase with Swift iOS
- Swift SKSpriteNode with UIImage
- How to copy SKSpriteNode with SKPhysicsBody?
- Bug: hit-testing with sibling nodes and the userInteractionEnabled property in Sprite Kit
- Firebase, get immediate parent of a child with specific value
- Rotate sprite by touch with a limited rotation speed
- Confusion about coordinates, frames & child nodes in SpriteKit on iOS?
- TouchesMoved() lagging very inconsistently when there is a SkSpriteNode with a physics body
- How To Properly Add Child View Controller in iOS 8 With Swift
- SwiftUI: Custom button does not recognize touch with clear background and buttonStyle
- Passing data between UIPageViewController Child views with Swift
- How to know which SKSpriteNode is affected by collision detection in Swift?
- ScrollView Doesn't Scroll with Geometry Reader as Child
- How to remove all nodes with a specific name spritekit
- Apple Vision – Barcode Detection doesn't work for barcodes with different colours
- Fading a shadow together with the SKSpriteNode that casts it
More questions with similar tag
- Drawing performance with Swift in Xcode for iOS app
- When a widget opened an app, can the app knows the widget size
- organizing dates in uitableview sections
- Swift Dictionary: join key and value into string
- Does XCGLogger use os_log for fast logging?
- Differences between subscript and functions in Swift
- How to avoid passing currentTime about in SpriteKit?
- Sqlite File Location Core Data
- UIImage(named: String) or UIImage swift 4
- How does a method get called twice?
- How can I use the OR operator with optional binding?
- Multi-level generic type constraints in Swift
- How to access username and password entered into a WKWebView in Swift 3
- why I can't keep observable alive after .onError() even I already use error handling like .catchError() onRxSwift?
- With which method has Apple replaced "dragImage:"?