我想制作一个像这样的 Sprite 节点:
|---------|
|some text|
|_________|
这只是框架中包含的一些文本。
在 UIKit
中,我只是简单地创建一个自定义 View 并覆盖 drawRect
。我认为这也可以用 Sprite 或节点来完成。但令人失望的消息是,我做不到。文档明确表示您不能进行自定义绘图。
我的意思是上面是一个如此简单的对象。我需要为此创建两个 Sprite (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))
请注意,在这种情况下,您实际上不能将此操作应用于父节点,因为它不是 Sprite 节点。
关于ios - SpriteKit 中自定义绘图的替代方案有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569206/