简体   繁体   English

为什么RepeatForever在Spritekit,Swift3中不起作用

[英]Why repeatForever is not working in spritekit, swift3

This is my code for test: 这是我的测试代码:

    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)

This cloud is only run one time, please tell me the solution please 此云仅运行一次,请告诉我解决方案

based on your question, I think I might have a solution. 根据您的问题,我想我可能有解决方案。

the reason as to why it is not moving after it reaches a certain destination is because you are using moveTo . 之所以在到达某个目的地后不移动的原因是因为您使用的是moveTo Here is a little example I made based on the code you have provided. 这是我根据您提供的代码制作的一个小示例。

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)

hopefully this solves your problem! 希望这可以解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM