Kevin Wang
Kevin Wang

Reputation: 49

SKTileMap Not Displaying Any Tiles

I'm creating this simple platformer game using SpriteKit in Swift Playgrounds as a small project.

However, when I created the SKTileSet, the TileMap, etc. and ran the playground, none of the tiles actually showed up.

The other SKSpriteNodes, which I've created also in the same SpriteKit Scene File, displays perfectly.

The code:

GameScene.swift

class GameScene: SKScene {

    var worldLayer: Layer!
    var backgroundLayer: RepeatingLayer!
    var mapNode: SKNode!
    var tileMap: SKTileMapNode!

    var lastTime: TimeInterval = 0
    var dt: TimeInterval = 0

    override func didMove(to view: SKView) {
        createLayers()
    }

    func createLayers() {
        worldLayer = Layer()
        worldLayer.zPosition = 2
        addChild(worldLayer)
        worldLayer.layerVelocity = CGPoint(x: -200.0, y: 0.0)

        backgroundLayer = RepeatingLayer()
        backgroundLayer.zPosition = 0
        addChild(backgroundLayer)

        for i in 0...1 {
            let backgroundImage = SKSpriteNode(imageNamed: "Background")
            backgroundImage.name = String(i)
            backgroundImage.scale(to: frame.size, width: false, multiplier: 1.0)
            backgroundImage.anchorPoint = CGPoint.zero
            backgroundImage.position = CGPoint(x: 0.0 + CGFloat(i) * backgroundImage.size.width, y: 0.0)
            backgroundLayer.addChild(backgroundImage)
        }

        backgroundLayer.layerVelocity = CGPoint(x: -100.0, y: 0.0)

        load(level: "Level_0-1")
    }

    func load(level: String) {
        if let levelNode = SKNode.unarchiveFromFile(file: level) {
            mapNode = levelNode
            worldLayer.addChild(mapNode)
            loadTileMap()
        }
    }

    func loadTileMap() {
        if let groundTiles = mapNode.childNode(withName: "GroundNode") as? SKTileMapNode {
            tileMap = groundTiles
            tileMap.scale(to: frame.size, width: false, multiplier: 1.0) 
            PhysicsHelper.addPhysicsBody(to: tileMap, and: "ground")
            for child in groundTiles.children {
                if let sprite = child as? SKSpriteNode, sprite.name != nil {
                    ObjectHelper.handleChild(sprite: sprite, with: sprite.name!)
                }
            }

        }
    }
}

How may I fix this issue?

Is it related to the fact that I am using Swift Playgrounds instead of it being a project?

I have confirmed that there are no spelling errors and that it all runs i.e. there are actually the tiles as nodes in the background, but none of it shows up.

What I mean is that with the tiles deleted, the screen will only contain 31 nodes.

But when I added the TileMap and the tiles, the screen will then contain 162 nodes, but the tiles just don't show up.

Thanks for the help, I am really struggling with this.

Upvotes: 1

Views: 302

Answers (2)

Martin
Martin

Reputation: 4765

In my case I assigned a tile set to it. Then I modified this tile set to the sample one or I don't remember exactly what I've done. Anyway, I clicked on the "Tile Sets" dropdown in the Interface Builder, then re-selected the tile set and it appeared. (My problem was that I saw the tiles in the interface builder but nothing when I ran the app on device.)

Upvotes: 0

ZeeBlueAngel
ZeeBlueAngel

Reputation: 37

I had similar troubles playground and tilemap seems to have a conflict Put your code in xcode project and see if it works

Upvotes: 0

Related Questions