matsmats matsmats - 2 months ago 29
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

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)
Comments