swift - swift 重复一个功能

标签 swift sprite-kit repeat

我制作了这个抖动功能来尝试为我的游戏中的菜单标题添加抖动效果。以为我觉得这是一个很长的路要走!有没有更短/更简洁的方法来执行这样的重复操作?

还有可能让这个永远重复吗?

func shakeMenu(){

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute:
        {

    let moveNodeRight = SKAction.moveBy(x: 7.0, y: 0.0, duration: 0.05)
    let moveNodeLeft = SKAction.moveBy(x: -7.0, y: 0.0, duration: 0.05)
    let moveNodeRight1 = SKAction.moveBy(x: 7.0, y: 0.0, duration: 0.05)
    let moveNodeLeft1 = SKAction.moveBy(x: -7.0, y: 0.0, duration: 0.05)
    let moveNodeRight2 = SKAction.moveBy(x: 7.0, y: 0.0, duration: 0.05)
    let moveNodeLeft2 = SKAction.moveBy(x: -7.0, y: 0.0, duration: 0.05)
    let moveNodeRight3 = SKAction.moveBy(x: 7.0, y: 0.0, duration: 0.05)
    let moveNodeLeft3 = SKAction.moveBy(x: -7.0, y: 0.0, duration: 0.05)
    let moveNodeRight4 = SKAction.moveBy(x: 7.0, y: 0.0, duration: 0.05)


    self.shake = SKAction.sequence([moveNodeRight, moveNodeLeft, moveNodeRight1, moveNodeLeft1, moveNodeRight2, moveNodeLeft2, moveNodeLeft3, moveNodeRight3, moveNodeRight4])
    self.menu.run(self.shake)

    })
}





func shakeMenu(){

    let moveRight: SKAction = .moveBy(x: 7.0, y: 0.0, duration: 0.05)
    let sequence: SKAction = .sequence([moveRight, moveRight.reversed()]
    self.menu.run(.repeat(sequence), count: 4)

}

最佳答案

SKAction.repeatForever ( https://developer.apple.com/reference/spritekit/skaction/1417676-repeatforever ) 怎么样

这样你就可以写:

let moveNodeRight = SKAction.moveBy(x: 7.0, y: 0.0, duration: 0.05)
let moveNodeLeft = SKAction.moveBy(x: -7.0, y: 0.0, duration: 0.05)
let moveSequence = SKAction.sequence([moveNodeRight, moveNodeLeft])
let moveForever = SKAction.repeatForever(moveSequence)

self.menu.run(moveForever)

希望对你有帮助。

关于swift - swift 重复一个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099210/

相关文章:

javascript - Angular : add a dynamic attribute

c# - For 循环在完成后重复并返回 ArgumentOutOfRangeException

ios - 即使登录错误也能登录

ios - SwiftUI 可以将 ObservableObject 放入另一个 ObservableObject 中吗?

ios - 由于 cocoa pod 中的内部保护错误快速类功能而无法访问

ios - SpriteKit - SKCameraNode 视差效果?

swift - 针对特定手势的同时手势识别

ios - 如何让两个相同的 UISwipeGestureRecognizers 根据触摸位置同时工作?

ios - 暂停除一个节点的动画之外的所有内容?

algorithm - 查找重复字符的函数式编程算法