Cocos2d iphone 在启用ARC的cocos2d iphone中,在运行时释放内存
我已经在我的cocos2d iPhone项目中启用了ARC。现在我面临的一个问题是,一些元素目前在屏幕上或屏幕外都不可见。我想在运行时释放这些元素占用的内存,因为我在项目中实现了ARC,它将在编译时释放这些内存。但我认为我应该在运行时释放内存以优化内存。为了演示,我使用4个平铺贴图对象实现了无限平铺贴图。这些文件如下所示 HelloWorldLayer.hCocos2d iphone 在启用ARC的cocos2d iphone中,在运行时释放内存,cocos2d-iphone,Cocos2d Iphone,我已经在我的cocos2d iPhone项目中启用了ARC。现在我面临的一个问题是,一些元素目前在屏幕上或屏幕外都不可见。我想在运行时释放这些元素占用的内存,因为我在项目中实现了ARC,它将在编译时释放这些内存。但我认为我应该在运行时释放内存以优化内存。为了演示,我使用4个平铺贴图对象实现了无限平铺贴图。这些文件如下所示 HelloWorldLayer.h #import "cocos2d.h" #import "Box2D.h" #import "GLES-Render.h" #define
#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
#导入“HelloWorldLayer.h”
#导入“AppDelegate.h”
#导入“physicsprite.h”
@HelloWorldLayer的实现
+(场景*)场景
{
CCScene*scene=[CCScene节点];
HelloWorldLayer*层=[HelloWorldLayer节点];
[场景添加子对象:层];
返回场景;
}
-(id)init
{
if((self=[super init])){
屏幕大小=[[CCDirector sharedDirector]winSize];
defaultMap=[CCTMXTiledMap tiledMapWithTMXFile:@“bgdefaultmap.tmx”];
defaultMap.position=ccp(0,屏幕大小.高度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;
速度=3;
complexMap=NO;
[自行安排:@选择器(更新:)];
CCMenuItemFont*按钮=[CCMenuItemFont itemWithString:@“菜单”目标:自选择器:@选择器(onMenuButton)];
菜单=[CCMenu menuWithItems:按钮,无];
menu.position=ccp(屏幕大小.宽度/2,屏幕大小.高度/2);
[自添加子菜单z:10];
}
回归自我;
}
-(void)onmenu按钮{
[[CCTextureCache sharedTextureCache]dumpCachedTextureInfo];
[CCAnimationCache purgeSharedAnimationCache];
[[CCSpriteFrameCache SharedPriteFrameCache]移除PriteFrames];
[[CCDirector sharedDirector]purgeCachedData];
[[CCTextureCache sharedTextureCache]removeAllTextures];
[[CCDirector sharedDirector]替换场景:[CCTransitionFade transitionWithDuration:1.0场景:[HelloWorldLayer场景]withColor:ccWHITE];
}
-(无效)更新:(ccTime)dt{
如果(-1*self.position.y在ARC下要释放由强引用持有的对象,必须将对它的所有引用置零。在我的情况下,大部分内存被平铺贴图占用。我将它们逐个添加,这样总内存占用量就会增加。但当我使用remove child从层中移除平铺贴图时,此时内存不会被释放。所以如何释放TileMap动态占用的内存我将tile map的所有对象设为零,然后我将删除这些对象。在这之后,我应该如何释放该对象的真实内存?