ios - 通过滑动分割 SKTextureNode

标签 ios sprite-kit sprite skspritenode

我正在尝试使用滑动手势将 SKTextureNode 分成多个部分。我有进入和退出的坐标。

我如何使用它来分割节点?

最佳答案

我建议使用 SKPhysicsJointFixed 连接两个 SKSpriteNode 来形成一个对象。当用户在对象上滑动时,您可以通过移除关节来将其分割,并施加一个脉冲以将碎片发送到相反的方向。这是连接两个节点的方法:

- (void) connectNode1:(SKSpriteNode *)node1 toNode2:(SKSpriteNode *)node2
{
    CGPoint midPoint = CGPointMake((node1.position.x + node2.position.x)/2,
                                   (node1.position.y + node2.position.y)/2);

    SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody
                                                               bodyB:node2.physicsBody
                                                              anchor:midPoint];
    [self.physicsWorld addJoint:joint];
}

这是一个如何在触摸对象时分割对象的示例。这应该替换为滑动处理程序。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        // Determine which node was touched
        SKNode *touchedObject = [self nodeAtPoint:location];

        if (touchedObject == self) continue;

        // Check if node is connected
        if ([touchedObject.physicsBody.joints count]) {
            SKPhysicsJointFixed *joint = [touchedObject.physicsBody.joints firstObject];
            SKSpriteNode *node2 = (SKSpriteNode *)(touchedObject.physicsBody == joint.bodyA ?
                            joint.bodyB.node : joint.bodyA.node);

            [self.physicsWorld removeJoint:joint];
            CGFloat dx = touchedObject.position.x - node2.position.x;
            CGFloat dy = touchedObject.position.y - node2.position.y;

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

            // unit vector
            dx /= magnitude;
            dy /= magnitude;

            // send nodes in opposite directions
            [touchedObject.physicsBody applyImpulse:CGVectorMake(dx*20, dy*20)];
            [node2.physicsBody applyImpulse:CGVectorMake(-dx*20, -dy*20)];
        }
    }
}

关于ios - 通过滑动分割 SKTextureNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25179021/

相关文章:

ios - SpriteKit 和 Tiles - 处理非正方形的图 block

ios - 无法将 Sprite 添加到场景中

ios - 在 iPhone 5s 上将音频合并到视频时出现问题

python - Pygame:如何返回碰撞的 Sprite 的相对位置

iOS加入多张图片

ios - 为什么 Apple 使用特殊的 COREDATA_EXTERN 限定符而不只是 extern?

ios - 将任意信息附加到 SKSpriteNode

swift - 表达式类型 UIImage 在没有更多上下文的情况下不明确

php - 评级系统和 CSS Sprite

swift - 使 NSTimer 无效无效