【问题标题】:sprite kit: verify if touch is inside a nodesprite kit:验证触摸是否在节点内
【发布时间】:2015-01-07 07:16:23
【问题描述】:

我试图弄清楚如何检测我是否触摸了精灵套件中的节点。我认为它在 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。

有人知道这是为什么吗?

【问题讨论】:

    标签: ios ios7 touch sprite-kit


    【解决方案1】:
    -(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.
        }
    }
    

    【讨论】:

    • 不幸的是,这对我不起作用,因为我正在使用忍者刀片效果,并且只检测具有刀片效果的节点。
    • 我在添加精灵节点的地方添加了代码,但我无法检测到何时触摸精灵。触摸的节点总是返回null。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多