我正在尝试在动画时更改SKSpriteNode的纹理,但我遇到了FPS掉线!它从60下降到30!在设备和模拟器上!节点是并行滚动背景,这里是代码:
- (void)createBricksEdge {
brickEdges = [[SKSpriteNode alloc]initWithImageNamed:@"edge.png"];
brickEdges.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));;
brickEdges.name = @"edge";
[self addChild:brickEdges];
bEdge1 = [[SKSpriteNode alloc]initWithImageNamed:@"edge.png"];
bEdge1.position = CGPointMake(brickEdges.position.x,brickEdges.position.y+(brickEdges.size.height));
bEdge1.name = @"edge";
[self addChild:bEdge1];
}
移动背景:
- (void)moveBg
{
[self enumerateChildNodesWithName:@"edge" usingBlock: ^(SKNode *node, BOOL *stop)
{
SKSpriteNode * bg = (SKSpriteNode *) node;
if (isSpeedBonus == YES) {
SPEED = 200;
} else {
switch (speedCriteria) {
case 0:
SPEED = 7.0;
break;
case 1:
SPEED = 11;
NSLog(@"FPS drops");
[bg runAction:[SKAction setTexture:[SKTexture textureWithImage:[UIImage imageNamed:@"edgeSpeed.png"]]]];
//I also tried : [bg setTexture:textre];
break;
.
.
.
.
.
}
}
bg.position = CGPointMake(bg.position.x , bg.position.y - SPEED);
if (bg.position.y <= -bg.size.width)
{ bg.position = CGPointMake(bg.position.x ,bg.position.y + bg.size.height*2); }} ];
}
更新框架:
- (void)speedOMeter
{
int i = (int)[scoreLabel.text integerValue];
if (i <= 1000) {
speedCriteria = 0;
} else if (i <= 1500) {
speedCriteria = 1;
} else if (i <=2000) {
speedCriteria = 2;
} else if (i <= 2500) {
.
.
.
.
}
- (void)update:(CFTimeInterval)currentTime {
if (_lastUpdateTime)
{
_dt = currentTime - _lastUpdateTime;
}
else
{
_dt = 0;
}
_lastUpdateTime = currentTime;
[self moveBg]; [self speedOMeter];
}
答案 0 :(得分:1)
您首先将图像创建为UIImage,从而绕过Sprite Kit缓存机制。这将从磁盘加载图像,可能每次:
[bg runAction:[SKAction setTexture:[SKTexture textureWithImage:[UIImage imageNamed:@"edgeSpeed.png"]]]];
除此之外,你对行动过度了。再次使用简化版本,只需指定纹理即可为您提供相同的结果:
bg.texture = [SKTexture textureWithImageNamed:@"edgeSpeed.png"];