I have a problem concerning SKSpriteNode Textures that are not drawn to the screen. This problem appeared out of the blue and I took me quite a while to hunt it down to its origin, but I'm still looking for a solution.
What I'm doing is simple: I'm basically using a grid (x columns times y rows) to draw the background of a match-3 game. Each cell contains a sprite with a texture to draw the background. That means there are x times y sprites in total. The texture for the inner cells is the same for all of them (plain grey), for the outer cells I'm drawing round tiles on the corners and fitting tiles for the edges.
The thing is: Sometimes some of the cells are not being loaded correctly. I'm developing for @2x resolution only at the moment, but that should not be the problem.
My code for adding the sprites looks like this:
for ((column, row), tile) in field.tiles.enumerate(){
let sprite = SKSpriteNode(imageNamed: tile!.tileType.spriteName)
sprite.position = pointForColumn(column, row: row)
print("Sprite: \(sprite)")
tileLayer.addChild(sprite)
tile!.sprite = sprite
}
tileType is an enum describing the needed 9 different cell types:
enum TileType: Int{
case Tl = 1, Tr, B, Bl, L = 5, Br = 8, R = 10, T = 12, All = 15
var spriteName: String{
return String(format: "Tile_%ld.png", rawValue)
}
}
My pictures are in an .atlas folder.
The console output for the sprites looks like this when it worked:
Sprite: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'Tile_3@2x.png' (180 x 180)] position:{270, 30} scale:{1.00, 1.00} size:{90, 90} anchor:{0.5, 0.5} rotation:0.00, Tile: Optional(tetris_attack.Tile)
When it did not then the texture name is missing the "@2x" and the size is "0 x 0" instead of "180 x 180". Setting the size by hand only leads to a big white square. It seems like the picture simply wasn't loaded correctly because the file name was generated wrong.
Sprite: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'Tile_1.png' (0 x 0)] position:{330, 30} scale:{1.00, 1.00} size:{0, 0} anchor:{0.5, 0.5} rotation:0.00, Tile: Optional(tetris_attack.Tile)
That seems to happen almost randomly. Most of the time the corner tile on the bottom right is missing. Sometimes also some of the inner tiles don't show. Then other times some of the edge tiles are missing.
The thing is that some sprites load their texture correctly and some don't - with the exact same picture and the exact same code!
The positions of the sprites are correct. Also, changing the zPosition does not resolve the issue. It clearly is the picture that did not load properly.
When I change my code so that "spriteName" is always the same name of a picture (e.g. spriteName returns always "Tile_1.png") I never saw missing tiles, but of course thats not what I want. This behavior is absolutely unexplainable for me.
I found this related topic on the Apple developer forum, but the bugfix (adding the file extension) did not work for me. I even tried out my code in Swift 1.2 and XCode 6.3 but the issue did not change, so it's unlikely that my issue is a Swift 2 / XCode 7.1.1 problem.
Is there somebody who had the same problem and who can help me out here? I'm still hoping that I'm just missing something big here.