以编程方式创建SKTileMapNode

时间:2017-07-29 14:47:03

标签: ios swift sprite-kit

我正在尝试以编程方式在Swift中创建一个SKTileMapNode,通过分配交替的图块(这些将是更复杂的组合,这只是一个试验,这个例子应该显示交替的绿色和黄色图案的图块)。然而,当我运行应用程序时,它只显示一块颜色,而不是应该显示的图案,而不是重复的一个图块,只是一块颜色类似于其中一个图块的背景颜色。即使试图用一种瓷砖填充地图也不会在瓷砖上显示图案,只是一块坚固的颜色。

我的场景中设置SKScene didMove(to view: SKView)地图的代码如下:

let tileset = SKTileSet(tileGroups: [
        SKTileGroup(tileDefinition: SKTileDefinition(texture: SKTexture(image: #imageLiteral(resourceName: "Sand3")))),
        SKTileGroup(tileDefinition: SKTileDefinition(texture: SKTexture(image: #imageLiteral(resourceName: "Grass1"))))
])

let tilemapNode = SKTileMapNode(tileSet: tileset, columns: 20, rows: 20, tileSize: CGSize(width: 32.0, height: 32.0))

var x = 0
var y = 0
var sand = false

while x < 20 {
    while y < 20 {
        var tilegroup: SKTileGroup
        if sand {
            tilegroup = tileset.tileGroups[0]
        } else {
            tilegroup = tileset.tileGroups[1]
        }
        tilemapNode.setTileGroup(tilegroup, forColumn: x, row: y)
        y = y + 1
        sand = !sand
    }
    x = x + 1
    y = 0
    sand = !sand
}

self.addChild(tilemapNode)

我的视图控制器:

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view = SKView(frame: UIScreen.main.bounds)

        if let view = self.view as! SKView? {
            let scene = GameScene()
            scene.scaleMode = .aspectFill
            view.presentScene(scene)
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

App代表:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)

        let testGameController = GameViewController()

        window?.rootViewController = testGameController
        window?.makeKeyAndVisible()
        return true
    }
//...
}

我找不到任何类似于使用内置SKTileMapNodes的问题。

0 个答案:

没有答案