Adding SKAction to SKSpriteNode in a SKReference

Home 4 Forums General Support Adding SKAction to SKSpriteNode in a SKReference

This topic contains 3 replies, has 2 voices, and was last updated by  Justin 8 months, 4 weeks ago.

  • February 14, 2018 at 10:57 am #180274

    I’d like to create a hud, with various details including a progress bar. I’d built the progress bar by creating an SKSpriteNode and used an action to stretch it over time so it looked like it was filling up to 100%

    Using the basic spritekit default template and justins code:

    func findChildByNameAndDoSomething( theName: String) {
    self.enumerateChildNodes(withName: “//*”){
    node, stop in
    if (node.name == theName) {
    if let someSpriteNode:SKSpriteNode = node as? SKSpriteNode {
    someSpriteNode.size.width = someSpriteNode.size.width + 10
    print(“found it”)
    someSpriteNode.run(SKAction(named: “Pulse”)!, withKey: “fadeInOut”)
    }
    }
    }
    }

    Having added an SKSpriteNode named “redBox” to a new .sks file then dragging this onto the GameScene – this creates a referenceNode with my redBox on GameScene.

    then calling: findChildByNameAndDoSomething(theName: “redBox”)

    finds the node and changes the width by 10, but wont’ run the action ‘Pulse’

    If redBox is an SKSpriteNode directly on the GameScene it works as expected and the SKAction works…?

    Have I missed something?

  • February 15, 2018 at 11:13 am #180297

    Thats an interesting one. I can’t think of why that Pulse action would / would not be found in that case.

    You might want to just programmatically create the pulse. I’m guessing its a fade in / fade out effect. Just a few extra lines of code.

    But I’ll testify in court, your original code SHOULD work. =)

  • February 16, 2018 at 7:55 am #180309

    Thanks Justin,

    we’ll it turns out that the nodes within in SKReferenceNodes seem to be paused by default, and this is probably a bug.

    https://forums.developer.apple.com/thread/84719

    The ‘solution’ to my problem above is to set the target SKNode isPaused to false, before you run the action. You can see what’s going on here if you add some print statements as follows:

        func findChildByNameAndDoSomething( theName: String) {
            self.enumerateChildNodes(withName: "//*"){
                node, stop in
                if (node.name == theName) {
                    if let someSpriteNode:SKSpriteNode = node as? SKSpriteNode {
                        print("found it")
                        print("\(someSpriteNode.name!) actions:\(someSpriteNode.hasActions()) paused:\(someSpriteNode.isPaused)")
                        if let someAction = SKAction(named: "Pulse") {
                            someSpriteNode.isPaused = false
                            someSpriteNode.run(someAction, withKey: "fadeInOut")
                        }
                        print("\(someSpriteNode.name!) actions:\(someSpriteNode.hasActions()) paused:\(someSpriteNode.isPaused)")
                    }
                }
            }
        }

    Log out put gives:
    (first tap)
    found it
    redBox actions:false paused:true
    redBox actions:true paused:false
    (following taps)
    found it
    redBox actions:false paused:false
    redBox actions:true paused:false

    and the ‘redBox’ SKSpriteNode does its ‘Pulse’ action at last. I hope my two days of searching for this this will help someone else!
    cheers

    Will

    • February 16, 2018 at 1:32 pm #180328

      Wow. Who would have thought! Yeah, that might be worth a Bug Report.

You must be logged in to reply to this topic.