lws*_*803 5 sprite-kit skspritenode swift3
有没有一种更有效的方法来在一个 sklabelnode 中通过打字来为颤抖的文本制作动画?我正在尝试在一些游戏中实现这种效果,比如传说中的文字,当它们同时颤抖时,它们会出现打字机的风格。
到目前为止,我只能实现它,但幸运的是:
class TextEffectScene: SKScene {
var typeWriterLabel : SKLabelNode?
var shiveringText_L : SKLabelNode?
var shiveringText_O : SKLabelNode?
var shiveringText_S : SKLabelNode?
var shiveringText_E : SKLabelNode?
var shiveringText_R : SKLabelNode?
var button : SKSpriteNode?
override func sceneDidLoad() {
button = self.childNode(withName: "//button") as? SKSpriteNode
self.scaleMode = .aspectFill //Very important for ensuring that the screen sizes do not change after transitioning to other scenes
typeWriterLabel = self.childNode(withName: "//typeWriterLabel") as? SKLabelNode
shiveringText_L = self.childNode(withName: "//L") as? SKLabelNode
shiveringText_O = self.childNode(withName: "//O") as? SKLabelNode
shiveringText_S = self.childNode(withName: "//S") as? SKLabelNode
shiveringText_E = self.childNode(withName: "//E") as? SKLabelNode
shiveringText_R = self.childNode(withName: "//R") as? SKLabelNode
}
// Type writer style animation
override func didMove(to view: SKView) {
fireTyping()
shiveringText_L?.run(SKAction.repeatForever(SKAction.init(named: "shivering")!))
shiveringText_O?.run(SKAction.repeatForever(SKAction.init(named: "shivering2")!))
shiveringText_S?.run(SKAction.repeatForever(SKAction.init(named: "shivering3")!))
shiveringText_E?.run(SKAction.repeatForever(SKAction.init(named: "shivering4")!))
shiveringText_R?.run(SKAction.repeatForever(SKAction.init(named: "shivering5")!))
}
let myText = Array("You just lost the game :)".characters)
var myCounter = 0
var timer:Timer?
func fireTyping(){
typeWriterLabel?.text = ""
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(TextEffectScene.typeLetter), userInfo: nil, repeats: true)
}
func typeLetter(){
if myCounter < myText.count {
typeWriterLabel?.text = (typeWriterLabel?.text!)! + String(myText[myCounter])
//let randomInterval = Double((arc4random_uniform(8)+1))/20 Random typing speed
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(TextEffectScene.typeLetter), userInfo: nil, repeats: false)
} else {
timer?.invalidate() // stop the timer
}
myCounter += 1
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self) {
if (button?.contains(location))! {
print("doggoSceneLoaded")
let transition = SKTransition.fade(withDuration: 0.5)
let newScene = SKScene(fileNamed: "GameScene") as! GameScene
self.view?.presentScene(newScene, transition: transition)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我必须用“失败者”一词为每个单独的标签节点设置动画。
要创建此效果:
我有很多经验......除了你已经在做的事情之外,没有办法正确地做到这一点。我的解决方案(对于文本游戏)是使用 NSAttributedString 和 CoreAnimation 一起使用,它可以让你在 UILabels 上拥有疯狂的好动画......然后将 UILabels 添加到 SpriteKit 之上。
我正在研究一个更好的 SKLabel 子类,但最终放弃了它,因为我意识到如果不做更多的工作就无法获得正确的字距调整。
可以使用 SKSpriteNode 并将视图作为纹理,然后您只需每帧更新纹理,但这需要更多的时间/资源。
执行此操作的最佳方法是在 SK 编辑器中按照您的操作方式进行操作。如果您需要大量动画文本,那么您需要将UIKitNSAttributedString 与 CoreAnimation 一起使用来实现奇特的效果。
在我看来,这是一个巨大的监督,也是 SpriteKit 的一个相当大的缺点。SKLabelNode 很糟糕。