简体   繁体   English

Swift 2.0-SK​​SpriteNode的SKTexture有时不显示

[英]Swift 2.0 - SKSpriteNode's SKTexture sometimes does not show

I have a problem concerning SKSpriteNode Textures that are not drawn to the screen. 我有一个关于SKSpriteNode纹理的问题,这些纹理没有绘制到屏幕上。 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. 我正在做的事情很简单:我基本上是使用网格(x列乘以y行)来绘制第3场比赛的背景。 Each cell contains a sprite with a texture to draw the background. 每个单元格包含一个带有纹理的精灵,用于绘制背景。 That means there are x times y sprites in total. 这意味着总共有x个y个精灵。 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. 我目前仅针对@ 2x分辨率进行开发,但这不应该是问题。

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: tileType是一个枚举,描述了所需的9种不同的单元格类型:

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. 我的图片位于.atlas文件夹中。

The console output for the sprites looks like this when it worked: Sprite的控制台输出在工作时如下所示:

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". 如果没有,则纹理名称缺少“ @ 2x”,并且大小为“ 0 x 0”,而不是“ 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. 另外,更改zPosition不能解决问题。 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 (eg spriteName returns always "Tile_1.png") I never saw missing tiles, but of course thats not what I want. 当我更改代码以使“ spriteName”始终与图片相同(例如,spriteName始终返回“ Tile_1.png”)时,我从来没有看到丢失的图块,但是那当然不是我想要的。 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. 我在Apple开发人员论坛上找到了这个相关主题 ,但是错误修正(添加文件扩展名)对我不起作用。 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. 我什至在Swift 1.2和XCode 6.3中试用了我的代码,但问题没有改变,因此我的问题不太可能是Swift 2 / XCode 7.1.1问题。

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. 我仍然希望我在这里只是想念一些大事。

I looked into this further and you need to change the z position of your node because it may be blocked behind other nodes. 我对此进行了进一步研究,您需要更改节点的z位置,因为它可能会在其他节点后面被阻止。

sprite.zposition = 1.0;

this should resolve the issue. 这应该可以解决问题。 You can also set the opacity of all your nodes to 0.5 to see if anything is blocking a node. 您还可以将所有节点的不透明度设置为0.5,以查看是否有任何东西阻塞了节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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