0

I am working on a GameScene and I set up some nodes in the scene editor, since I want to place them visually in the level. The nodes have all names going from "platform1" to "platform5". Depending on the level there are more or less platform nodes.

when the level is loaded I want to enumerate over all nodes with the title like "platform*" and put them into an array. For now I use enumerateChildNodesWithName but I dont get the code in the block working correct. This is what I have so far:

    enumerateChildNodesWithName("//platform*", usingBlock: {node, _ in
        if let platform = node as? PlatformNode {
            print(platform.name)
        }
    })

And it prints out following error message:

CUICatalog: Invalid Request: requesting subtype without specifying idiom

But the platform name isn't printed out. Any ideas how to achieve this?

My next goal would be to out every single platform into an array, so I can access the properties of every platform though the array.

Does someone have a helping hand?

Thanks in advance...

0

2 Answers 2

1

If you add the platforms to an SKNode container in the scene editor, they are automatically added to the container's children array (in the order they are added). You can then access the platforms with

    if let platforms = childNodeWithName("platforms") {
        for platform in platforms.children {
            print ("\(platform.name)")
        }
    }

To add the platforms to an SKNode in the scene editor, add an SKNode (listed as an Empty) to the editor, set the SKNode's name appropriately (e.g., platforms), and set the parent of each platform by setting platform's parent property in the editor (see image below).

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Your search string is not correct. You can do it like this:

class PlatformNode:SKSpriteNode{

}

class GameScene: SKScene {

override func didMoveToView(view: SKView) {


    let p1 = PlatformNode()
    p1.name = "platform1"

    let p2 = PlatformNode()
    p2.name = "platform2"

    let p3 = PlatformNode()
    p3.name = "platform3"

    let p4 = PlatformNode()
    p4.name = "platform4"

    let p5 = PlatformNode()
    p5.name = "platform5"


    addChild(p1)
    addChild(p2)
    addChild(p3)
    addChild(p4)
    addChild(p5)


    enumerateChildNodesWithName("platform[1-3]", usingBlock: {node, _ in
        if let platform = node as? PlatformNode {
            print(platform.name)
        }
    })  
}

}

This will give you all the platforms named platform1,platform2 and platform3. To retrieve nodes named platform4 and platform5, you will change search string to this:

"platform[1-5]"

3 Comments

I also thought of adding the nodes to the scene like proposed, but I wanted to add the nodes in the scene editor. Because these node change from level to level and I wanted to place these nodes visually and not with code. Any ideas how to get the nodes that way into an array?
@boehmatron Like Epsilon pointed, when node is added to the parent, it is automatically added to parent's children array. So I guess that is the answer to your question if all of your nodes have the same parent. If not, you can preform advanced search or, to enumerate each parent one by one (again, like from Epsilon's answer).
Thank you for your Input, this seems to work very well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.