Ios SKAction.rotate不工作吗?
我正在尝试创建一个节点,并使用一个动作设置其zRotation属性的动画,但是,当尝试运行该动作时,我的节点的zRotation属性没有更改。我不确定这为什么不起作用Ios SKAction.rotate不工作吗?,ios,swift,sprite-kit,skspritenode,skaction,Ios,Swift,Sprite Kit,Skspritenode,Skaction,我正在尝试创建一个节点,并使用一个动作设置其zRotation属性的动画,但是,当尝试运行该动作时,我的节点的zRotation属性没有更改。我不确定这为什么不起作用 import UIKit import SpriteKit let aim = SKSpriteNode() print(aim.zRotation) aim.zRotation = CGFloat(Double.pi/2) print(aim.zRotation) let myAction = SKAction.rotat
import UIKit
import SpriteKit
let aim = SKSpriteNode()
print(aim.zRotation)
aim.zRotation = CGFloat(Double.pi/2)
print(aim.zRotation)
let myAction = SKAction.rotate(toAngle: CGFloat(3.14), duration: 0)
aim.run(myAction)
print(aim.zRotation)
带输出的代码图像
只有当节点位于场景中且该场景依次显示在视图中时,才会计算SKAction: SKAction对象是由中的节点执行的操作 场景(SKScene)。。。当场景处理其节点时,操作 将计算与这些节点关联的节点 (来自) 目前,你没有这些东西,所以行动仍然处于休眠状态。有关将节点添加到场景并在操场中显示该场景的示例,请参见。我猜您希望实现类似的效果 问题 您的代码中有一些错误:
import PlaygroundSupport
import SpriteKit
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 500, height: 500))
let scene = SKScene(size: CGSize(width: 500, height: 500))
scene.anchorPoint.x = 0.5
scene.anchorPoint.y = 0.5
sceneView.showsFPS = true
sceneView.presentScene(scene)
PlaygroundPage.current.liveView = sceneView
let square = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
square.fillColor = .red
scene.addChild(square)
square.run(.rotate(toAngle: 3.14, duration: 20))
谢谢,一旦我将此节点作为子节点添加到我的场景对象(myScene.addChild(aim))中,操作就可以运行了!谢谢你的意见。我使用节点旋转属性来确定枪射击的角度,因此不需要向节点添加图像。当我将节点添加为场景对象的子对象时,操作正常。