ios - SKSpriteNode 来自图像触摸检测

标签 ios touch sprite-kit shapes

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

enter image description here

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

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

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

马可

最佳答案

-(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];
}

关于ios - SKSpriteNode 来自图像触摸检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738614/

相关文章:

iphone - 获取触摸坐标(0-480和0-320的形式)

ios - 扩展数组以附加 SKTextures

swift - 在运行时创建常量,但在其他函数中访问常量

ios - 如何将现有对象保存到 Core Data

iphone - InfoPlist.strings 路径和 SVN

java - 在 Android 上检测后台的屏幕触摸实例

cocoa - 从许多其他音频的部分制作音频

ios - 像 Instagram 一样暂停/恢复视频录制

iphone - 如何加密捆绑的文本/json 文件?

swift - runAction() 内的变化值没有改变