gpt4 book ai didi

ios - SKTileMapNode 碰撞不起作用

转载 作者:行者123 更新时间:2023-11-28 06:23:15 24 4
gpt4 key购买 nike

我正在尝试使用 SKTileMapNode 和 SKTileMaps 制作一个横向卷轴器,但我在处理单个图 block 的物理体时遇到了问题。我按照此链接中的描述在循环中创建它们:

https://forums.developer.apple.com/thread/50043

但是我的播放器节点穿过地面。我做错了什么?

        guard let landBackground = childNode(withName: "ground")
as? SKTileMapNode else {
fatalError("Background node not loaded")
}
self.landBackground = landBackground
self.tileSize = landBackground.tileSize

for x in 0...(landBackground.numberOfColumns-1){
for y in 0...(landBackground.numberOfRows-1){

let tile = landBackground.tileDefinition(atColumn: x, row: y)
if(tile != nil){
let rect = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
let tileNode = SKShapeNode(rect: rect)

tileNode.position = self.landBackground.centerOfTile(atColumn: x, row: y)

tileNode.position.x -= (tileSize.width * 0.5)
tileNode.position.y -= (tileSize.height * 0.5)

tileNode.physicsBody = SKPhysicsBody(rectangleOf: tileSize, center: tileNode.position)
tileNode.physicsBody?.isDynamic = false
tileNode.physicsBody?.collisionBitMask = playerCategory|groundCategory

//tileNode.physicsBody?.contactTestBitMask = playerCategory
tileNode.physicsBody?.categoryBitMask = groundCategory
landBackground.addChild(tileNode)
}
}
}

最佳答案

SKPhysicsBody(rectangleOf: tileSize, center: tileNode.position)

这可能是不正确的。调用的文档说:

The center of the square in the owning node’s coordinate system.

这意味着您可能应该使用 .zero。物理体始终假定与其所属的节点位于同一位置,除非特别偏移。你不应该为此使用场景坐标。

还要确保在 SKView 上将 showsPhysics 设置为 true,这样您就可以看到它们的真实位置。

关于ios - SKTileMapNode 碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742054/

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