ios - 拖放椭圆形的 SKSpriteNode

标签 ios objective-c cocoa-touch sprite-kit

我有一个只有一个 Sprite 的场景,用于 Sprite 的图像是椭圆形的。

Poker chip

我面临的问题是,每当我使用 touchAtPoint 时,即使触摸发生在图像的透明区域,它也会返回 sprite。

我该如何解决?

我当前的代码如下:

-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {

        self.backgroundColor = [SKColor blackColor];
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Red"];
        sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:50.0];
        sprite.physicsBody.affectedByGravity = NO;
        sprite.physicsBody.usesPreciseCollisionDetection = YES;
        [self addChild: sprite];

    }

    return self;
}

- (void)didMoveToView:(SKView *)view {

    UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
    [self.view addGestureRecognizer:gestureRecognizer];

}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {

    if (recognizer.state == UIGestureRecognizerStateBegan) {

        CGPoint touchLocation = [recognizer locationInView:recognizer.view];
        touchLocation = [self convertPointFromView:touchLocation];
        SKNode *touchedNode = (SKNode *)[self nodeAtPoint:touchLocation];
        _selectedNode = touchedNode;

    }
    else if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = CGPointMake(translation.x, -translation.y);
        [self panForTranslation:translation];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    }
}

- (void)panForTranslation:(CGPoint)translation {
    CGPoint position = [_selectedNode position];
    [_selectedNode setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)];
}

最佳答案

好的,所以这里唯一相关的代码是...

CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [self convertPointFromView:touchLocation];
SKNode *touchedNode = (SKNode *)[self nodeAtPoint:touchLocation];
_selectedNode = touchedNode;

这需要改成这样...

CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [self convertPointFromView:touchLocation];
SKNode *touchedNode = (SKNode *)[self nodeAtPoint:touchLocation];

// Work out if the touch is inside the circle
CGPoint centerOfNode = CGPointMake(touchedNode.position.x + touchedNode.size.width * 0.5, touchedNode.positiony + touchedNode.size.height * 0.5);

CGFloat dx = touchedNode.position.x - touchLocation.x;
CGFloat dy = touchedNode.position.y - touchLocation.y;

CGFloat distanceFromCenter = sqrtf(dx * dx + dy * dy);

if (distanceFromCenter <= touchedNode.size.width * 0.5) {
    _selectedNode = touchedNode;
}

关于ios - 拖放椭圆形的 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163604/

相关文章:

ios - 如何用弹出窗口样式显示 UISplitViewController 的主视图?

iphone - UIButton边框和背景图片

ios - Swift/Alamofire 完成处理程序的困难

ios - 以秒、分钟、小时为单位的耗时

ios - 如何围绕屏幕中心旋转 Sprite 节点?

objective-c - AVPlayer 不在文档目录 iOS 中播放 MP3 音频文件

iphone - 为 iOS 创建本地化文件

objective-c - 我可以将自定义 UI 元素添加到 UIActionSheet 吗?

javascript - 如果在获取子字符串时重复,正则表达式将忽略起始字符

android - 错误 : unable to locate asset entry in pubspec. yaml: "assets/fonts/Lato-Regular.ttf"