如何快速旋转 SKNode
Posted
技术标签:
【中文标题】如何快速旋转 SKNode【英文标题】:How to rotate SKNode in swift 【发布时间】:2016-10-17 00:02:41 【问题描述】:我希望我的 SKNode 像下图一样旋转 :)
Image Here
而是围绕屏幕的左下角旋转! Click Here To View Video of what is happening that I do not want
如何让它逆时针或顺时针旋转到一个位置,如上图所示?
提前感谢能帮助我的人。不知道我是否必须更改锚点或什么...谢谢
下面是我的 swift 代码。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate
var top = SKSpriteNode()
var bottom = SKSpriteNode()
var line = SKSpriteNode()
var RightSide = SKSpriteNode()
var LeftSide = SKSpriteNode()
var pointBar = SKSpriteNode()
var Second_point_Bar_For_First_Hoop = SKSpriteNode()
override func didMove(to view: SKView)
physicsWorld.contactDelegate = self
createHoop()
func createHoop()
top = SKSpriteNode(imageNamed: "top")
top.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 15)
top.size = CGSize(width: 100, height: 60)
top.zPosition = 0
bottom = SKSpriteNode(imageNamed: "bottom")
bottom.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 45)
bottom.size = CGSize(width: 100, height: 60)
bottom.zPosition = 2
LeftSide = SKSpriteNode()
LeftSide.position = CGPoint(x: bottom.position.x - 40, y: bottom.position.y)
LeftSide.size = CGSize(width: 10, height: 10)
LeftSide.zPosition = 0
LeftSide.color = UIColor.blue
RightSide = SKSpriteNode()
RightSide.position = CGPoint(x: bottom.position.x + 40, y: bottom.position.y)
RightSide.size = CGSize(width: 5, height: 10)
RightSide.zPosition = 0
RightSide.color = UIColor.blue
pointBar = SKSpriteNode()
pointBar.position = CGPoint(x: bottom.position.x, y: bottom.position.y + 10)
pointBar.size = CGSize(width: 90, height: 2)
pointBar.zPosition = 100
pointBar.color = UIColor.green
pointBar.zPosition = 100
Second_point_Bar_For_First_Hoop = SKSpriteNode()
Second_point_Bar_For_First_Hoop.position = CGPoint(x: top.position.x, y: top.position.y - 10)
Second_point_Bar_For_First_Hoop.size = CGSize(width: 90, height: 2)
Second_point_Bar_For_First_Hoop.zPosition = 100
Second_point_Bar_For_First_Hoop.color = UIColor.green
Second_point_Bar_For_First_Hoop.zPosition = 100
let hoopPair = SKNode()
hoopPair.addChild(top)
hoopPair.addChild(pointBar)
hoopPair.addChild(Second_point_Bar_For_First_Hoop)
hoopPair.addChild(bottom)
hoopPair.addChild(LeftSide)
hoopPair.addChild(RightSide)
let rotate = SKAction.rotate(byAngle: 1, duration: 5)
let repeatRotation = SKAction.repeatForever(rotate)
hoopPair.run(repeatRotation)
self.addChild(hoopPair)
override func update(_ currentTime: CFTimeInterval)
/* Called before each frame is rendered */
【问题讨论】:
【参考方案1】:精灵的锚点是什么?精灵围绕它们的锚点旋转,您的似乎设置为 (0,0),即左下角。如果是这样,请尝试将其更改为 (0.5,0.5)
【讨论】:
史蒂夫,通常这是正确的,我投了赞成票,但他的问题是他的锚点设置为 0.5,0.5,但他的节点只画在 1 个象限(右上象限) ,而不是全部 4,所以答案是 (0.75,0.75) 将锚点放在 1 个象限的中心,或者将项目放置在所有 4 个象限中 史蒂夫,是的,我认为你非常接近:)。它围绕 0,0 的锚点旋转,但我似乎无法弄清楚如何更改我的 SKNode 周围的锚点。 :) 你试过node.anchorPoint = CGPoint(0.5, 0.5) 刚刚查了一下,SKNode 没有锚点,@AmenParham,你将不得不将你的孩子集中在主父母中,只需删除所有那些 .self.frame.width / 2,和.self.frame.height/2。你应该没事 Knight0fDragon 所说的是正确的。将子代定位在父代 (hoopPair) 中是错误的。【参考方案2】:默认情况下,SKNode 锚点始终为 0.5、0.5。这意味着您需要处理这些位置,以使所有内容都离开节点的中心。
现在一切都是相对的,所以你的顶部和底部是相对于你的箍节点的。
然后你需要移动 hoopnode 的位置,让它在你想要的位置。
这是执行此操作的代码:
(请注意,我取出了所有不必要的代码来让您的图像在中心旋转)
(另一个节点:如果size
不起作用,使用frame.size
)
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate
var top = SKSpriteNode() var bottom = SKSpriteNode() var line = SKSpriteNode() var RightSide = SKSpriteNode() var LeftSide = SKSpriteNode() var pointBar = SKSpriteNode() var Second_point_Bar_For_First_Hoop = SKSpriteNode()
override func didMove(to view: SKView)
physicsWorld.contactDelegate = self
createHoop()
func createHoop()
top = SKSpriteNode(imageNamed: "top")
top.size = CGSize(width: 100, height: 60)
top.position = CGPoint(x: 0, y: top.size.height/2)
top.zPosition = 0
bottom = SKSpriteNode(imageNamed: "bottom")
bottom.size = CGSize(width: 100, height: 60)
bottom.position = CGPoint(x: 0, y: -bottom.size.height/2)
bottom.zPosition = 2
let hoopPair = SKNode()
hoopPair.addChild(top)
hoopPair.addChild(bottom)
let rotate = SKAction.rotate(byAngle: 1, duration: 5)
let repeatRotation = SKAction.repeatForever(rotate)
hoopPair.position = CGPoint(x:self.size.width/2,self.size.height/2)
hoopPair.run(repeatRotation)
self.addChild(hoopPair)
override func update(_ currentTime: CFTimeInterval)
/* Called before each frame is rendered */
【讨论】:
甜蜜。谢谢,它有效。不知道我必须那样做。谢谢你对我的朋友的教育:)当然还有其他人:)以上是关于如何快速旋转 SKNode的主要内容,如果未能解决你的问题,请参考以下文章
如何从 SKNode.children 数组访问 SKNode 子类方法?