简体   繁体   English

带颜色的SKSpriteNode不显示

[英]SKSpriteNode with color doesn't show

I created a SKSPriteNode without texture but color 我创建了一个没有纹理但有颜色的SKSPriteNode

let tileNode = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 80.0, height: 120.0))
tileNode.position = CGPointMake(50, 50)
tileNode.name = "rectangle"
addChild(tileNode)

But the node doesn't display on the screen. 但是该节点未显示在屏幕上。

However, I can detect it with touch collision 但是,我可以通过触摸碰撞检测到它

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    let location: CGPoint = (touches.anyObject() as UITouch).locationInNode(tilesLayer)
    let rect: SKSpriteNode = tilesLayer.childNodeWithName("rectangle") as SKSpriteNode
    if rect.containsPoint(location) {
        println("TOUCHED")    //It works
    }
}

EDIT : The SKSpriteNode color is only hidden if there is a background SKSpriteNode texture. 编辑 :仅当有背景SKSpriteNode纹理时,才隐藏SKSpriteNode颜色。 Complete code : https://gist.github.com/BabyAzerty/9dca752d9faa7b768bf0 完整代码: https : //gist.github.com/BabyAzerty/9dca752d9faa7b768bf0

I believe you created your project using the Game project template in Xcode, correct? 我相信您使用Xcode中的Game项目模板创建了项目,对吗? Your issue is this line in GameViewController.m (roughly line 41). 您的问题是GameViewController.m中的这一行(大约41行)。

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;

If you set that to NO, then it will render as you expect. 如果将其设置为NO,则它将按预期呈现。 I build my game projects from empty projects, so I never had that property set. 我从空项目中构建游戏项目,因此从未设置该属性。 Just search your code for ignoresSiblingOrder 只需在您的代码中搜索ignoresSiblingOrder

Of course if you want that rendering optimization, then you can always use zPosition. 当然,如果您要进行渲染优化,则可以始终使用zPosition。

I noticed that the difference between creating a SKSpriteNode with texture and a SKSpriteNode with color is that the texture version doesn't need to have its z-index modified. 我注意到创建带有纹理的SKSpriteNode和创建带有颜色的SKSpriteNode之间的区别是纹理版本不需要修改其z-index。 It will display over the previous addChild(someNodes) automatically. 它将自动显示在先前的addChild(someNodes)上。

They both have a default z-index of 0 but for some reasons SKSpriteNode with color is hidden behind your previous addChild(someNodes). 它们的默认z-index均为0,但由于某些原因,带有颜色的SKSpriteNode隐藏在您先前的addChild(someNodes)后面。

This line should do the trick : 这行应该可以解决问题:

tileNode.zPosition = 1 //Or higher

However, I decided to create an enum that controls the different z-layers (just like the one in Unity Engine) : 但是,我决定创建一个枚举来控制不同的z层(就像Unity Engine中的那个一样):

enum ZLayer: CGFloat {
    case Undefined = 0
    case Background
    case Foreground
    case GUI
}

And use it like this : 并像这样使用它:

tileNode.zPosition = ZLayer.Background.rawValue

The advantage of this enum is that if you need to add a new layer between background and foreground layers, you don't need to bother with the values at all, just an entry in the enum between the 2 cases. 该枚举的优点是,如果需要在背景层和前景层之间添加新层,则根本不需要理会值,只需在两种情况下的枚举中输入一个即可。

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

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