matsmats matsmats - 1 year ago 156
iOS Question

How to set up game loop in Swift

To set up a game loop in Objective-C I learnt that I should set up a CADisplayLink

updater = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop) ];
[updater setFrameInterval: 1];
[updater addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes];

How do I do this in Swift?

I have tried to Google this but I cannot find any examples of this.

Answer Source

Pretty much just a direct translation from Objective-C to Swift with a few tweaks.

import QuartzCore
var updater = CADisplayLink(target: self, selector: Selector("gameLoop"))
    updater.frameInterval = 1
    updater.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download