Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 710
Crashalot Avatar asked Jul 15 '26 09:07

Crashalot


1 Answers

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

like image 138
Knight0fDragon Avatar answered Jul 17 '26 22:07

Knight0fDragon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!