精灵套件中的tilemap

时间:2015-06-03 18:51:34

标签: swift sprite-kit tile

这是关于在SpriteKit中绘制TileMap。我收到以下错误:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'尝试添加已有父级的SKNode:name:'(null)'texture:['nil'] position:{64,0} size: {0,0}轮换:0.00'

这是我的GameViewController.swift:

import SpriteKit

class GameScene: SKScene {

let dungeon = SKNode()

let readableMap: [[Int]] = [
    [01, 00, 01, 01],
    [01, 00, 01, 01],
    [01, 00, 01, 01],
    [00, 01, 00, 01]]

override init(size: CGSize) {
    super.init(size: size)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    drawMap()
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

func drawMap() {
    let map = readableMap.reverse()
    let tiles = [[Tile]](count: map.count, repeatedValue: [Tile](count: map[0].count, repeatedValue: Tile()))

    for i in 0..<map.count {
        for j in 0..<map[0].count {
            let tile = tiles[j][i]
            tile.position = CGPoint(x: j * Int(tile.tileSize.width), y: i * Int(tile.tileSize.height))
            dungeon.addChild(tile)
        }
    }
}
}

这是我的GameScene.swift:

import SpriteKit

class Tile: SKSpriteNode {
let tileSize: CGSize = CGSize(width: 64, height: 64)
let tileTexture: SKTexture = SKTexture(imageNamed: "o")
}

这是Tile.swift中的类:

{{1}}

可能是什么原因?感谢任何人,我还在学习。

0 个答案:

没有答案