2017-04-27 68 views
2

这是我的测试代码:为什么repeatForever不spritekit工作,swift3

let cloud1 = SKSpriteNode(imageNamed: "cloud1") 
    cloud1.position = CGPoint(x: size.width + cloud1.size.width/2, y: size.height/2) 
    addChild(cloud1) 
    let wait = SKAction.wait(forDuration: 0.25) 

    let actionMove = SKAction.move(to: CGPoint(x: -cloud1.size.width/2, y: cloud1.position.y), duration: 2.0) 

    let sequence = SKAction.sequence([actionMove, wait]) 

    let repeatAction = SKAction.repeatForever(sequence) 
    cloud1.run(repeatAction) 

这种云只运行一次,请告诉我该解决方案请

+0

移动到CGPoint(x:-cloud1.size.width/2,y:cloud1.position.y)后,您希望进一步移动到哪里? – matt

+0

我只是想要它从中间左侧再次移动到右侧。任何想法 –

+0

什么会使它做到这一点?我没有看到你的序列中有任何动作。你告诉它向左移动,这就是你所要做的。 – matt

回答

3

根据你的问题,我想想我可能有一个解决方案。

为什么它在达到某个目的地后没有移动的原因是因为您正在使用moveTo。以下是我根据您提供的代码所做的一个小例子。

var cloudSpeed = CGFloat(12) //this can be any number you want, the higher the faster 


    let cloud1 = SKSpriteNode(imageNamed: "cloud1") 
    cloud1.position = CGPoint(x: size.width/2, y: size.height/2) 
    addChild(cloud1) 
    let wait = SKAction.wait(forDuration: 0.25) 

    let actionMove = SKAction.move(by: CGVector(dx:-10 * ballSpeed, dy:0), duration: 1) //experiment with the dx value. If you want the cloud to travel from right to left make it a negative 

    let sequence = SKAction.sequence([actionMove, wait]) 

    let repeatAction = SKAction.repeatForever(sequence) 
    cloud1.run(repeatAction) 

希望这可以解决您的问题!