I am pretty new to iOS, objective C and working with Xcode. I only did one simple news type app, but now I would like to create a game I had published on another platform.
I never used the SKSprite framework but I will try to help. First, your function "-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event" has to be set in your custom SKSpriteNode class, if not it's normal if that doesn't work.
If it's already done, you have to search where the tap is handle (by an other object) then you can use : "- (BOOL)gestureRecognizer:(UIPanGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer)otherGestureRecognizer"
hope that will help !
Your code is essentially correct. Just a few minor changes are required.
You have implemented the -touchesBegan:
method in your scene class. If you want to handle touches from within the scene class, then all the child nodes should have the userInteractionEnabled
property set to NO
. A sprite node's userInteractionEnabled
property should be set to YES
only when it is capable of handling touches on it's own.
So, in your case you should make the following changes:
1 - Set the spriteNode's userInteraction to NO.
mySKSpriteNode.userInteractionEnabled = NO;
2 - Change the way you check for your own sprite.
if([node isEqual: mySKSpriteNode])
You will find your code is working as intended.
Have you tried setting the name
property of your spritenode?
In your initialization for mySKSpriteNode
:
mySKSpriteNode = [[SKSpriteNode alloc] initWithTexture:sometexture];
mySKSpriteNode.name = @"thisIsMySprite"; // set the name for your sprite
mySKSpriteNode.userInteractionEnabled = NO; // userInteractionEnabled should be disabled
Then:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"thisIsMySprite"]) {
NSLog(@"mySKSpriteNode was touched!");
[node runAction:[SKAction fadeOutWithDuration:0]];
}
}
In Swift 2.0!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node.name == "spriteName" {
// Present new game scene
}
}