简体   繁体   中英

Spritekit SKTexture(rect: CGRect, in: SKTexture) issue

I have a question which I am breaking my brains over for the last hours. I am creating a tiled map, and as textures for the tiles I have some spritesheets.

I can demonstrate my issue with the following spritesheet: 精灵表

Now I use the SKtexture(rect: , in:) method to get the tile textures out of the spritesheet. My problem is that when I use this method, somehow there seems to exist a very small offset, so that when I for example want to render a map filled with the lowest left sprite (just snow) I can see a very small part of the tile above it.

I can demonstrate this with the following screenshot:

示例渲染

As you can see, there are some blue lines between the tile rows. This is the code I used for the example, it is an extract of the tilemap parser I use but it shows the same behaviour.

let source = SKTexture(imageNamed: "snowiceCor")
let superNode = SKNode()
let sourceWidth = source.size().width
let sourceHeight = source.size().height
let tileSize: CGFloat = 32.0
let tileX: CGFloat = 0
let tileY: CGFloat = 8
for i in 0...8 {
   for j in 0...8 {
        let rX = (tileSize * tileX) / sourceWidth
        let rY = CGFloat(1) - ((tileSize * tileY) / sourceHeight)
        let rW = tileSize / sourceWidth
        let rH = tileSize / sourceHeight
        let rect = CGRect(x: rX, y: rY, width: rW, height: rH)
        let texture = SKTexture(rect: rect, in: source)
        let node = SKSpriteNode(texture: texture, color: .clear, size: CGSize(width: tileSize, height: tileSize))
        superNode.addChild(node)
        node.zPosition = 99
        node.position.x = CGFloat(i*32)
        node.position.y = CGFloat(j*32)
    }
}
addChild(superNode)
superNode.setScale(2)

I hope someone can help me with this.

The blue line is probably anti-aliasing kicking in. You can turn this off by setting 'filteringMode = .nearest' on the textures. Alternatively (if you want the anti-aliasing), you could pad the tiles with transparent pixels).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM