如何在swift中旋转SKNode



我想让我的SKNode像下图一样旋转:)

图片在这里

相反,它在屏幕左下角旋转!点击这里查看我不想要的视频

如何让它在一个位置逆时针或顺时针旋转,如上图所示?

提前感谢你的人谁可以帮助我。不确定我是否必须改变锚点或什么…谢谢你

下面是我在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 */

}
}

默认情况下,SKNode锚点始终为0.5,0.5。这意味着您需要调整位置,以便所有内容都偏离节点的中心。

现在一切都是相对的,所以你的顶部和底部是相对于你的环节点的。

然后你需要移动箍结的位置,使它在你想要的地方。

代码如下:
(注意,我去掉了所有不必要的代码,让你的图像在中心旋转)
(另一个节点:如果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 */

} }

精灵的锚点是什么?精灵围绕它们的锚点旋转,而你的锚点似乎被设置为(0,0),即左下角。如果是,尝试将其更改为(0.5,0.5)

最新更新