Crashalot
Crashalot

Reputation: 34503

Code inside SKAction run action not executing

The code block inside a SKAction.run action never executes for some reason.

To clarify, it's the two lines inside of startAction that never run for some reason even though the other lines do run.

Putting breakpoints on those lines prove those lines never execute.

Any clue why?

    // Set first frame
    let firstFrame = frames[0]
    let animationNode = SKSpriteNode(texture: firstFrame)
    animationNode.position = CGPoint(x: x, y: y)

    // Set start action
    let startAction = SKAction.run({
        gAudio.playSound(file: .TestSound) // Never runs
        self.animationLayer.addChild(animationNode) // Never runs
    })

    // Set rest of animation
    let timePerFrame = 0.5
    let animationAction = SKAction.animate(with: frames, timePerFrame: timePerFrame, resize: false, restore: true)
    let removeAction = SKAction.removeFromParent()
    let animationSequence = SKAction.sequence([startAction, animationAction, removeAction])

    // Run animation
    animationNode.run(animationSequence)

Upvotes: 3

Views: 952

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16827

Actions will not fire for a node until it is placed on the scene, you have a chicken and egg dilemma going on here. You want to add the node (egg) to the scene (chicken) after the node (egg) exists in the world (chicken gives birth to the same egg). You need to have somethings else place the node on the scene, then the node will be able to run the actions.

Place your start action on your scene, and not your node, and it should start running

Upvotes: 3

Related Questions