重复 SKAction 起始位置变化
Repeat SKAction starting position changes
我有一条路径和一个 SKShapeNode,我想让 SKShapeNode 永远遵循这条路径(六边形)。
let pathHexagon:SKShapeNode = SKShapeNode()
let player:SKShapeNode = SKShapeNode()
pathHexagon.path = playerHexagonPath(pathHexRadius)
pathHexagon.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
pathHexagon.strokeColor = UIColor.blackColor()
pathHexagon.lineWidth = 5
addChild(pathHexagon)
player.path = playerPath(playerSize)
player.position = CGPoint(x: CGRectGetMidX(frame), y:CGRectGetMidY(frame))
player.strokeColor = UIColor.blackColor()
player.fillColor = UIColor.blackColor()
addChild(player)
let action = SKAction.followPath(playerHexagonPath(pathHexRadius), speed: CGFloat(300.0))
player.runAction(SKAction.repeatActionForever(action))
有点效果,但起始位置每个周期都在变化:
为什么会这样?
最终我想link这个动画给用户输入。作为练习,我正在尝试复制 Super Hexagon。因此,如果您知道更好的方法,请也告诉我。
这就是 asOffset
参数的作用:
@param asOffset If YES, the points in the path are relative offsets to
the node’s starting position. If NO, the points in the node are absolute
coordinate values.
所以根据我的理解,当我们将其设置为false时,路径中的点是节点父节点坐标系中的绝对坐标值。
你可以做两件事让它工作:
1) 制作容器并添加播放器
2) 改变场景的锚点,但我怀疑这适合你,因为你必须重新定位所有东西。
可能解决这个问题的方法不多,但我自己没试过。
选项 1:
let container = SKNode()
player.path = playerPath(playerSize)
player.position = CGPoint(x: 0, y: 0)
player.strokeColor = UIColor.blackColor()
player.fillColor = UIColor.blackColor()
container.addChild(player)
container.position = CGPoint(x:frame.midX, y:frame.midY)
addChild(container)
let action = SKAction.followPath(pathHexagon.path!, asOffset: false, orientToPath: true, speed: CGFloat(300.0))
player.runAction(SKAction.repeatActionForever(action))
或者,选项二:
self.anchorPoint = CGPoint(x:0.5, y:0.5)
mainHexagon.path = hexagonPath(mainHexRadius)
mainHexagon.strokeColor = UIColor.redColor()
mainHexagon.lineWidth = 10
addChild(mainHexagon)
pathHexagon.path = playerHexagonPath(pathHexRadius)
pathHexagon.strokeColor = UIColor.blackColor()
pathHexagon.lineWidth = 5
addChild(pathHexagon)
player.path = playerPath(playerSize)
player.position = CGPoint(x: 0, y: 0)
player.strokeColor = UIColor.blackColor()
player.fillColor = UIColor.blackColor()
addChild(player)
let action = SKAction.followPath(pathHexagon.path!, asOffset: false, orientToPath: true, speed: CGFloat(300.0))
player.runAction(SKAction.repeatActionForever(action))
我有一条路径和一个 SKShapeNode,我想让 SKShapeNode 永远遵循这条路径(六边形)。
let pathHexagon:SKShapeNode = SKShapeNode()
let player:SKShapeNode = SKShapeNode()
pathHexagon.path = playerHexagonPath(pathHexRadius)
pathHexagon.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
pathHexagon.strokeColor = UIColor.blackColor()
pathHexagon.lineWidth = 5
addChild(pathHexagon)
player.path = playerPath(playerSize)
player.position = CGPoint(x: CGRectGetMidX(frame), y:CGRectGetMidY(frame))
player.strokeColor = UIColor.blackColor()
player.fillColor = UIColor.blackColor()
addChild(player)
let action = SKAction.followPath(playerHexagonPath(pathHexRadius), speed: CGFloat(300.0))
player.runAction(SKAction.repeatActionForever(action))
有点效果,但起始位置每个周期都在变化:
为什么会这样?
最终我想link这个动画给用户输入。作为练习,我正在尝试复制 Super Hexagon。因此,如果您知道更好的方法,请也告诉我。
这就是 asOffset
参数的作用:
@param asOffset If YES, the points in the path are relative offsets to the node’s starting position. If NO, the points in the node are absolute coordinate values.
所以根据我的理解,当我们将其设置为false时,路径中的点是节点父节点坐标系中的绝对坐标值。
你可以做两件事让它工作:
1) 制作容器并添加播放器
2) 改变场景的锚点,但我怀疑这适合你,因为你必须重新定位所有东西。
可能解决这个问题的方法不多,但我自己没试过。
选项 1:
let container = SKNode()
player.path = playerPath(playerSize)
player.position = CGPoint(x: 0, y: 0)
player.strokeColor = UIColor.blackColor()
player.fillColor = UIColor.blackColor()
container.addChild(player)
container.position = CGPoint(x:frame.midX, y:frame.midY)
addChild(container)
let action = SKAction.followPath(pathHexagon.path!, asOffset: false, orientToPath: true, speed: CGFloat(300.0))
player.runAction(SKAction.repeatActionForever(action))
或者,选项二:
self.anchorPoint = CGPoint(x:0.5, y:0.5)
mainHexagon.path = hexagonPath(mainHexRadius)
mainHexagon.strokeColor = UIColor.redColor()
mainHexagon.lineWidth = 10
addChild(mainHexagon)
pathHexagon.path = playerHexagonPath(pathHexRadius)
pathHexagon.strokeColor = UIColor.blackColor()
pathHexagon.lineWidth = 5
addChild(pathHexagon)
player.path = playerPath(playerSize)
player.position = CGPoint(x: 0, y: 0)
player.strokeColor = UIColor.blackColor()
player.fillColor = UIColor.blackColor()
addChild(player)
let action = SKAction.followPath(pathHexagon.path!, asOffset: false, orientToPath: true, speed: CGFloat(300.0))
player.runAction(SKAction.repeatActionForever(action))