Release memory at Run-time in cocos2d iphone with ARC enabled -
i have enabled arc in cocos2d iphone project. facing 1 problem of elements not visible on-screen or off-screen. want release memory occupied elements @ run time.as have implemented arc in project release memory @ compile time. think should release memory @ run time memory optimization. demonstration have implemented infinite tiled map using 4 tile map object. files shown below.
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
so know possible release memory @ run-time? should do? kind of or guidance helpful.
under arc release object held strong reference, have nil references it.
Comments
Post a Comment