Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKSpriteNode subclass and touches events

Tags:

ios

sprite-kit

I can't understand why if I subclass a SKSpriteNode, that implements the UIResponder protocol, the methods related with touches are never been called.

For example, this is a really simple class that I'm using as test:

//INTERFACE ----------------
#import <SpriteKit/SpriteKit.h>

@interface TestClass : SKSpriteNode

@end

//IMPLEMENTATION -----------
#import "TestClass.h"

@implementation TestClass


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     NSLog(@"oook");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}

@end

If I attach an instance of this class to the scene, touching the object... nothing happens.

Am I forgetting something?

like image 657
MatterGoal Avatar asked Feb 04 '26 17:02

MatterGoal


1 Answers

I've just seen that SKNode has the parameter userInteractionEnabled by default equal to NO. Just set it to YES to make the code works properly.

like image 155
MatterGoal Avatar answered Feb 06 '26 06:02

MatterGoal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!