Swift 如何使精灵在其大小改变时沿圆形路径移动?

Swift 如何使精灵在其大小改变时沿圆形路径移动?,swift,path,sprite-kit,scale,Swift,Path,Sprite Kit,Scale,我已经说过球沿着一条圆形的路径,但唯一的问题是,当触球开始时,路径会增长,球会继续沿着路径运动。我尝试添加一个SKAction,但球没有沿着路径移动。希望有人能帮助我 class GameScene: SKScene { var circuloPrincipal = SKSpriteNode(imageNamed: "circulo") var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo") override func didM

我已经说过球沿着一条圆形的路径,但唯一的问题是,当触球开始时,路径会增长,球会继续沿着路径运动。我尝试添加一个
SKAction
,但球没有沿着路径移动。希望有人能帮助我

class GameScene: SKScene {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")

override func didMoveToView(view: SKView) {

  circuloPrincipal.size = CGSize(width: 35, height: 35)
  circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2 - 105)

  circuloPrincipal.color =  UIColor(red: 0.2, green: 0.9, blue: 0.6, alpha: 1.0)
  circuloPrincipal.colorBlendFactor = 1.0
  circuloPrincipal.zPosition = 3.0

  circuloFondo.size = CGSize(width: 300, height: 300)
  circuloFondo.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
  self.addChild(circuloFondo)
  circuloFondo.color = UIColor(red: 0.2, green: 0.9, blue: 0.6, alpha: 1.0)
  circuloFondo.colorBlendFactor = 1.0
  circuloFondo.zPosition = 1.0

  circuloPrincipal.position = CGPointMake( CGRectGetMidX(frame) , (CGRectGetMidY(frame) + circuloFondo.size.width/2) )
  addChild(circuloPrincipal)
  let dx = circuloPrincipal.position.x - frame.width / 2
  let dy = circuloPrincipal.position.y - frame.height / 2

  let radius = (circuloFondo.frame.size.width / 2) - 20.0

  let radian = atan2(dy, dx)
  let playerPath = UIBezierPath(
     arcCenter: CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)),
     radius: radius,
     startAngle: radian,
     endAngle: radian + CGFloat(M_PI * 4.0),
     clockwise: true)

  let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200)
  circuloPrincipal.runAction(SKAction.repeatActionForever(follow))

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  circuloFondo.runAction(SKAction.scaleTo(0.5, duration: 5))
}
class游戏场景:SKScene{
var circuloPrincipal=SKSpriteNode(图像名为:“circulo”)
var circuloFondo=SKSpriteNode(图像名为:“circuloFondo”)
覆盖func didMoveToView(视图:SKView){
circuloPrincipal.size=CGSize(宽:35,高:35)
circuloPrincipal.position=CGPoint(x:frame.width/2,y:frame.height/2-105)
circuloPrincipal.color=UIColor(红色:0.2,绿色:0.9,蓝色:0.6,阿尔法:1.0)
circuloPrincipal.colorBlendFactor=1.0
circuloPrincipal.zPosition=3.0
circuloFondo.size=CGSize(宽:300,高:300)
circuloFondo.position=CGPoint(x:frame.width/2,y:frame.height/2)
self.addChild(circuloFondo)
circuloFondo.color=UIColor(红色:0.2,绿色:0.9,蓝色:0.6,阿尔法:1.0)
circuloFondo.colorBlendFactor=1.0
circuloFondo.zPosition=1.0
circuloPrincipal.position=CGPointMake(CGRectGetMidX(框架),(CGRectGetMidY(框架)+circuloFondo.size.width/2))
addChild(圆形主体)
设dx=circuloPrincipal.position.x-frame.width/2
设dy=circuloPrincipal.position.y-frame.height/2
让半径=(circuloFondo.frame.size.width/2)-20.0
设弧度=atan2(dy,dx)
让playerPath=UIBezierPath(
arcCenter:CGPoint(x:CGRectGetMidX(帧),y:CGRectGetMidY(帧)),
半径:半径,
星际缠结:弧度,
端角:弧度+CGFloat(μPI*4.0),
顺时针:正确)
让follow=SKAction.followPath(playerPath.CGPath,asOffset:false,orientToPath:true,速度:200)
circuloPrincipal.runAction(SKAction.repeatActionForever(follow))
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
circuloFondo.runAction(SKAction.scaleTo(0.5,持续时间:5))
}
//新代码

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    //fondoTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("fondoEfecto"), userInfo: nil, repeats: true)

    if (circuloFondo.actionForKey("scaleTo") == nil) {
        let scale = SKAction.scaleTo(0.5, duration: 5)
        circuloFondo.runAction(scale,withKey:"scaleTo",optionalCompletion: {
 //here the error occours         


   // scaleTo is terminated
            // stop followTrackPath
            self.circuloPrincipal.removeActionForKey("followTrackForever")
            // re-run in the new path
            let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200)
            self.circuloPrincipal.runAction(SKAction.repeatActionForever(follow),withKey: "followTrackForever")
        })
    }
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
//FondTimer=NSTimer.scheduledTimerWithTimeInterval(1,目标:self,选择器:selector(“FondeFecto”),userInfo:nil,repeats:true)
if(circuloFondo.actionForKey(“scaleTo”)==nil){
让scale=SKAction.scaleTo(0.5,持续时间:5)
circuloFondo.runAction(缩放,带键:“缩放到”,可选完成:{
//这就是错误所在
//scaleTo被终止
//停止跟踪跟踪路径
self.circuloPrincipal.removeActionForKey(“followTrackForever”)
//在新路径中重新运行
让follow=SKAction.followPath(playerPath.CGPath,asOffset:false,orientToPath:true,速度:200)
self.circuloPrincipal.runAction(SKAction.repeatActionForever(follow),带有键:“followTrackForever”)
})
}

我认为您可以做一些更正,以帮助您完成项目的其余部分

以下内容适用于Swift 2.x,请勿在Swift 3.x中尝试:

首先,我想介绍这个有用的扩展:

extension SKNode
{
    func runAction( action: SKAction!, withKey: String!, optionalCompletion: dispatch_block_t? )
    {
        if let completion = optionalCompletion
        {
            let completionAction = SKAction.runBlock( completion )
            let compositeAction = SKAction.sequence([ action, completionAction ])
            runAction( compositeAction, withKey: withKey )
        }
        else
        {
            runAction( action, withKey: withKey )
        }
    }

    func actionForKeyIsRunning(key: String) -> Bool {
        if self.actionForKey(key) != nil {
            return true
        } else {
            return false
        }
    }
}
用法

self.runAction(myAction,withKey: "myActionName",optionalCompletion: {
    // do whatever you want when action is finished..
})

if self.actionForKeyIsRunning("myActionName") {
   // this action is up
}
放置此扩展名的位置:

也可以将此扩展放置在类的最后一个大括号之后,如以下示例所示:

class MyClass: SKScene {
   ...
}  // end of MyClass

extension SKNode
{
   ...
}

这条线可以变成:

    circuloPrincipal.runAction(SKAction.repeatActionForever(follow),withKey: "followTrackForever"))
scaleTo
行可以是:

    if !circuloFondo.actionForKeyIsRunning("scaleTo") {
            let scale = SKAction.scaleTo(0.5, duration: 5)
            circuloFondo.runAction(scale,withKey:"scaleTo",optionalCompletion: {
               // scaleTo is terminated
               // stop followTrackPath
               self.circuloPrincipal.removeActionForKey("followTrackForever")
               // re-run in the new path 
               let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200)
               self.circuloPrincipal.runAction(SKAction.repeatActionForever(follow),withKey: "followTrackForever"))
            })
    }

所有这些都需要在触摸开始?只有刻度线替换你对scaleIn是什么意思是终端?我有困难,因为这是甲烷储罐的新功能,我在哪里添加它?,“myAction”是一个我需要创建的skaction,对吗?你必须添加我的扩展才能有“完成”部分