我设置了一个包含 200 个精灵节点的场景,并将它们随机放置在屏幕上,如下所示:
for(int i = 0; i < bubbleCount; i++) {
CGSize size = [self getRandomSize];
SKSpriteNode *bubble = [SKSpriteNode spriteNodeWithTexture:texture size:size];
bubble.anchorPoint = CGPointMake(0.5, 0.5);
bubble.position = [self getRandomPosition];
bubble.name = BUBBLE;
SKPhysicsBody *physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:size.width/2];
physicsBody.dynamic = YES;
physicsBody.affectedByGravity = NO;
physicsBody.allowsRotation = NO;
physicsBody.categoryBitMask = 0;
physicsBody.contactTestBitMask = 0;
physicsBody.collisionBitMask = 0;
bubble.physicsBody = physicsBody;
[self addChild:bubble];
}
只要节点彼此保持距离,我就能获得稳定的 60 fps。当我施加力使节点开始向场景中心移动并相互重叠时,性能会急剧下降到 2-3 fps。我猜它与碰撞检测有关,而不是渲染(如果我用更大的半径初始化物理体,一开始性能就已经很低了)。我已将类别、联系人和碰撞掩码设置为 0,但这没有帮助。