在SpriteKit文档以及WWDC的视频中,Apple建议使用zPosition来提高SpriteKit渲染性能。
我试图测量效果。不幸的是我看不到任何影响。
我创建了一个标准游戏项目:
并使用以下代码替换 touchesBegan 方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Determine the center of the view
CGPoint loc = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
// My expectation was that this has a huge defect on the FPS
self.view.ignoresSiblingOrder = YES;
// Create thousands of sprites on different layers
for (int i=-25; i<=25; i++) {
for (int j=-50; j<=50; j++) {
// create a node to group the sprites
SKNode *node = [[SKNode alloc] init];
node.position = CGPointMake(loc.x+10*i, loc.y+10*j);
node.zPosition=100;
[self addChild:node];
// Create sprites for layer 1
SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite1.zRotation=0.0;
sprite1.size=CGSizeMake(10, 10);
if (self.view.ignoresSiblingOrder) sprite1.zPosition=1;
[node addChild:sprite1];
// Create sprites for layer 2
SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite2.size=CGSizeMake(10, 10);
sprite2.zRotation=0.75;
if (self.view.ignoresSiblingOrder) sprite2.zPosition=2;
[node addChild:sprite2];
// Create sprites for layer 3
SKSpriteNode *sprite3 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite3.size=CGSizeMake(10, 10);
sprite3.zRotation=1.5;
if (self.view.ignoresSiblingOrder) sprite3.zPosition=3;
[node addChild:sprite3];
// Create sprites for layer 4
SKSpriteNode *sprite4 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite4.size=CGSizeMake(10, 10);
sprite4.zRotation=2.2;
if (self.view.ignoresSiblingOrder) sprite4.zPosition=4;
[node addChild:sprite4];
// Create sprites for layer 5
SKSpriteNode *sprite5 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite5.size=CGSizeMake(10, 10);
sprite5.zRotation=3.14;
if (self.view.ignoresSiblingOrder) sprite5.zPosition=5;
[node addChild:sprite5];
// Add some animations
SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
[sprite1 runAction:[SKAction repeatActionForever:action]];
[sprite2 runAction:[SKAction repeatActionForever:action]];
[sprite3 runAction:[SKAction repeatActionForever:action]];
[sprite4 runAction:[SKAction repeatActionForever:action]];
[sprite5 runAction:[SKAction repeatActionForever:action]];
}
}}
我的期望是在两种情况之间看到巨大差异:
self.view.ignoresSiblingOrder = YES;
和
self.view.ignoresSiblingOrder = NO;
但它只有一个FPS。
效果是低还是我做错了?我已经在iOS 5 Beta 4的iPhone 5上进行了测试。
我知道这是许多精灵在iPhone上玩真实游戏的方法。我开始使用少量精灵,发现没有效果,增加了精灵的数量,......
非常感谢 斯蒂芬
考虑到LearnCocos2d的评论,我改变了测试场景的代码。精灵更大,每个节点3个精灵完全覆盖自己。
令人惊讶的是,self.view.ignoresSiblingOrder = NO比4 FPS更快。现在我真的很困惑......
这是我的新测试代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
int iSpriteSize =100;
// Determine the center of the view
CGPoint loc = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
// My expectation was that this has a huge defect on the FPS
self.view.ignoresSiblingOrder = NO;
// Create thousands of sprites on different layers
for (int i=-5; i<=5; i++) {
for (int j=-5; j<=5; j++) {
// create a node to group the sprites
SKNode *node = [[SKNode alloc] init];
node.position = CGPointMake(loc.x+iSpriteSize*i, loc.y+iSpriteSize*j);
node.zPosition=100;
[self addChild:node];
// Create sprites for layer 1
SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite1.zRotation=0.0;
sprite1.size=CGSizeMake(iSpriteSize, iSpriteSize);
if (self.view.ignoresSiblingOrder) sprite1.zPosition=1;
[node addChild:sprite1];
// Create sprites for layer 2
SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite2.size=CGSizeMake(iSpriteSize, iSpriteSize);
sprite2.zRotation=0.0;
if (self.view.ignoresSiblingOrder) sprite2.zPosition=2;
[node addChild:sprite2];
// Create sprites for layer 3
SKSpriteNode *sprite3 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite3.size=CGSizeMake(iSpriteSize, iSpriteSize);
sprite3.zRotation=0.0;
if (self.view.ignoresSiblingOrder) sprite3.zPosition=3;
[node addChild:sprite3];
// Create sprites for layer 4
SKSpriteNode *sprite4 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite4.size=CGSizeMake(iSpriteSize, iSpriteSize);
sprite4.zRotation=2.2;
if (self.view.ignoresSiblingOrder) sprite4.zPosition=4;
[node addChild:sprite4];
// Create sprites for layer 5
SKSpriteNode *sprite5 = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite5.size=CGSizeMake(iSpriteSize, iSpriteSize);
sprite5.zRotation=3.14;
if (self.view.ignoresSiblingOrder) sprite5.zPosition=5;
[node addChild:sprite5];
// Add some animations
SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
[sprite1 runAction:[SKAction repeatActionForever:action]];
[sprite2 runAction:[SKAction repeatActionForever:action]];
[sprite3 runAction:[SKAction repeatActionForever:action]];
[sprite4 runAction:[SKAction repeatActionForever:action]];
[sprite5 runAction:[SKAction repeatActionForever:action]];
}
}}