我在我的cocos2d iPhone项目中启用了ARC。现在我面临一个问题,即目前在屏幕上或屏幕外不可见的一些元素。我想在运行时释放那些元素占用的内存。由于我在我的项目中实现了ARC,它将在编译时释放该内存。但我认为我应该在运行时释放内存以进行内存优化。为了演示,我使用4个tile map对象实现了Infinite平铺地图。文件如下所示。
HelloWorldLayer.h
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#define PTM_RATIO 32
@interface HelloWorldLayer : CCLayer
{
CCTMXTiledMap *map1;
CCTMXTiledMap *map2;
CCTMXTiledMap *map3;
CCTMXTiledMap *map4;
CCTMXTiledMap *defaultMap;
float mapToBeAdded;
int whichMap;
CGSize screenSize;
int speed;
BOOL complexMap;
CCMenu *menu;
}
+(CCScene *) scene;
@end
HelloworldLayer.mm
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
#import "PhysicsSprite.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init])) {
screenSize = [[CCDirector sharedDirector] winSize];
defaultMap = [CCTMXTiledMap tiledMapWithTMXFile:@"bgdefaultmap.tmx"];
defaultMap.position = ccp(0, screenSize.height-defaultMap.mapSize.height*defaultMap.tileSize.height);
[self addChild:defaultMap];
map1=[CCTMXTiledMap tiledMapWithTMXFile:@"bgmap11.tmx"];
map1.position = ccp(0, defaultMap.position.y-map1.mapSize.height*map1.tileSize.height);
[self addChild:map1];
mapToBeAdded = map1.position.y + (map1.mapSize.height*map1.tileSize.height)/4;
whichMap = 2;
speed = 3;
complexMap=NO;
[self schedule:@selector(update:)];
CCMenuItemFont * button = [CCMenuItemFont itemWithString:@"MENU" target:self selector:@selector(onMenuButton)];
menu = [CCMenu menuWithItems:button, nil];
menu.position=ccp(screenSize.width/2, screenSize.height/2);
[self addChild:menu z:10];
}
return self;
}
-(void)onMenuButton{
[[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];
[CCAnimationCache purgeSharedAnimationCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeAllTextures];
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene] withColor:ccWHITE]];
}
-(void)update:(ccTime)dt{
if(-1*self.position.y<mapToBeAdded){
if(!complexMap){
if(whichMap==2){
[self removeChild:defaultMap cleanup:YES];
map2 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap22.tmx"];
map2.position=ccp(0, map1.position.y-map2.mapSize.height*map2.tileSize.height);
whichMap =3;
mapToBeAdded = map2.position.y + (map2.mapSize.height*map2.tileSize.height)/4;
[self addChild:map2];
}else if(whichMap==3){
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background1.png"];
[self removeChild:map1 cleanup:YES];
map3 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap33.tmx"];
map3.position=ccp(0, map2.position.y-map3.mapSize.height*map3.tileSize.height);
[self addChild:map3];
whichMap =4;
mapToBeAdded = map3.position.y + (map3.mapSize.height*map3.tileSize.height)/4;
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background1.png"];
}else if(whichMap==4){
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background2.png"];
[self removeChild:map2 cleanup:YES];
map4 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap44.tmx"];
map4.position=ccp(0, map3.position.y-map4.mapSize.height*map4.tileSize.height);
[self addChild:map4];
complexMap=YES;
whichMap =1;
mapToBeAdded = map4.position.y + (map4.mapSize.height*map4.tileSize.height)/4;
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background2.png"];
}
}else{
if(whichMap==1){
[self removeChild:map3 cleanup:YES];
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background3.png"];
map1 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap1.tmx"];
map1.position=ccp(0, map4.position.y-map1.mapSize.height*map1.tileSize.height);
[self addChild:map1];
whichMap =2;
mapToBeAdded = map1.position.y + (map1.mapSize.height*map1.tileSize.height)/4;
}else if(whichMap==2){
[self removeChild:map4 cleanup:YES];
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background4.png"];
map2 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap2.tmx"];
map2.position=ccp(0, map1.position.y-map2.mapSize.height*map2.tileSize.height);
[self addChild:map2];
whichMap =3;
mapToBeAdded = map2.position.y + (map2.mapSize.height*map2.tileSize.height)/4;
}else if(whichMap==3){
[self removeChild:map1 cleanup:YES];
[[CCTextureCache sharedTextureCache] removeTextureForKey:@"background1.png"];
map3 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap3.tmx"];
map3.position=ccp(0, map2.position.y-map3.mapSize.height*map3.tileSize.height);
[self addChild:map3];
whichMap =4;
mapToBeAdded = map3.position.y + (map3.mapSize.height*map3.tileSize.height)/4;
}else if(whichMap==4){
[self removeChild:map2 cleanup:YES];
map4 = [CCTMXTiledMap tiledMapWithTMXFile:@"bgmap4.tmx"];
map4.position=ccp(0, map3.position.y-map4.mapSize.height*map4.tileSize.height);
[self addChild:map4];
complexMap=YES;
whichMap =1;
mapToBeAdded = map4.position.y + (map4.mapSize.height*map4.tileSize.height)/4;
}
}
[CCAnimationCache purgeSharedAnimationCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
}
float y = self.position.y;
y = y+speed;
self.position =ccp(self.position.x, y);
menu.position = ccp(menu.position.x, menu.position.y-speed);
printf("%d\n",[[self children] count]);
}
@end
所以我想知道可以在运行时释放内存吗?我该怎么办? 任何形式的帮助或指导都会有所帮助。
答案 0 :(得分:0)
在ARC下发布一个由强引用持有的对象,你必须将所有对它的引用都取消。