I'm trying to put a countDown on my application. I'm using a SKLabelNode. This is my code :
class PlayScene: SKScene {
var startCount = true
var setTime = 0
var myTime = 0
var printTime = SKLabelNode(fontNamed: "arial")
override func didMoveToView(view: SKView!) {
self.backgroundColor = UIColor(hex: 0xD64541)
self.printTime.text = NSString(format: "%i", self.myTime)
self.addChild(self.printTime)
}
override func update(currentTime: NSTimeInterval) {
if self.startCount == true {
self.setTime = Int(currentTime)
self.startCount = false
}
self.myTime = Int(currentTime) - self.setTime
}
}
I had no problem to compile it but it's crashing at the execution...
I think that's coming from my convertion in string..
Thank you
float timeforFunction;
int numberoftime;
int __totalscore;
timeforFunction=1.5f;
numberoftime=10;
SKAction *updateTime= [SKAction sequence:@[
[SKAction waitForDuration: timeforFunction],
[SKAction performSelector:@selector(updatescore)
onTarget:self]
]];
[self runAction:[SKAction repeatAction:updateTime count: numberoftime] withKey:@"zorbTime"];
-(void) updatescore
{
__totalscore+=1;
_GamescoreText.text = [NSString stringWithFormat:@“%i”, __totalscore];
}
if you wanna repeat this action forever just change to
[self runAction:[SKAction repeatAction:updateTime count: numberoftime] withKey:@"zorbTime"];
I hope this helps. you almost had it
class PlayScene: SKScene {
var startCount = true
var setTime = 0
var myTime = 0
var printTime = SKLabelNode(fontNamed: "arial")
override func didMoveToView(view: SKView) {
self.backgroundColor = UIColor.orangeColor()
//self.printTime.text = NSString(format: "%i", self.myTime)//you need the position
self.printTime.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5)
self.addChild(self.printTime)
}
override func update(currentTime: NSTimeInterval) {
if self.startCount == true {
self.setTime = Int(currentTime)
self.startCount = false
}
self.myTime = Int(currentTime) - self.setTime
self.printTime.text = ("format:\(self.myTime)")//put your label in update so it shows each second.
}
Here's how I solved this problem for my Swift/SpriteKit 'Breakout' application. I wanted a countdown from 5 to 1 onthe main game screen but before the ball started to move. I Added these functions and then a call to countdown(5) at the end of didMoveToView
: Notice the ball.physicsBody!.applyImpulse(CGVectorMake(20, 20))
as the last step of endCountdown
which starts the ball and effectively starts the game.
func countdown(count: Int) {
countdownLabel.horizontalAlignmentMode = .Center
countdownLabel.verticalAlignmentMode = .Baseline
countdownLabel.position = CGPoint(x: size.width/2, y: size.height*(1/3))
countdownLabel.fontColor = SKColor.whiteColor()
countdownLabel.fontSize = size.height / 30
countdownLabel.zPosition = 100
countdownLabel.text = "Launching ball in \(count)..."
addChild(countdownLabel)
let counterDecrement = SKAction.sequence([SKAction.waitForDuration(1.0),
SKAction.runBlock(countdownAction)])
runAction(SKAction.sequence([SKAction.repeatAction(counterDecrement, count: 5),
SKAction.runBlock(endCountdown)]))
}
func countdownAction() {
count--
countdownLabel.text = "Launching ball in \(count)..."
}
func endCountdown() {
countdownLabel.removeFromParent()
ball.physicsBody!.applyImpulse(CGVectorMake(20, 20))
}