1

I am building simple game with Sprite Kit in which level is built from identical rectangular blocks. But when I add to many block (i.e. 1000), game becomes very slow (FPS drops to 20). Here's how I add blocks to scene:

-(void)drawLevel
{
    SKSpriteNode *shelf = [SKSpriteNode spriteNodeWithTexture:_initialLevel.earthBoxTexture];
    shelf.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:shelf.size];
    shelf.physicsBody.dynamic=NO;
    shelf.physicsBody.categoryBitMask = platformCategory;

    for (NSValue *positionValue in _initialLevel.boxPositions)
    {
        shelf.position = positionValue.CGPointValue;
        [_platformsNode addChild:[shelf copy]];
    }

    [_world addChild:_platformsNode];
}

_world is a child of main scene. I create "shelf" once and than copy it. How should I do that to obtain good FPS?

App was tested on the iPhone 5.

user2923525
  • 45
  • 1
  • 5
  • Is this on device or in the simulator ? I think the answer given is correct, but would be worthwhile to add that information in the question. 20fps in simulator and 20fps on device are very different situations. – prototypical Oct 26 '13 at 18:39
  • For a solution that uses just 1 node, see http://stackoverflow.com/a/37768928/763355 – MoDJ Jun 11 '16 at 23:05

2 Answers2

2

How about not using this many sprites with physics bodies?

A thousand is definitely several hundred too many, especially if they're all on screen and if many of the bodies generate contact events (ie when stacked). Though in this case they are all static, so they shouldn't generate contact events. Which still leaves lots of sprites to be drawn.

Any code that is performed on all or most of the nodes will make matters worse, too. Just for reference, one app I have draws around 400 sprites on the screen, updates them all when scrolling and runs custom game logic - this app barely makes 60 fps on an iPod touch 5G.

Also, be sure to test performance on the device. The Simulator can not be used to assess performance.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks for your answer! Majority of those blocks are not on the screen (about 100 are visible on the screen). So far I test it with contact events turned off but it's still very slow. As those blocks are part of the level I need them not to be penetrable - can I do that without physics body? – user2923525 Oct 26 '13 at 21:24
0

I would recommend using a system that uses a sprite sheet map, which would save tremendous resources if done correctly. To get it down is a bit tricky, but each section of the sprite sheet can be set as different objects which can interact with your sprites in different ways. Good luck if your still looking for ways to tackle this issue, I'm still learning Tiled with SpriteKit myself, but it seems very promising.

http://www.raywenderlich.com/62049/sprite-kit-tutorial-make-platform-game-like-super-mario-brothers-part-1 - Tutorial explaining using Tiled with SpriteKit

http://www.mapeditor.org - Tiled website

stickynugz
  • 41
  • 7