我试图用滑动手势将SKTextureNode
分割成碎片。我有进入和退出的坐标。
我如何使用它来分割节点?
答案 0 :(得分:3)
我建议使用SKPhysicsJointFixed连接两个SKSpriteNodes以形成一个对象。当用户在对象上滑动时,您可以通过移除关节来分割它并施加冲动以相反的方向发送碎片。这是连接两个节点的方法:
- (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)];
}
}
}
答案 1 :(得分:1)
在这种情况下,有一种与SKTexture相关的方法可以提供帮助。
[SKTexture textureWithRect:someRect inTexture:someTexture];
您可以通过根据坐标计算两个rects,并将它们分配给两个SKSpriteNode对象来使用此方法。
但是,这只能用于生成所讨论纹理的矩形切口。
另外,请查看documentation。