【问题标题】:SKSpriteNode from image touch detection来自图像触摸检测的 SKSpriteNode
【发布时间】:2014-11-04 15:14:40
【问题描述】:

我有一些像这样的图像中的 SKSpriteNode:

当我尝试像这样检测触摸时:

-(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:@"shape"]) {
        NSLog(@"TOUCH DETECT");
    }
}

即使我触摸角落的外部蓝色形状,它也会检测到触摸。它采用方形进行触摸检测。如何绕过它?我只想检测蓝光形状内的触摸。

马尔科

【问题讨论】:

    标签: ios touch sprite-kit shape


    【解决方案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:@"shapeMask"]) {
            //Whatever you want.
        }
    }
    -(void)blueShape{
        SKSpriteNode *blueShape = [SKSpriteNode spriteNodeWithImageNamed:@"blueShape"];
        blueShape.position = yourPosition;
        blueShape.name = @"blueShape";
        [self addChild:blueShape];
    
        SKShapeNode *shapeMask = [SKShapeNode node];
        shapeMask.name = @"shapeMask";
        CGFloat offsetX = blueShape.frame.size.width * blueShape.anchorPoint.x;
        CGFloat offsetY = blueShape.frame.size.height * blueShape.anchorPoint.y;
    
        CGMutablePathRef path = CGPathCreateMutable();
    
        CGPathMoveToPoint(path, NULL, 0 - offsetX, 137 - offsetY);
        CGPathAddLineToPoint(path, NULL, 80 - offsetX, 1 - offsetY);
        CGPathAddLineToPoint(path, NULL, 239 - offsetX, 2 - offsetY);
        CGPathAddLineToPoint(path, NULL, 318 - offsetX, 139 - offsetY);
        CGPathAddLineToPoint(path, NULL, 238 - offsetX, 275 - offsetY);
        CGPathAddLineToPoint(path, NULL, 80 - offsetX, 274 - offsetY);
        //Values may not be accurate. You can set yourself with shape tool.
        CGPathCloseSubpath(path);
    
        [shapeMask setPath:path];
        [blueShape addChild:shapeMask];
    }
    

    【讨论】:

    • 我有这个设置,(我编辑问题)但它仍然检测到蓝色形状不可见的角落的触摸。
    • 如果你的图片是 png。它可以检测到已经是蓝色的形状。 en.wikipedia.org/wiki/Portable_Network_Graphics
    • 我有 png 图片。你可以试试(它是有问题的粘贴)。它有透明的背景
    • 使用这个方法。我测试了它适用于您的图像。顺便说一下,你可以用这个工具来做形状节点dazchong.com/spritekit
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多