Rotating 64 CCSprites more efficiently?

Refresh

April 2019

Views

392 time

1

I have an 8 by 8 matrix of sprites that I need to be able to rotate all of them 90 degrees at once. The way I've done it is using nested for loops, and a 2 dimensional array of sprite pointers.

for(row = 0;row<9;row++){
    for(column = 0;column<8;column++){
         [trolls[row][column] runAction:[RotateBy actionWithDuration:0.01 angle:90]]; 
    }
}

Is there a more efficient way of doing this? There seems to be a lag before they all rotate.

EDIT: here's is more of my code to respond to the alchemist:

@interface GameLayer : CCLayer {
    CCSprite *monsters[8][8];
    //other code ...
}
@property @property (nonatomic,retain) *monsters
//other code ...
@end

@implementation
@synthesize monsters

-(void)init {
    NSString *filename;
    int row,column,randnum;
    for(row = 0;row<9;row++){
        for(column = 0;column<8;column++){
            randnNum =(int)Rand(8);
            filename =stringWithFormat:@"%d.png",randnNum];
            monsters[row][column] = [[[CCSprite alloc] initWithImage:(CGImageRef)filename key:filename] autorelease];
        }
    }
 //other code ...
}

2 answers

0

The only thing I can thing of at the top of my head is to do the rotation sprite-by-sprite so the process can take recognize and take advantage of prefetching. More code would be helpful.

1

It looks like from the duration of your action that you want them to rotate instantly, or pretty close.

It seems like it would be a bit faster if you simply set the rotation manually:

for(row = 0;row<9;row++){
    for(column = 0;column<8;column++){
         trolls[row][column].rotation += 90; 
    }
}

There is still the computation of rotating each sprite, but no chance that it would have to do it more than once during the animation.

Another consideration if you are only ever going to be doing this in 90 degree increments would be to have sprites at each of the 4 rotations, and make a frame animation. Then just choose the frame of the appropriate animation.