PhysicsBody - Zero Using SpriteKit with Swift

I have a SpriteKit game that I create and I load a level from a multidimensional array. The loadlevel function works for the first time. It does not work if I perform println physicsBodyon the assignment physicsBody(after physicBody initialization). When I delete all tiles with removeChildrenInArray, the second time I run the download level, it gives an error message fatal error: unexpectedly found nil while unwrapping an Optionaland points to the line below printlnbelow. And printlnindicates that it is physicsBody nil. In my opinion, there are no reasons why fresh initialization physicsBodyshould be nil. printlnprints physicsBody nil. I have no idea why it physicsBodywill be nil. I'm just trying to reach the reset level by deleting all the nodes of the blocks and adding new ones to the original place according to the level map.

func loadLevel() {
    var levels = Levels().data
    var frameSize = view.frame.size
    var thisLevel = levels[currentLevel]
    println("running load level")
    for (rowIndex,row) in enumerate(thisLevel) {
        for (colIndex,col) in enumerate(row) {
            if col == 4 {
                continue
            }
            println("COL: \(col)")
            var tile = SKSpriteNode(texture: SKTexture(imageNamed: "brick_\(tileMap[col])"))
            tile.name = "tile_\(rowIndex)_\(colIndex)"
            tile.position.y = frameSize.height - (tile.size.height * CGFloat(rowIndex)) - (tile.size.height/2)
            tile.position.x = tile.size.width  * CGFloat(colIndex) + (tile.size.width/2)
            var physicsBody = SKPhysicsBody(rectangleOfSize: tile.size)
            tile.physicsBody = physicsBody
            tile.physicsBody.affectedByGravity = false
            tile.physicsBody.categoryBitMask = ColliderType.Block.toRaw()
            tile.physicsBody.contactTestBitMask = ColliderType.Ball.toRaw()
            tile.physicsBody.collisionBitMask = ColliderType.Ball.toRaw()
            scene.addChild(tile)
            tileCount++
        }
    }
}

Here is my ColliderType

enum ColliderType:UInt32 {
    case Paddle = 1
    case Block = 2
    case Wall = 3
    case Ball = 4
}

This is my content of the reset function:

func reset() {
    tileCount = 0
    var removeTiles = [SKSpriteNode]()
    // remove all the tiles
    for child in scene.children {
        var a_tile = child as SKSpriteNode
        if a_tile.name.hasPrefix("tile_") {
            a_tile.removeFromParent()
            a_tile.name = ""
            removeTiles.append(a_tile)
        }
    }
    removeTiles.removeAll(keepCapacity: false)
    ball!.position = CGPoint(x: 200, y: 200)
    ballVel = CGPoint(x: 0, y: -5)
    currentLevel++
    loadLevel()
    lost = false
    won = false
}

Level structs

struct Tile {
    let map = ["blue","green","purple","red"]
}

struct Levels {
    let data = [
        [
            [4,4,0,4,0,4,0,4,0,4,0,4,0,0,4,4],
            [4,4,1,4,1,4,1,4,1,4,1,4,1,1,4,4],
            [4,4,2,4,2,4,2,4,2,4,2,4,2,2,4,4],
            [4,4,3,4,3,4,3,4,3,4,3,4,3,3,4,4]
        ],
        [
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
            [2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2],
            [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
        ]
    ]
}

Swift, , .

+4
1

, SKPhysicsBody . , :

var physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(100, 100))

SKSpriteNode , CGSize, initWithTexture:color:size:

+1

All Articles