微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 SpriteKit 中同步纹理动画和声音

如何解决如何在 SpriteKit 中同步纹理动画和声音

我正在使用 SpriteKit 创建一个 RPGGameKit 来帮助我开发我的 iOS 游戏。现在我的播放器可以移动了,我添加了动画和音频系统。

我遇到了同步纹理和声音的问题。就像我的球员走路时的一步。

let atlas = SKTextureAtlas(named: "Walk")
let textures = atlas.getTextures() // I created an extension that returns textures of atlas

let walkingAnimation = SKAction.animate(with: textures,timePerFrame: 1)

因此,walkingAnimation 将遍历纹理并每 1 秒更改一次。

现在,我想在纹理改变时播放行走的声音。

我查看了 SKAction 和 SpriteKit 文档,但没有针对此 SKAction 的回调。

如果你想和我一起完成这件事,或者你有什么想法,请发表评论

谢谢:)

解决方法

试试这个:

  let frame1 = SKAction.setTexture(yourTexture1)
  let frame2 = SKAction.setTexture(yourTexture2)
  let frame3 = SKAction.setTexture(yourTexture3)
  //etc

  let sound = SKAction.playSoundFileNamed("soundName",waitForCompletion: false)
  let oneSecond = SKAction.wait(forDuration: 1)
  let sequence = SKAction.sequence([frame1,sound,oneSecond,frame2,frame3,oneSecond])

  node.run(sequence)
,

所以,现在我打算这样做:

let textures = SKTextureAtlas(named: "LeftStep").getTextures()
var actions = [SKAction]()

for texture in textures {
    
    let group = SKAction.group([
        SKAction.setTexture(texture),SKAction.playSoundFileNamed("Step.mp3",waitForCompletion: false)
    ])

    let sequence = SKAction.sequence([
        group,SKAction.wait(forDuration: 0.5)
    ])

    actions.append(sequence)

}

self.node.run(SKAction.repeatForever(SKAction.sequence(actions)))

感谢@StefanOvomate

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。