Lui*_*uiz 3 ios sprite-kit swift
我想知道除了 SKLabelNode 之外是否还有其他方法可以设置一个图标(因为我需要用来SKAction向上移动这个标签),如下所示:
我所发现的只是使用 UILabel(此处)或 GitHub 项目(此处),在那里我无法移动或弹跳(使用SpriteKit-Spring)我的标签。
我正在考虑创建一个带有图标图像的精灵节点并将其设置为coinsLabel,但由于此标签用作硬币计数器,因此增加时它会变大;并且图标将被覆盖。
我在下面制作了这个示例项目,以使其更易于可视化(当然,它没有图标。它只是coinsLabel通过按钮递增和移动)。
如果你愿意,你可以在这里下载。
import SpriteKit
class GameScene: SKScene {
//Declaration
var icon = SKSpriteNode()
var coins = Int()
var coinsLabel = SKLabelNode()
var incrementButton = SKSpriteNode()
//Setup
func setupIcon(){
//icon
icon = SKSpriteNode(imageNamed: "icon")
icon.position = CGPoint(x: self.frame.width / 1.45, y: self.frame.height / 1.075)
icon.setScale(0.1)
}
func setupCoinsLabel(){
//coinsLabel
coinsLabel.position = CGPoint(x: self.frame.width / 150 - 300, y: 0)
coinsLabel.setScale(12.5)
coinsLabel.text = "0"
}
func setupIncrementButton(){
//incrementButton
incrementButton = SKSpriteNode(imageNamed: "incrementButton")
incrementButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 3.15)
incrementButton.setScale(2.0)
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
setupIcon()
addChild(icon)
setupCoinsLabel()
icon.addChild(coinsLabel)
setupIncrementButton()
addChild(incrementButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
//When touch buttons/screen
for touch in touches{
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
//Increment
if node == incrementButton{
coins += 1
coinsLabel.text = NSString(format: "%i", coins) as String
coinsLabel.position = CGPoint(x: self.frame.width / 150 - coinsLabel.frame.width, y: 0)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需创建一个 SKSpriteNode 并将其作为子项添加到 SKLabelNode,您始终可以将 SKSpriteNode 的位置设置为 SKLabel 的右侧,无论您的标签中有多少位数字,因此永远不会发生重叠
//Increment
if node == incrementButton{
coins += 1
coinsLabel.text = NSString(format: "%i", coins) as String
icon.position = CGPoint(x: coinsLabel.frame.width / 2, y: 0)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
603 次 |
| 最近记录: |