简体   繁体   中英

SpriteKit max number of nodes

I am creating a SpriteKit game with a tiled map. Each tile is an SKSprite node. When I have about 800 tiles, there are no problems. But if I try to increase the size of the map to around 2000 tiles, my FPS goes from 60 to 20. The number of tile nodes on the screen doesn't change (about 80), just the number of nodes off-screen. Any ideas of what could be causing this, or how to remedy it?

There doesn't appear to be a defined max number of nodes. It really depends on the amount of available free memory on your device. For example consider the following code:

int NODE_LIMIT = 375000
....

    for (int i = 0; i<NODE_LIMIT; i++) {
        SKNode *node = [SKNode node];
        [self addChild:node];
    }

I can create 375000 nodes in my sprite kit game. But as I increase the number above that, my device runs out of memory. The amount of free memory on your device will vary depending on a number of factors. As mentioned in the comments, the reason your frame rate slows down, is because the physics simulation runs even for nodes which are not visible on screen.

To maintain a high frame rate, get rid of physics bodies which are not visible, or which do not need to be simulated every frame. You could do this by adding sprites / physics bodies only when they are in the viewable part of the screen, and removing them when they are not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM