Serious SpriteKit UIColor bug on iPhone 5S

旧巷老猫 提交于 2019-12-11 22:13:57

问题


I've found a serious swift-bug in SpriteKit while working with SKSpriteNodes and their colors.

This code works fine on all iPhones beside the iPhone 5S:

var color1 = UIColor(red: 123/255, green: 123/255, blue: 123/255, alpha: 1)
var color2 = UIColor(red: 123/255, green: 123/255, blue: 123/255, alpha: 1)

var sprite = SKSpriteNode(color: color1, size: CGSizeMake(100, 100))

if(sprite.color == color2){
     println("Same color")
}

As you see, the two colors are the absolut same. But on the iPhone 5S simulator, the if isn't called.

Has somebody else the same problem and can provide a solution?


回答1:


According to the documentation here:

Sprite Kit works only with solid colors. For best results, use the preset colors provided by the platform class or a custom color defined in the RGBA device color space.

As a result somehow the SKSpriteNode has made some changes to the color parameter in the init function. You can see it if you call encode:

sprite.color.encode() // 140,646,370,382,768

color1.encode() // 140,646,367,110,928

If you use predefined color values, then your problem goes away:

var color3 = UIColor.blueColor()
var sprite3 = SKSpriteNode(color: color3, size: CGSizeMake(100, 100))

sprite3.color == color3 // true



回答2:


You are comparing pointer values, not the actual color. Seeing that these are UIColor instances, you have to compare them using isEqual (showing ObjC code as I don't know what it looks like in Swift - or perhaps Swift is in fact using isEqual behind the scenes):

if ([sprite.color isEqual:color2])

If implemented correctly by UIColor this will compare the actual color values rather than the pointers.



来源:https://stackoverflow.com/questions/25852102/serious-spritekit-uicolor-bug-on-iphone-5s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!