sone3d
sone3d

Reputation: 25

SKSpritenode not responding in TouchesBegan

In my game, when player contact with a flag (using DidBeginContact) I add an SKSpritenode "nextlevel" (which is not responding in TouchesBegan and I don't know why. The NSLog code in TouchesBegan is not working.

This my code:

-(void)nextlevel
{
    nextlevel = [SKSpriteNode spriteNodeWithImageNamed:@"nextlevel.png"];
    nextlevel.userInteractionEnabled = NO;
    nextlevel.name = @"nextlevel";
    nextlevel.position = CGPointMake(self.size.width / 2.0, self.size.height / 2.0);
    [self addChild:nextlevel];
}


- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }


    if((firstBody.categoryBitMask == playerCategory && secondBody.categoryBitMask == flagCategory) ||
       (firstBody.categoryBitMask == flagCategory && secondBody.categoryBitMask == playerCategory))
    {
        // PLAYER WINS
        NSLog(@"player touches flag");
        SKAction * wait = [SKAction waitForDuration:1.2];
        SKAction *performSelector = [SKAction performSelector:@selector(nextlevel) onTarget:self];
       [self runAction:[SKAction sequence:@[wait, performSelector]]]; 
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    // Nextlevel splash screen
    if ([node.name isEqualToString:@"nextlevel"]) {
        NSLog(@“Next Level was touched!");
    }
}

Upvotes: 0

Views: 89

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

It's possible your SKSpriteNode is obstructed by another Node on top of it. Try setting the zPosition above all other nodes. Try different values depending on the zPositions of the other nodes in your scene.

nextlevel.zPosition = 10.0f; 

Upvotes: 1

Related Questions