为什么repeatForever不能在spritekit中运行,swift3

时间:2017-04-27 03:15:22

标签: swift3 sprite-kit sprite

这是我的测试代码:

    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)

此云只运行一次,请告诉我解决方案

1 个答案:

答案 0 :(得分: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)

希望这能解决你的问题!