Search code examples
iosiphonesprite-kitframe-rate

Setting the frames per second to 30 when running a sprite-kit project on a device instead of the simulator


I'm just about finished with my latest iPhone app and I ran into a problem when I was testing on my iPad. The default frames per second on the simulator was 30, but on my device its around 54 to 60. This is a major problem for me because my game is an infinite runner and moving at twice the fps I tested on makes the background and enemies fly by far too fast. I can't slow them down, so I was wondering if it was possible to limit the fps to 30.


Solution

  • Use the frameInterval of the SKView Class.

    According to the docs:

    The default value is 1, which results in your game being notified at the refresh rate of the display. If the value is set to a value larger than 1, the display link notifies your game at a fraction of the native refresh rate. For example, setting the interval to 2 causes the scene to be called every other frame, providing half the frame rate.

    In other words, if you set it to 2, every other frame will be processed which translates to 30 FPS.

    * UPDATE *

    Based on your last comment, this is how to properly set frameInterval :

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        SKView * skView = (SKView *)self.view;
        skView.frameInterval = 2;
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;
        SKScene * scene = [JKGLevel1 sceneWithSize:CGSizeMake(320, 480)];
        // SKScene * scene = [JKGMyScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;
        [skView presentScene:scene];
    }
    

    frameInterval is a property of SKView. in your comment you coded it as self.frameInterval which means you are trying to apply it to your VC by using self. Not correct.