gpt4 book ai didi

swift - Sprite-kit:在圆形路径中移动元素

转载 作者:IT王子 更新时间:2023-10-29 05:51:02 25 4
gpt4 key购买 nike

我正在尝试让一个元素移动到圆的边缘。

  • 我在屏幕中间创建并放置了一个圆圈:
var base = SKShapeNode(circleOfRadius: 200 ) // Size of Circle
base.position = CGPointMake(frame.midX, frame.midY)
base.strokeColor = SKColor.blackColor()
base.glowWidth = 1.0
base.fillColor = UIColor(hue:1, saturation:0.76, brightness:0.94, alpha:1)
base.zPosition = 0
self.addChild(base)
  • 然后我创建了另一个 Sprite 并在其上添加了一个 Action :
main = SKSpriteNode(imageNamed:"Spaceship")
main.xScale = 0.15
main.yScale = 0.15
main.zPosition = 1
let circle = UIBezierPath(roundedRect: CGRectMake((self.frame.width/2) - 200, CGRectGetMidY(self.frame) - 200,400, 400), cornerRadius: 200)
let circularMove = SKAction.followPath(circle.CGPath, duration: 5.0)
main.runAction(SKAction.repeatAction(circularMove,count: 2))
self.addChild(main)

宇宙飞船的第一次旋转(见下图)正好沿着圆圈的边缘旋转,但第二次迭代改变了宇宙飞船的位置并将其移出屏幕边界。这是正常现象还是我做错了什么?

谢谢。

enter image description here

最佳答案

+ follow:duration:产生与 follow:asOffset:orientToPath:duration: 相同的效果asOffsetorientToPath 参数均设置为 true

关于文档中的 asOffset 参数:

If true, the points in the path are relative offsets to the node’s starting position. If false, the points in the node are absolute coordinate values.

因此,您应该明确地将其设置为 false:

let circularMove = SKAction.follow(circle.CGPath, asOffset: false, orientToPath: true, duration: 5)

关于swift - Sprite-kit:在圆形路径中移动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35988452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com