ios - 用滑动分割 SKTextureNode
<p><p>我正在尝试使用滑动手势将 <code>SKTextureNode</code> 拆分为多个部分。我有进入和退出的坐标。</p>
<p>如何使用它来拆分节点?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我建议使用 SKPhysicsJointFixed 连接两个 SKSpriteNode 以形成一个对象。当用户在对象上滑动时,您可以通过移除关节来拆分它,并施加插入力以将碎片朝相反的方向发送。这是连接两个节点的方法:</p>
<pre><code>- (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];
;
}
</code></pre>
<p>这里有一个例子,如果它被触摸,如何分割它。这应该替换为滑动处理程序。</p>
<pre><code>- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = ;
// Determine which node was touched
SKNode *touchedObject = ;
if (touchedObject == self) continue;
// Check if node is connected
if () {
SKPhysicsJointFixed *joint = ;
SKSpriteNode *node2 = (SKSpriteNode *)(touchedObject.physicsBody == joint.bodyA ?
joint.bodyB.node : joint.bodyA.node);
;
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
;
;
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 用滑动分割 SKTextureNode,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25179021/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25179021/
</a>
</p>
页:
[1]