1

我已经工作了好几天了。我在关卡编辑器中创建了一个平铺地图。它可以很好地加载到我的代码中,但是当我遍历图块时,它们都没有显示为具有定义。不知道我做错了什么。

一切运行良好,但它不会加载磁贴定义。

SKTileNode

瓦片集 等级

'import SpriteKit

protocol EventListenerNode {
    func didMoveToScene()
}

typealias TileCoordinates = (column: Int, row: Int)

class GameScene: SKScene {

    var car = CarNode()
    var holdingAcceleration = false
    var mainCamera = SKCameraNode()
    var hub = SKNode()
    var levelHolder: SKNode!

    override func didMove(to view: SKView) {
        levelHolder = childNode(withName: "levelHolder")

        struct PhysicsCategory {
            static let None:  UInt32 = 0
            static let CarBody:   UInt32 = 0b1 // 0001 or 1
            static let Ground: UInt32 = 0b10 // 0010 or 2
            static let Tires:   UInt32 = 0b100 // 0100 or 4
        }

        // This code sends a message to all nodes added to scene that conform to the EventListenerNode protocol
        enumerateChildNodes(withName: "//*", using: { node, _ in
            if let eventListenerNode = node as? EventListenerNode {
                eventListenerNode.didMoveToScene()
                //print("calling to all nodes.  didMoveToScene")
            }
        })

        car = childNode(withName: "//Car") as! CarNode
        mainCamera = childNode(withName: "//Camera") as! SKCameraNode
        camera = mainCamera

//        /* Load Level 1 */
        let resourcePath = Bundle.main.path(forResource: "TestLevel", ofType: "sks")
        let level = SKReferenceNode (url: URL (fileURLWithPath: resourcePath!))
        levelHolder.addChild(level)
        let levelTileNode = childNode(withName: "//levelTileNode") as! SKTileMapNode
        var splinePoints = createGroundWith(tileNode:levelTileNode)

        let ground = SKShapeNode(splinePoints: &splinePoints,
                                 count: splinePoints.count)
        ground.lineWidth = 5
        ground.physicsBody = SKPhysicsBody(edgeChainFrom: ground.path!)
        ground.physicsBody?.restitution = 0.75
        ground.physicsBody?.isDynamic = false

        // Add the two nodes to the scene
        scene?.addChild(ground)

        ////////////////////////////Test///////////////////

    }

    override func update(_ currentTime: TimeInterval) {
        if holdingAcceleration{
            car.accelerate()
        }
        let carPosition = car.scene?.convert(car.position, from: car.parent!)

        mainCamera.position = carPosition!

    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let location = touch.location(in: self)
            let touchNode = atPoint(location)

            if touchNode.name == "Gas"{
                holdingAcceleration = true
            }
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let location = touch.location(in: self)
            let touchNode = atPoint(location)

            if touchNode.name == "Gas"{
                holdingAcceleration = false
            }
        }
    }

    /*
    func createSplineFrom(tileNode: SKTileMapNode)->[CGPoint]{
        print("entered the createSpline function")
        var arrayOfPoints = [CGPoint]()
        let tileMap = tileNode

        let tileSize = tileMap.tileSize
        let halfWidth = CGFloat(tileMap.numberOfColumns) / 2.0 * tileSize.width
        let halfHeight = CGFloat(tileMap.numberOfRows) / 2.0 * tileSize.height

        for col in 0..<tileMap.numberOfColumns {
            print("in column \(col) of \(tileMap.numberOfColumns)")

            for row in 0..<tileMap.numberOfRows {
                //print("col: \(col) row: \(row)")

                if let tileDefinition = tileMap.tileDefinition(atColumn: col, row: row)
                {
                    print("tileDefinition is found.  Holy cow")

                    let isEdgeTile = tileDefinition.userData?["groundFriction"] as? Int  //uncomment this if needed, see article notes
                    if (isEdgeTile != 0) {
                        let tileArray = tileDefinition.textures
                        //let tileTexture = tileArray[0]
                        let x = CGFloat(col) * tileSize.width - halfWidth + (tileSize.width/2)
                        let y = CGFloat(row) * tileSize.height - halfHeight + (tileSize.height/2)
                        _ = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
                        arrayOfPoints.append(CGPoint(x: x, y: y))

                        //let tileNode = SKNode()

                        //tileNode.position = CGPoint(x: x, y: y)

                    }
                }

            }
        }

        print(arrayOfPoints.count)

        return arrayOfPoints
    }
    */

    func tile(in tileMap: SKTileMapNode, at coordinates: TileCoordinates) -> SKTileDefinition? {
            return tileMap.tileDefinition(atColumn: coordinates.column,    row: coordinates.row)
    }

    func createGroundWith(tileNode:SKTileMapNode) -> [CGPoint] {
        var arrayOfPoints = [CGPoint]()
        print("inside createGround")
        let groundMap = tileNode

        let tileSize = groundMap.tileSize
        let halfWidth = CGFloat(groundMap.numberOfColumns) / 2.0 * tileSize.width
        let halfHeight = CGFloat(groundMap.numberOfRows) / 2.0 * tileSize.height

        for row in 0..<groundMap.numberOfRows {
            for column in 0..<groundMap.numberOfColumns {
                // 2
                guard let tileDefinition = tile(in: groundMap, at: (column, row))
                    else { continue }
                print("inside tileDefinitioin")
                let isEdgeTile = tileDefinition.userData?["groundFriction"] as? Int
                if (isEdgeTile != 0) {
                    let tileArray = tileDefinition.textures
                    //let tileTexture = tileArray[0]
                    let x = CGFloat(column) * tileSize.width - halfWidth + (tileSize.width/2)
                    let y = CGFloat(row) * tileSize.height - halfHeight + (tileSize.height/2)
                    _ = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
                    arrayOfPoints.append(CGPoint(x: x, y: y))
                }
        // 4
        //bugsNode.name = "Bugs"
        //addChild(bugsNode)
        // 5
        //bugsMap.removeFromParent()
            }

        }
        return arrayOfPoints
    }
}

`

4

1 回答 1

2

所以我想通了。瓦片集有一个“按需”资源标签。由于我没有意识到这一点,游戏并没有从游戏场景中自动加载图块。总是简单的东西。

于 2017-10-05T09:39:36.570 回答