views:

59

answers:

2

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 ...
}
A: 

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.

The Alchemist
I don't know what you mean by "sprite-by-sprite", isn't that what is happening in the for loops? I added the sprite declarations and initializations, to my post above. I don't know what else would be helpful.
Cassie
Oh, sorry, I mis-read your code. I think you're already doing rotating them one-by-one instead of row 0 of sprite 0, row 0 of sprite 1, etc.I recommend Jeff B's suggestion: preload the rotations or just change the `rotation` property.Even if you preload all the rotations, you'll only have 8*8*4 variations because it seems you're rotating only by 90 degrees.
The Alchemist
+1  A: 

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.

Jeff B
So if I understand you right, you are saying that changing the texture of a Sprite is less expensive than rotating it. Is this correct?
Cassie
It is, if it is preloaded (a la frame animation). Otherwise, cocos is calculating the rotation every time and then drawing to the screen.
Jeff B