gpt4 book ai didi

swift - SpriteKit 瓦片 map 不加载瓦片

转载 作者:行者123 更新时间:2023-11-28 15:18:52 26 4
gpt4 key购买 nike

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

一切运行正常,但它不会加载 tile 定义。

SKTileNode

Tileset Level

'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
}
}

`

最佳答案

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

关于swift - SpriteKit 瓦片 map 不加载瓦片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46362257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com