Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't tap SKSpriteNode - no touch detected ios

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.

Basically, I have one object that will appear randomly and then fade out and the purpose is to tap on the object to make it go away.

However I can't seem to tap the object, even though I set mySKSpriteNode.userInteractionEnabled = YES;

Here is what I have in my touchesBegan method:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    NSLog(@"Something was touched");
    UITouch *touch = [touches anyObject];
    NSLog(@"%@", touch);
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if(node == mySKSpriteNode)
    [node runAction:[SKAction fadeOutWithDuration:0]];

}

In my log I get this when i tap the screen (not where I have objects):

2014-02-17 23:18:30.870 BubbleChallenge[48541:70b] Something was touched
2014-02-17 23:18:30.875 BubbleChallenge[48541:70b] <UITouch: 0x1130ea530> phase: Began tap count: 1 window: <UIWindow: 0x109c2ab60; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x109c274d0>; layer = <UIWindowLayer: 0x109c26810>> view: <SKView: 0x109c2e5e0; frame = (0 0; 320 568); autoresize = RM+BM; layer = <CAEAGLLayer: 0x109c0e180>> location in window: {147.5, 128.5} previous location in window: {147.5, 128.5} location in view: {147.5, 128.5} previous location in view: {147.5, 128.5}

When I touch mySKSpriteNode I get nothing in the log, not even "Something was touched";

Any idea why this could be happening?

The other questions I found said the answer is to set userInteractionEnabled = YES.... maybe there's a specific point in the code where I should set this?..

All help is appreciated!

like image 993
Deea B Avatar asked Feb 17 '14 21:02

Deea B


Video Answer


1 Answers

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]];
    }
}
like image 102
DoctorClod Avatar answered Nov 08 '22 10:11

DoctorClod