Question

I am trying to change a SKSpriteNode's texture while it is animated , but I faced with FPS dropping ! it goes down from 60 to 30 ! on both device and simulator ! the node is a parallel scrolling background , and here is the codes :

- (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];

}

moving background :

- (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); }}   ];

}

Updating frames :

- (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]; 


}
Was it helpful?

Solution

You're bypassing the Sprite Kit caching mechanisms by creating the image first as a UIImage. This will load the image from disk, possibly every time:

[bg runAction:[SKAction setTexture:[SKTexture textureWithImage:[UIImage imageNamed:@"edgeSpeed.png"]]]];

Besides that you're overdoing it with the action. Try again with the simplified version that'll give you the same result by simply assigning the texture:

bg.texture = [SKTexture textureWithImageNamed:@"edgeSpeed.png"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top