我正在做一个游戏,我已经设置了一个网格模式.现在你可以在屏幕截图中看到,小方块是发生碰撞的物理体,由于某种原因,这些方块与图像不对齐.

enter image description here

相关代码如下:

    override func didMove(to view: SKView) {
        ....
        let rockTexture = SKTexture(imageNamed: "pathrocks")
        let gapTexture = SKTexture(imageNamed: "claybricks")
        
        let tileSet = SKTileSet(tileGroups: [])
        let tileSize = CGSize(width: 32, height: 32)
        
        let rockTileGroup = SKTileGroup(rules: [])
        let rockTextureRule = SKTileGroupRule(adjacency: .adjacencyAll, tileDefinitions: [SKTileDefinition(texture: rockTexture)])
        rockTileGroup.rules.append(rockTextureRule)
        tileSet.tileGroups.append(rockTileGroup)
        
        let gapTileGroup = SKTileGroup(rules: [])
        let gapTextureRule = SKTileGroupRule(adjacency: .adjacencyAll, tileDefinitions: [SKTileDefinition(texture: gapTexture)])
        gapTileGroup.rules.append(gapTextureRule)
        tileSet.tileGroups.append(gapTileGroup)
        
        let columns = 20
        let rows = 10 // Increase the number of rows
        let tileMapNode = CustomTileMapNode(tileSet: tileSet, columns: columns, rows: rows, tileSize: tileSize)

        for x in 0..<columns {
            for y in 0..<rows {
                // Determine if it's a gap tile or a rock tile
                let isGapTile = (x == 4 || x == 5 || x == 6)
                let tileGroup = isGapTile ? gapTileGroup : rockTileGroup

                // Set the appropriate tile group for the tile
                tileMapNode.setTileGroup(tileGroup, forColumn: x, row: y)

                // Store the coordinates of gap tiles in the custom tile map node
                if isGapTile {
                    tileMapNode.gapTileCoords.append(TileCoord(column: x, row: y))
                }

                if !isGapTile {
                    let tileRect = CGRect(x: CGFloat(x) * tileSize.width,
                                          y: CGFloat(y) * tileSize.height,
                                          width: tileSize.width,
                                          height: tileSize.height)
                    
                    let tilePhysicsBody = SKPhysicsBody(edgeLoopFrom: tileRect)
                    tilePhysicsBody.categoryBitMask = 4 // You can set it to any appropriate bitmask value
                    tilePhysicsBody.contactTestBitMask = 1 // The car's categoryBitMask
                    let physicsBodyNode = SKNode()
                    physicsBodyNode.physicsBody = tilePhysicsBody
                    tileMapNode.addChild(physicsBodyNode)
                }
            }
        }

        tileMapNode.position = CGPoint(x: 0, y: 0)
        addChild(tileMapNode)

推荐答案

我通过添加锚点修复了这个问题,我在初始化timeMapNode之后立即添加了tileMapNode.anchorPoint = CGPoint(x: 0, y: 0)

Swift相关问答推荐

多个提取来计算核心数据中的记录

操作员如何点逻辑非(.!)在Swift工作?

启用完整并发判断以及如何解决警告

如何返回JSON数据?

如何在HStack中均匀分布视图?

为什么第二项任务不在第一项任务完成之前开始?(并发队列)

如何防止自动状态恢复,如果应用程序被启动打开特定的文档?

使用View()和List()无法显示影子

为什么 UITapGestureRecognizer 对于 Swift 集合视图中的单元格图像无法正常工作?

为 SwiftUI 中的属性提供默认值

引用类型(类)不需要但 struct 需要的可识别 id

SwiftUI:从存储在 observableObject 中的数组中删除对象时查看崩溃

AVPlayer 在 iOS 15.4 中寻求 completionHandler 返回 false

如果没有标记,则为 Swift 预处理器

Swift 覆盖实例变量

如何快速截取 UIView 的屏幕截图?

CMutablePointer - 如何访问它?

快速在 LLDB 中使用 po

如何发送静默推送通知有效负载

UITableView 布局在 push segue 和 return 上搞砸了. (iOS 8、Xcode beta 5、Swift)