Adding multiple SKSpriteNodes at once

hello,

My game is simple, yet it reads as "HIgh energy" usage. I am cleaning the code. the current code I have works, but when i try to access one of the nodes i added it does not work.

var GameCenterBtn: SKSpriteNode!;
    var newGameBtn: SKSpriteNode!;
    var StoreBtn: SKSpriteNode!;
    var ModeBtn: SKSpriteNode!;
    var Flip: SKSpriteNode!;
    var RemoveAdsBtn: SKSpriteNode!;
    var Flop: SKSpriteNode!;
    var background1: SKSpriteNode!;
    var background2: SKSpriteNode!;



override func didMove(to view: SKView) {
       SKNode.connectNode(names: [RemoveAdsBtn , ModeBtn , GameCenterBtn , newGameBtn , StoreBtn , Flop , Flip , background1 , background2 ] , nodeName: [ "RemoveAds" , "GameMode" , "GameCenter" , "StartGame" , "UpGrade" , "FlopText" , "FlipText" , "Background1" , "Background2" ], selector: self)
        print("finished")
        RemoveAdsBtn.removeFromParent()
        print("got this far")



public extension SKNode{
    public static func connectNode( names: [SKSpriteNode?] , nodeName: [String] , selector: SKScene){
        var names = names
        for i in 0...8{
            print(i)
            print(names[i] as SKSpriteNode!)
            print(nodeName[i])
            names[i] = selector.childNode(withName: nodeName[i]) as! SKSpriteNode!;
            print("done", i)
        }
    }
}

it only prints out "finished". But i do not know why I cannot access the nodes I've added with connectNodes func

Are you just trying to create variables for sprites you've laid out in an SKScene file? If so, you could just write...


self.enumerateChildNodes(withName: "//*"){
            node, stop in
if (node.name == "GameCenter"){

          if ( node is SKSpriteNode){
               GameCenterBtn = node as! SKSpriteNode  //for style, gameCenterBtn preferred (lowercase G)
          }

     }
}


That enumerator is going to look through all children (even within other nodes). Might be easier than what you're trying to do with the SKNode extension. Obviously you'll have to do it for each element, but from what I'm seeing it doesn't look like thats more than 8 or so node.


Also you don't need the ; with Swift.

Right now i have this


RemoveAdsBtn = self.childNode(withName: "RemoveAds") as! SKSpriteNode!;
        ModeBtn = self.childNode(withName: "GameMode") as! SKSpriteNode!;
        GameCenterBtn = self.childNode(withName: "GameCenter") as! SKSpriteNode;
        newGameBtn = self.childNode(withName: "StartGame") as! SKSpriteNode;
        StoreBtn = self.childNode(withName: "UpGrade") as! SKSpriteNode;
        Flop = self.childNode(withName: "FlopText") as! SKSpriteNode;
        Flip = self.childNode(withName: "FlipText") as! SKSpriteNode;
        background1 = self.childNode(withName: "Background1") as! SKSpriteNode;
        background2 = self.childNode(withName: "Background2") as! SKSpriteNode;

and i want to turn that into one line of code which there's 2 arrays.

i just want to turn those 9 lines of code into one line. I use this extension :

public extension SKNode{
    public static func connectNode( names: [SKSpriteNode?] , nodeName: [String] , selector: SKScene){
        var names = names
        for i in 0...8{
            print(i)
            print(names[i] as SKSpriteNode!)
            print(nodeName[i])
            names[i] = selector.childNode(withName: nodeName[i]) as! SKSpriteNode!;
            print("done", i)
        }
    }
}

and it is implemented like this.


SKNode.connectNode(names: [RemoveAdsBtn , ModeBtn , GameCenterBtn , newGameBtn , StoreBtn , Flop , Flip , background1 , background2 ] , nodeName: [ "RemoveAds" , "GameMode" , "GameCenter" , "StartGame" , "UpGrade" , "FlopText" , "FlipText" , "Background1" , "Background2" ], selector: self)

I am just trying to reduce the energy usage of my app. If you have any tips for how to reduce energy usage that'd also be helpful but for right now I'm just trying to get this function to work.

So I guess no one else has any ideas either lol

Accepted Answer

Your code can't work. Your sprite node variables (such as RemoveAdsBtn) all start out as nil. You form the "names" array from the values of these variables, so you have an array of 8 nils. In your static method, you copy that array to a mutable array, then replace the value in each element by some node reference, then you throw the mutable array away (when it goes out of scope at line 11). Calling "connectNode" achieves nothing.


But it's not clear why you're doing this. What makes you think that your new code is any more energy-efficient than the original code? You're still doing 8 "childNode(withName:)" calls, plus some additional overhead, so it's going to burn more energy, not less.


Since your original code was only executed once per scene, it can't have been the reason for ongoing high energy usage. For that, it has to be something that's being done repeatedly. For example, if you do work during the scene's "update" method, you may be able to do it more efficiently. Or, it's possible that Sprite Kit apps are inherently energy hogs to some degree, since they're potentially updating the screen 60 times a second.


You need to find out what's actually causing the enery usage before you can try to reduce it.

I agree, I think its just saying High Energy because its a game. I wouldn't worry about it.

Adding multiple SKSpriteNodes at once
 
 
Q