SpriteKit 中自定义绘图的替代方案是什么?

What are the alternatives for custom drawing in SpriteKit?

我想制作这样的精灵节点:

|---------|
|some text|
|_________|

这只是框架中包含的一些文本。

UIKit 中,我只是简单地创建一个自定义视图并覆盖 drawRect。我认为这也可以用精灵或节点来完成。但令人失望的消息是,我做不到。文档明确表示您不能进行自定义绘图。

我的意思是上面就是这样一个简单的对象。我需要为此创建 两个 精灵(SKSpriteNode 用于框架,SKLabelNode 用于文本)?对于这么简单的事情,这太烦人了!

此外,如果我想让用户点击按钮时颜色变暗怎么办?如果自定义绘图是可能的,我可以在 touchesBegan 时在顶部绘制一个带有一些 alpha 的灰色层。但是现在我需要 2 种不同的纹理(正常的一种和较暗的一种)来做到这一点!

是不是太难了?我相信一定有另一种更简单的方法。你能告诉我怎么做吗?

您可以像这样设置按钮节点:

let buttonText = SKLabelNode(text: "Button")
let buttonBG = SKSpriteNode(imageNamed: "buttonTexture")
let button = SKNode()
button.addChild(buttonBG)
button.addChild(buttonText)

如果您想对按钮应用 35% 的黑色覆盖层以使其变暗,您可以对 touchesBegan 中的按钮纹理执行类似的操作:

buttonBG.runAction(SKAction.colorizeWithColor(UIColor.blackColor(), colorBlendFactor: 0.35, duration: 0.18))

touchesEnded中:

buttonBG.runAction(SKAction.colorizeWithColor(UIColor.blackColor(), colorBlendFactor: 0.0, duration: 0.18))

请注意,在这种情况下,您实际上不能将此操作应用于父节点,因为它不是精灵节点。