【发布时间】:2016-09-03 13:25:34
【问题描述】:
我想知道除了 SKLabelNode 之外是否有任何方法可以设置图标(因为我需要使用 SKAction 来上移这个标签),如下所示:
我所发现的只是使用 UILabel (here) 或 GitHub 项目 (here),我无法移动或弹跳 (SpriteKit-Spring) 我的标签。
我想用图标图像创建一个精灵节点并将其位置设置在coinsLabel之外,但由于此标签用作硬币计数器,增加时会变大;并且图标将被覆盖。
我在下面制作了这个示例项目,以使其更易于可视化(当然,它没有图标。它只是通过按钮递增和移动coinsLabel)。
如果需要,可以here下载。
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)
}
}
}
}
【问题讨论】:
标签: ios swift sprite-kit