2017-04-27 75 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) 

希望這可以解決您的問題!