SKSpritenode在TouchesBegan中没有响应

时间:2015-02-22 18:02:57

标签: xcode sprite-kit

在我的游戏中,当玩家与旗帜接触时(使用DidBeginContact),我添加了SKSpritenode" nextlevel" (在TouchesBegan中没有回复,我也不知道为什么。在TouchesBegan中的NSLog代码无效。

这是我的代码:

-(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!");
    }
}

1 个答案:

答案 0 :(得分:1)

您的SKSpriteNode可能被其上的另一个节点阻挡。 尝试将zPosition设置为高于所有其他节点。根据场景中其他节点的zPosition尝试不同的值。

nextlevel.zPosition = 10.0f;