57552

countdown in swift and spritekit

Question:

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

Answer1:

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"];

Answer2:

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. }

Answer3:

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 endCountdownwhich 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)) }

Recommend

  • Created PDF does not reflect changes made to the original document
  • xlsx database ios
  • SQL Server varchar(50) and varchar(128) performance difference [duplicate]
  • Dates in .net vs in javascript
  • Entity Framework Adding Existing Item Causes Clone
  • RedirectToAction after successful post to show a message
  • How to convert a DataGridViewCell to a Control
  • How can I allow nulls to be accepted by my WebAPI model?
  • Suppress ORA-00942 errors in ddl create scripts
  • Java.net.SocketTimeoutException in Ksoap2
  • Resize of Iframe dynamically depending on the content of the Iframe of the Iframe's src URL
  • c3p0.SQLWarnings Server user id 29 is not a valid user in database 'defaultdb'
  • AngularJS: Returning a promise in directive template function
  • infinite loop slider using keyframes css3
  • Cannot visualize images inside Xcode
  • Gap between search menu and its previous menu on addition of search plugin
  • Getting webGL error in autodesk viewer
  • apache camel how to get header attributes from sql-component
  • Modify chart label orientation with XlsxWriter module (Python)
  • Java 8/Javascript (Nashorn) long interoperatiblity
  • Close Colorbox iframe after form submit and then redirect parent page
  • XMPPConnection Error in android
  • GetFinalPathNameByHandle() without prepended '\\\\?\\'
  • How would I send and receive packets over a WebSocket in Javascript
  • CSS custom shape button with two colors
  • removing the default blue color on focus
  • I don't get what's the difference between format() and … (python)
  • How to define an array of floats in Shader properties?
  • Scala: Function returning an unknown type
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal