首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于图像触摸检测的SKSpriteNode

基于图像触摸检测的SKSpriteNode
EN

Stack Overflow用户
提问于 2014-11-04 15:14:40
回答 1查看 232关注 0票数 0

我有一些图片中的SKSpriteNode:

当我试图检测到这样的触觉时:

代码语言:javascript
复制
-(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");
    }
}

它检测触摸,即使我触摸外面的蓝色形状在角落。触觉检测采用方形。怎么才能绕过它?我只想在布鲁形状内检测触觉。

马尔科

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-04 15:28:32

代码语言:javascript
复制
-(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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26738614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档