SpriteKit 瓷砖地图不加载瓷砖
Posted
技术标签:
【中文标题】SpriteKit 瓷砖地图不加载瓷砖【英文标题】:SpriteKit tile map not loading tiles 【发布时间】:2017-09-22 10:13:41 【问题描述】:我已经工作了好几天了。我在关卡编辑器中创建了一个平铺地图。它可以很好地加载到我的代码中,但是当我遍历图块时,它们都没有显示为具有定义。不知道我做错了什么。
一切运行良好,但不会加载磁贴定义。
'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
`
【问题讨论】:
感谢您的编辑,但为什么要投反对票?我研究了几天为什么 tileMap.definition 没有返回一些东西。我试过直接在游戏场景中加载场景,结果没有变化。我添加了额外的瓷砖来覆盖每个方格,看看某个瓷砖是否有问题。我只是不知道该去哪里回答这个问题。我已经查看了 Apple 的所有关于此的文档,但一无所获。 不知道为什么有人投了反对票,我只是给了你更多的标签来帮助你的结果 【参考方案1】:所以我想通了。瓦片集有一个“按需”资源标签。由于我没有意识到这一点,游戏并没有从游戏场景中自动加载图块。总是很简单。
【讨论】:
以上是关于SpriteKit 瓷砖地图不加载瓷砖的主要内容,如果未能解决你的问题,请参考以下文章