我正在尝试使用SKTileMapNode和SKTileMaps制作一个侧滑控制器,但我在处理单个磁贴的物理体时遇到了问题。我在一个循环中创建它们,如下面的链接所述:
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)
}
}
}发布于 2017-03-12 20:27:04
SKPhysicsBody(rectangleOf: tileSize, center: tileNode.position)
这可能是不正确的。调用的文档是这样写的:
拥有节点坐标系中正方形的中心。
这意味着您可能应该使用.zero。物理实体总是被假定与它所属的节点处于相同的位置,除非特别偏移。您不应该为此使用场景坐标。
此外,请确保在SKView上将showsPhysics设置为true,这样您就可以看到它们的实际位置。
https://stackoverflow.com/questions/42742054
复制相似问题