0
votes
        let randomize = SKAction.runBlock({ [unowned self] in
            self.footstepFile = "Content/footstep\(RandomInt(1, max: 4))"
            print(self.footstepFile)
        })


        sprite.runAction(SKAction.repeatActionForever(SKAction.sequence([randomize, SKAction.playSoundFileNamed(footstepFile, waitForCompletion: true)])), withKey: "footsteps")

When this action runs, the footstepFile shows its randomizing when I print it, but in reality it's just playing the same sound file over and over. Why is this?

I play this action whenever the sprite is moving, and pause it whenever he stops. When it pauses and unpauses the footstepFile changes but if I'm continuously running, it just plays the same one over and over. Shouldn't the runblock be randomizing it continuously?

1

1 Answers

0
votes

I believe the issue is that the footstepFile that is active when the SKAction.sequence is created is the one that is used repeatedly. This is because the sequence only gets created once, and then used repeatedly.

To solve this, try creating an array of playSoundFileNamed actions all using random file names and pass that to SKAction.sequence:

var actions = [SKAction]()

for _ in 1...16 {
    footstepFile = "Content/footstep\(RandomInt(1, max: 4))"
    actions.append(SKAction.playSoundFileNamed(footstepFile, waitForCompletion: true))
}

sprite.runAction(SKAction.repeatActionForever(SKAction.sequence(actions)), withKey: "footsteps")