I'm making a scene in which the lyrics to a song which is played in the background of a scene are displayed line by line at the bottom of a spriteKit scene. I've tried a number of different solutions, but for some reason with each of them some of the lyrics are not being displayed or are out of sync. Im attempting to use an SKLabelNode and using SKActions change the text of it at specific times. There must be an easier way of doing this!?
here's the function I came up with:
import SpriteKit
let lyrics = SKLabelNode(fontNamed: "avenirnext")
func lyricSectionCreate(lnOneTxt: String, lnOneTime: Double, lnTwoTxt: String, lnTwoTime: Double, lnThreeTxt: String, lnThreeTime: Double, lnFourTxt: String, lnFourTime: Double, lnFiveTxt: String, lnFiveTime: Double, lnSixTxt: String, lnSixTime: Double, lnSevenTxt: String, lnSevenTime: Double, lnEightTxt: String, lnEightTime: Double, lnNineTxt: String, lnNineTime: Double, lnTenTxt: String, lnTenTime: Double){
lyrics.position = CGPoint(x:250, y: 25)
lyrics.zPosition = 5
//line one
let lnOne = SKAction.runBlock { () -> Void in
lyrics.text = lnOneTxt
}
runAfterDelay(lnOneTime){
lyrics.runAction(lnOne)
}
//line two
let lnTwo = SKAction.runBlock { () -> Void in
lyrics.text = lnTwoTxt
}
runAfterDelay(lnTwoTime){
lyrics.runAction(lnTwo)
}
//line three
let lnThree = SKAction.runBlock { () -> Void in
lyrics.text = lnThreeTxt
}
runAfterDelay(lnThreeTime){
lyrics.runAction(lnThree)
}
//line four
let lnFour = SKAction.runBlock { () -> Void in
lyrics.text = lnFourTxt
}
runAfterDelay(lnFourTime){
lyrics.runAction(lnFour)
}
//line five
let lnFive = SKAction.runBlock { () -> Void in
lyrics.text = lnFiveTxt
}
runAfterDelay(lnFiveTime){
lyrics.runAction(lnFive)
}
//line six
let lnSix = SKAction.runBlock { () -> Void in
lyrics.text = lnSixTxt
}
runAfterDelay(lnSixTime){
lyrics.runAction(lnSix)
}
//line seven
let lnSeven = SKAction.runBlock { () -> Void in
lyrics.text = lnSevenTxt
}
runAfterDelay(lnSevenTime){
lyrics.runAction(lnSeven)
}
//line eight
let lnEight = SKAction.runBlock { () -> Void in
lyrics.text = lnEightTxt
}
runAfterDelay(lnEightTime){
lyrics.runAction(lnEight)
}
//line nine
let lnNine = SKAction.runBlock { () -> Void in
lyrics.text = lnNineTxt
}
runAfterDelay(lnNineTime){
lyrics.runAction(lnNine)
}
//line ten
let lnTen = SKAction.runBlock { () -> Void in
lyrics.text = lnTenTxt
}
runAfterDelay(lnTenTime){
lyrics.runAction(lnTen)
}
}
func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), block)
}