SKShapeNode consuming too much memory and slowing

2019-02-07 18:44发布

I am using SKShapeNode to to create a mountain like object . I use CGMutablePathRef to give the points to my path

SKShapeNode *shapenode = [[SKShapeNode alloc] init];

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
......
for (int i = 0; i < [refData count];i++) {


    CGPathAddLineToPoint(path, NULL, ((float)i/[refData count])*screenWidthBoundry,      [[refData objectAtIndex:i] doubleValue]);
}


CGPathAddLineToPoint(path, NULL, screenWidthBoundry, 0);
CGPathAddLineToPoint(path, NULL, 0, 0);

shapenode.path = path;
shapenode.antialiased = YES;
shapenode.fillColor = [SKColor colorWithRed:17.0/255.0 green:108.0/255.0 blue:125.0/255.0 alpha:1.0];
//shapenode.strokeColor = [UIColor clearColor];

shapenode.lineWidth = 1.0f;

shapenode.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path];

shapenode.physicsBody.affectedByGravity = NO;
shapenode.physicsBody.categoryBitMask = CollisionTypeMountain;
shapenode.physicsBody.collisionBitMask = 0;
shapenode.physicsBody.contactTestBitMask = CollisionTypeBird;

CGPathRelease(path);
return shapenode;

My Problem is that SkShapeNode is slowing down the animation on iPhone 4 (runs smoothly on iPad and iPhone 5 but still not 60 fps) and consuming lot of memory. Can I cache skshapnode object to run animation smoothly ? Any Help is appreciated !!

2条回答
别忘想泡老子
2楼-- · 2019-02-07 19:08

It's hard to tell what is going on in your program based on just the code you have shown. If you are creating a lot of sprites based on the method above then memory and performance will suffer.

Consider creating all of the shapes and images you need for your program with an external image editor like Fireworks, Photoshop, etc... and placing them into a texture atlas. Using an atlas will greatly increase performance and reduce memory pressure.

Apple's Sprite Kit Programming Guide makes specific mention of this issue:

If each texture is treated as a separate object, then Sprite Kit and the graphics hardware must work harder to render scenes—and your game’s performance might suffer. Specifically, Sprite Kit must make at least one drawing pass per texture. To avoid making multiple drawing passes, Sprite Kit uses texture atlases to collect related images together. You specify which assets should be collected together, and Xcode builds a texture atlas automatically. Then, when your game loads the texture atlas, Sprite Kit manages all the images inside the atlas as if they were a single texture. You continue to use SKTexture objects to access the elements contained in the atlas.

查看更多
唯我独甜
3楼-- · 2019-02-07 19:33

I was running into the same problem with SKShapeNodes consuming too much cpu resources and killing the framerate. Use your SKView's textureFromNode: method to create an SKTexture. Now create an SKSpriteNode with the SKTexture your view generated. This will fix your performance issues.

Create an SKTexture from an SKShapeNode

查看更多
登录 后发表回答