1
votes

I'm new in Sprite Kit and I have a strange problem with my GameScene. Can't figure out, what causes the problem. I present my scene from controller in viewWillAppearMethod in this way:

        let atlas = SKTextureAtlas(named: "Sprites")
        atlas.preload { [unowned self] in
        DispatchQueue.main.async {
            self.gameScene = GameScene(level: self.level, size: self.gameSKView!.bounds.size)
            self.gameScene.scaleMode = .resizeFill
            self.gameSKView?.presentScene(self.gameScene)
            self.gameSKView?.ignoresSiblingOrder = true
            self.gameSKView?.showsNodeCount = true
        }
    }

My sprite atlas content looks like: link

Than i create my spaceship:

final class SpaceshipSpriteNode: SKSpriteNode {
    required init(size: CGSize) {
        let texture = SKTexture(image: #imageLiteral(resourceName: "Spaceship"))

        super.init(texture: texture, color: .white, size: size)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

func configureSpaceship() {
    let middleRow = Int(Double(unwrappedMatrix.rowsCount) / 2)
    let middleColumn = Int(Double(unwrappedMatrix.columnsCount) / 2)
    let xOffset = CGFloat(level.startPoint.column - middleColumn)
    let yOffset = CGFloat(level.startPoint.row - middleRow)

    spaceship = SpaceshipSpriteNode(size: spaceshipSize)
    spaceship.position = CGPoint(x: frame.midX + (spaceshipSize.width * xOffset), y: frame.midY + (spaceshipSize.height * yOffset))
    spaceshipObject.addChild(spaceship)
    addChild(spaceshipObject)
}

configureSpaceship method is called in didMove(to view: SKView) The problem is that sometimes(1 per 3/4/5/6 cases) my spaceship is missing from the scene. Visibility, position, size are always the same, a count of the nodes on scene is the same too. Some images here link

1
Seems your spaceship is hiding behind a blue tile. Have you tried specifying zPosition of each node?OOPer
Go I've it a high zPosition - it might occasionally being rendered behind other objects: spaceship.zPosition = 100Steve Ives
@OOPer Thanks I have set zPozition in 1 and everything start work correctly.Bohdan Savych
@BohdanSavych, please post your solution as an answer. It may be simple, but we cannot confirm if it actually works.OOPer

1 Answers

1
votes

According to comments, I have changed zPosition for my objects:

tile.zPosition = 0
spaceship.zPosition = 1.0
backgroundSpriteNode.zPosition = -1

And everything start working correct, thanks guys.