更有效地旋转 64 个 CCSprite?
我有一个 8 x 8 的精灵矩阵,我需要能够将它们同时旋转 90 度。我完成的方法是使用嵌套的 for 循环和精灵指针的二维数组。
for(row = 0;row<9;row++){
for(column = 0;column<8;column++){
[trolls[row][column] runAction:[RotateBy actionWithDuration:0.01 angle:90]];
}
}
有没有更有效的方法来做到这一点?在它们全部旋转之前似乎有一个滞后。
编辑:这是我回应炼金术士的更多代码:
@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 ...
}
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 ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从你的动作持续时间来看,你似乎希望它们立即旋转,或者非常接近。
如果您简单地手动设置旋转,似乎会更快一点:
仍然有旋转每个精灵的计算,但在动画期间不可能多次执行。
如果您只打算以 90 度增量执行此操作,则另一个考虑因素是在 4 次旋转中的每一次旋转中都有精灵,并制作帧动画。然后只需选择适当动画的帧即可。
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:
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.
我唯一能想到的就是逐个精灵地旋转精灵,以便该过程可以识别并利用预取。更多代码会有所帮助。
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.