ios - 在 Xcode SpriteKit 中,使用 Swift 向一个方向展开 SKSpritenode
<p><p>我想让 <code>SKSpritenode</code> 只向一个方向扩展,在这种情况下只向上扩展。 </p>
<pre><code>override func didMoveToView(view: SKView) {
var rightlegs = SKSpriteNode()
rightlegs.size = CGSize(width: 10, height: 50)
rightlegs.color = SKColor.yellowColor()
rightlegs.position = CGPointMake(self.frame.width / 2 + 20, self.frame.height / 2 - 40)
self.addChild(rightlegs)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
</code></pre>
<p>所以.. 当我按下屏幕时,我希望高度扩大;然而,不是从两个方向。我该如何解决这个问题?感谢您的宝贵时间:)</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以通过更改 <a href="https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKSpriteNode_Ref/index.html#//apple_ref/occ/instp/SKSpriteNode/anchorPoint" rel="noreferrer noopener nofollow">anchorPoint</a> 来做到这一点 Sprite 的属性为 <code>CGPoint(x:0.5, y:0.0)</code> (这将底部对齐 Sprite 的纹理)并使用 <a href="https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instp/SKNode/yScale" rel="noreferrer noopener nofollow">yScale</a> 缩放 Sprite 的高度属性,像这样:</p>
<pre><code>import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let rightlegs = SKSpriteNode(color: .yellowColor(), size: CGSize(width: 10, height: 50))
override func didMoveToView(view: SKView) {
rightlegs.anchorPoint.y = 0.0
rightlegs.position = CGPointMake(self.frame.width / 2 + 20, self.frame.height / 2 - 40)
self.addChild(rightlegs)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Without animation
rightlegs.yScale += 0.2
}
}
</code></pre>
<p>或者您可以使用一些 SKAction 缩放方法使其动画化:</p>
<pre><code>override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//With animation
rightlegs.runAction(SKAction.scaleYTo(rightlegs.yScale + 0.2, duration: 0.2))
}
</code></pre>
<p>关于 <code>anchorPoint</code> 来自文档:</p>
<blockquote>
<p>Defines the point in the sprite that corresponds to the node’s
position.</p>
<p>You specify the value for this property in the unit coordinate space.
The default value is (0.5,0.5), which means that the sprite is
centered on its position.</p>
</blockquote>
<p><code>anchorPoint</code> 的基本作用是定义如何相对于节点的位置绘制纹理并影响节点的旋转和缩放...</p>
<p>Apple 的 <a href="https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html#//apple_ref/doc/uid/TP40013043-CH9-SW36" rel="noreferrer noopener nofollow">Working with Sprites</a> 中很好地解释了 anchor 的工作原理及其作用。文档部分。</p></p>
<p style="font-size: 20px;">关于ios - 在 Xcode SpriteKit 中,使用 Swift 向一个方向展开 SKSpritenode,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36218198/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36218198/
</a>
</p>
页:
[1]