SpriteKit max number of nodes
Asked Answered
D

2

7

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?

Daredevil answered 16/10, 2013 at 18:29 Comment(5)
The tilemap renderer in Kobold Kit only draws the visible tiles. koboldkit.com How big is your map? How many layers? Do you do something with each tile or are they just static sprites?Shaylynn
The tiles are not dynamic, but they do have physics bodies. It's a digging game, so the "player" sits on-top of these tiles via gravity, and removes them from the scene as he moves around. I would like to map to be about 40 tiles wide and 200 tiles tall.Daredevil
that makes 8.000 physics bodies, no wonder things slow down. You don't need physics though, a simple CGRectIntersect test + velocity integration sufficesShaylynn
He's right, and I haven't worked much with physics bodies, but would it be possible to apply physics to only tiles near the character, instead of the whole map at one time?Homiletics
You can change the Physics bodies to be not Dynamic (set Dynamic to false) and turn on Dynamic only we needed at the flip of a switch. This would cut down on FPS running slower. Dynamic helps ignore the simulation applied to the "ground" tile map cells. You could have a fake wall sprite offscreen that when it passes through it, the Dynamic flag gets turned on. In some games, it may be best to only use physics on key elements. Not the entire game. You can still detect collisions with Dynamic off. My current game has 1200 nodes / 60fps and not all the elements are dynamic at the same time.Hark
R
6

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.

Romanticist answered 16/11, 2013 at 8:5 Comment(1)
There's a big difference between an SKNode and a SKSpriteNode, especially the display of it...Mitch
P
0

The map tile should not be placed inside an SKSpriteNode.

Tile maps are a special tool that allow for the creation of backgrounds without the use of large arrays of nodes. Such arrays could potentially cripple the performance of your game.

According to an unofficial post on the Apple Developer Forums, it supports up to 30,400,000 tiles with no performance issue.

https://developer.apple.com/forums/thread/102601

Panamerican answered 12/6, 2023 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.