Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在SpriteKit中同步纹理、动画和声音_Ios_Swift_Sprite Kit_Skaction - Fatal编程技术网

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

Ios 如何在SpriteKit中同步纹理、动画和声音,ios,swift,sprite-kit,skaction,Ios,Swift,Sprite Kit,Skaction,我正在使用SpriteKit创建一个RPGGameKit来帮助我开发iOS游戏。现在我的播放器可以移动了,我添加了动画和音频系统 我遇到了一个同步纹理和声音的问题。就像我的球员走路时的一步 let atlas = SKTextureAtlas(named: "Walk") let textures = atlas.getTextures() // I created an extension that returns textures of atlas let walkin

我正在使用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,sound,oneSecond,frame3,sound,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

下面类似的东西会起作用-让sequence=SKAction.sequence([walkinganimation,playsound])。我试着使用组,序列,但是声音播放一次,动画继续。我希望能够在每次帧更改时播放声音。您的动画中有多少帧?这取决于动画,但我想抽象此部分。我不想指定每个帧,但我想我必须指定!我会试着回来,谢谢。我认为唯一的其他方法是为你的动画找到/创建匹配的声音。在找到更好的方法之前,我将使用这些代码!