sprite kit:验证触摸是否在节点内

时间:2014-11-10 21:11:19

标签: ios ios7 touch sprite-kit

我试图找出如何检测我是否在sprite kit中触摸节点。我认为它在UIView中的功能如下:

[myView pointInside:point withEvent:nil];

但到目前为止,我无法找到替代方案。我想要完成的是知道我在屏幕上触摸了精灵节点。

如果你能帮助我完成这个

,我真的很感激

以下是我的代码中的内容。我正在添加动画节点:

-(void)addMyNodeAnimated
{
    NSMutableArray *myNodeArray = [NSMutableArray array];
    NSArray *animatedFrames = [NSArray new];
    SKTextureAtlas *AnimatedAtlas = [SKTextureAtlas atlasNamed:@"pc2"];
    int numImages = (int)AnimatedAtlas.textureNames.count;

    for (int i=1; i <= numImages; i++) {
        NSString *textureName = [NSString stringWithFormat:@"%@-%d", @"pc2", i];
        SKTexture *temp = [AnimatedAtlas textureNamed:textureName];
        [myNodeArray addObject:temp];
    }
    animatedFrames = myNodeArray;

    SKTexture *temp = animatedFrames[0];
    SKSpriteNode *animationNode = [SKSpriteNode spriteNodeWithTexture:temp];
    animationNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    animationNode.userInteractionEnabled = YES;
    animationNode.name = @"AnimationNode";
    [self addChild:animationNode];

    [animationNode runAction:[SKAction repeatActionForever:
                                  [SKAction animateWithTextures:animatedFrames
                                                   timePerFrame:0.1f
                                                         resize:NO
                                                        restore:YES]] withKey:@"AnimationRuning"];

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint _touchLocation = [[touches anyObject] locationInNode:self];

     SKNode *node = [self nodeAtPoint:_touchLocation];

    if (node != nil)
    {
        NSLog(@"node name %@", node.name);
    }

}

当我触摸屏幕中的节点时,节点返回null。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:4)

-(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:@"yourSpriteName"] && [node.name isEqualToString:@"yourEffectName"]) {
        //Whatever you want.
    }
}