ios - 缩放用 SKSpriteNodes 制作的钟摆
<p><p>我有一个带钟摆的工作时钟,但摆臂不能正确缩放。这是缩放之前的样子...</p>
<p> <a href="/image/nbBJQ.png" rel="noreferrer noopener nofollow"><img src="/image/nbBJQ.png" alt="enter image description here"/></a> </p>
<p>然后,在缩放之后……</p>
<p> <a href="/image/gVYQd.png" rel="noreferrer noopener nofollow"><img src="/image/gVYQd.png" alt="enter image description here"/></a> </p>
<p>钟面按比例缩小,摆臂(绿线)也按比例缩小,但看起来它围绕其中心点而不是钟面中心的支点进行缩放。我可以通过设置秋千的 <code>anchorPoint</code> 来修复这种缩放,使其向一端而不是中心缩放...</p>
<pre><code>swing.anchorPoint = CGPointMake(0, 0.5);
</code></pre>
<p>但是这样做会导致钟摆不摆动。 (我认为是因为物理力被施加到摇摆的 <code>anchorPoint</code> 上,如果我将它移动到一端,它位于固定的销 anchor 上。</p>
<p><strong>我怎样才能让 ARM 朝着支点缩放并仍然允许它摆动?</strong></p>
<p>这是代码...</p>
<pre><code>// in my SKScene
ClockFace *face = ; // just an SKNode subclass
face.name = @"face";
face.position = CGPointMake(150, 150);
;
// add the pendulum
SKSpriteNode *fulcrum = size:CGSizeMake(5, 5)];
;
fulcrum.physicsBody = ;
fulcrum.physicsBody.dynamic = NO;
SKSpriteNode *swing = size:CGSizeMake(190, 2)];
;
swing.position = CGPointMake(95, 0);
swing.physicsBody = ;
SKPhysicsJointPin *pinJoint = ;
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在 Swift 3 中这个例子:</p>
<p> <a href="/image/SgfCz.gif" rel="noreferrer noopener nofollow"><img src="/image/SgfCz.gif" alt="enter image description here"/></a> </p>
<pre><code> let nodeSize = CGSize(width: 10, height: 10)
let node = SKSpriteNode(color: .red, size: nodeSize)
node.physicsBody = SKPhysicsBody(rectangleOf: nodeSize)
node.physicsBody?.isDynamic = false
self.addChild(node)
let node2Size = CGSize(width: 60, height: 8)
let node2 = SKSpriteNode(color: .green, size: node2Size)
node2.position = CGPoint(x: 5, y: 0)
node2.anchorPoint = CGPoint(x: 0.0, y: 0.5) // <- New Line
node2.physicsBody = SKPhysicsBody(rectangleOf: node2Size)
node2.physicsBody?.mass = 1.0
self.addChild(node2)
// Scale Line
node2.run(SKAction.repeatForever(SKAction.sequence([
SKAction.scale(to: 0.2, duration: 1.0),
SKAction.scale(to: 1.5, duration: 0.5),
])))
// Anchor Point
let a = SKPhysicsJointPin.joint(withBodyA: node.physicsBody! , bodyB: node2.physicsBody!, anchor: CGPoint(x: 0.0, y: 0.0))
self.physicsWorld.add(a)
</code></pre></p>
<p style="font-size: 20px;">关于ios - 缩放用 SKSpriteNodes 制作的钟摆,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/45259118/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/45259118/
</a>
</p>
页:
[1]