我使用 Cocos2D 编写了一个简单的游戏,其中我有一个动画精灵在纯绿色的田野中行走。
行走精灵为 64x64px,操纵杆来自 Sneakyjoystick 类,绿色区域由 5-6 个 32x32 像素组成,使用 Tiled 制作成 tilemap。使用 Tiled,我创建了一个 50 x 50 的平铺地图。
我得到 20-30 FPS,我不知道为什么。我将贴图大小减小到 10x10 贴图,它的速度达到了 50-60 FPS。我不知道该怎么办?
编辑:我为游戏层添加了初始化块。我在模拟器上测试。
-(id) init
{
if( (self=[super init] ))
{
self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"TileMap.tmx"];
self.background = [_tileMap layerNamed:@"Background"];
self.meta = [_tileMap layerNamed:@"Meta"];
self.topLayer = [_tileMap layerNamed:@"TopLayer"];
_topLayer.visible = YES;
_background.visible = YES;
_meta.visible = YES;
[self addChild:_tileMap z:-1];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"male_walkcycle.plist"];
sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"male_walkcycle.png"];
[self addChild:sceneSpriteBatchNode z:0];
CCTMXObjectGroup *objects = [_tileMap objectGroupNamed:@"Objects"];
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"];
NSAssert(spawnPoint != nil, @"SpawnPoint object not found");
int x = [[spawnPoint valueForKey:@"x"] intValue];
int y = [[spawnPoint valueForKey:@"y"] intValue];
Caster *casterPlayer = [[[Caster alloc] init] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"male_walkcycle_19.png"]];
casterPlayer.position = ccp(x, y);
casterPlayer.joystick = nil;
[sceneSpriteBatchNode addChild:casterPlayer z:1000 tag:kCasterTagValue];
[casterPlayer release];
CCSprite *sprite = [CCSprite node];
CGSize size = CGSizeMake(50,50);
GLubyte *buffer = malloc(sizeof(GLubyte)*4);
for (int i=0;i<4;i++) {buffer[i]=255;}
CCTexture2D *tex = [[CCTexture2D alloc] initWithData:buffer pixelFormat:kCCTexture2DPixelFormat_RGB5A1 pixelsWide:1 pixelsHigh:1 contentSize:size];
[sprite setTexture:tex];
[sprite setTextureRect:CGRectMake(0, 0, size.width, size.height)];
free(buffer);
sprite.position = ccp(x, y);
sprite.visible = NO;
[self addChild:sprite z:5 tag:debugBoxTagValue];
[self setViewpointCenter:casterPlayer.position];
[self scheduleUpdate];
}
return self;
}