Search code examples
swiftsprite-kitcontactsskspritenode

mutliple nodes in GameScene.sks with same name


I'm trying to make a simple 2D Platformer game. Instead of importing the sprites, I decided to use the ones the SpriteKit engine provides: in GameScene.sks I dragged and dropped a sprite (simple, red square) which I called "wall". I copied and pasted it to have 2 walls the player could jump on. In the code part I wrote the following lines:

var wall = SKSpriteNode()

and then, in the "didMoveToView" function:

    wall = self.childNodeWithName("wall") as! SKSpriteNode
    wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
    wall.physicsBody?.affectedByGravity = false
    wall.physicsBody?.dynamic = true
    wall.physicsBody?.allowsRotation = false
    wall.physicsBody?.pinned = true
    wall.physicsBody?.categoryBitMask = physicsCategories.wall
    wall.physicsBody?.contactTestBitMask = physicsCategories.player

since I've created a struct outside the class ("physicsCategories") holding the various categories bit masks, when I check for contacts it works just fine:

 //CONTACT
func didBeginContact(contact: SKPhysicsContact) {
    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contactMask {
    case physicsCategories.player | physicsCategories.wall:
        isGrounded = true
    default:
        print("Some other contact")
    }
}

My problem is that this works only with one of the 2 sprites with the name "wall". Is there a way to reference in code ALL the sprites in the scene which have that same name? Like a tag or something like "self.childrenNodesWithName(...)", which I know doesn't exist unluckly. Or maybe am I doing something wrong? Thank you in advance.


Solution

  • I've not my mac with me, this is a screenshot from the web:

    enter image description here