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

前端之家收集整理的这篇文章主要介绍了swift – Sprite-kit:在圆形路径中移动元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正试图让元素在cercle的边缘移动.

>我已经在屏幕中间创建并定位了一个cercle:

  1. var base = SKShapeNode(circleOfRadius: 200 ) // Size of Circle
  2. base.position = CGPointMake(frame.midX,frame.midY)
  3. base.strokeColor = SKColor.blackColor()
  4. base.glowWidth = 1.0
  5. base.fillColor = UIColor(hue:1,saturation:0.76,brightness:0.94,alpha:1)
  6. base.zPosition = 0
  7. self.addChild(base)

>然后我创建了另一个精灵并在其上添加了一个动作:

  1. main = SKSpriteNode(imageNamed:"Spaceship")
  2. main.xScale = 0.15
  3. main.yScale = 0.15
  4. main.zPosition = 1
  5. let circle = UIBezierPath(roundedRect: CGRectMake((self.frame.width/2) - 200,CGRectGetMidY(self.frame) - 200,400,400),cornerRadius: 200)
  6. let circularMove = SKAction.followPath(circle.CGPath,duration: 5.0)
  7. main.runAction(SKAction.repeatAction(circularMove,count: 2))
  8. self.addChild(main)

宇宙飞船的第一次旋转(见下图)完全遵循圆的边缘,但第二次迭代改变了宇宙飞船的位置并将其移出屏幕边界.
这是正常的还是我做错了什么?

谢谢.

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

关于docs中的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:

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

猜你在找的Swift相关文章