Ios6 使用CCParticleSystemQuad分配ARC内存
我使用ARC进行内存管理 在游戏开始之前,我使用了一个预加载场景,其中我加载了很多将在游戏中使用的CCParticleSystemQuad对象。这一部分进展顺利,以下是我的代码:Ios6 使用CCParticleSystemQuad分配ARC内存,ios6,cocos2d-iphone,automatic-ref-counting,Ios6,Cocos2d Iphone,Automatic Ref Counting,我使用ARC进行内存管理 在游戏开始之前,我使用了一个预加载场景,其中我加载了很多将在游戏中使用的CCParticleSystemQuad对象。这一部分进展顺利,以下是我的代码: // // ReadySteadyGo.m // // Created by Nik on 23/05/13. // Copyright 2013 __MyCompanyName__. All rights reserved. // #import "ReadySteadyGoLayer.h" #import
//
// ReadySteadyGo.m
//
// Created by Nik on 23/05/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "ReadySteadyGoLayer.h"
#import "QuickStartLayer.h"
@implementation ReadySteadyGoLayer
// Helper class method that creates a Scene with the ReadySteadyLayer as the only child.
+(CCScene *) scene{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
ReadySteadyGoLayer *layer = [ReadySteadyGoLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
@synthesize particleExplosion;
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
if( (self=[super init]) ) {
compteur = 3;
plus10LablesArray = [[NSMutableArray alloc] initWithCapacity:400];
whiteParticlesArray = [[NSMutableArray alloc] initWithCapacity:500];
bombParticlesArray = [[NSMutableArray alloc] initWithCapacity:50];
self.particleExplosion = [[CCParticleSystemQuad alloc] init];
bombExplosion = [[CCParticleSystemQuad alloc] init];
plus10Label = [[CCLabelTTF alloc] init];
displayCompteur = [[CCLabelTTF alloc] init];
self.particleExplosion = [CCParticleSystemQuad particleWithFile:@"whiteExplosion.plist"];
bombExplosion = [CCParticleSystemQuad particleWithFile:@"explosion.plist"];
}
[self schedule:@selector(initAll)];
[self schedule:@selector(compt)];
return self;
}
-(void)initAll{
// Create a new NSOperationQueue instance.
operationQueue = [[NSOperationQueue alloc] init];
// Create a new NSOperation object using the NSInvocationOperation subclass.
// Tell it to run the counterTask method.
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(initWhiteParticles)
object:nil];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(initBombParticles)
object:nil];
NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(initPlus10Labels)
object:nil];
// Add the operation to the queue and let it to be executed.
[operationQueue addOperation:operation1];
[operationQueue addOperation:operation2];
[operationQueue addOperation:operation3];
[self unschedule:@selector(initAll)];
}
- (void)compt{
CGSize winSize = [CCDirector sharedDirector].winSize;
if (compteur == 3){
displayCompteur = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%i...", compteur] fontName:@"Helvetica" fontSize:120];
displayCompteur.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:displayCompteur z:1000];
compteur--;
[self unschedule:@selector(compt)];
[self schedule:@selector(boucle) interval:1.0f];
}
else if (compteur == 2){
displayCompteur.string = [NSString stringWithFormat:@"%i..", compteur];
compteur--;
[self schedule:@selector(compt) interval:1.0f];
}
else if (compteur == 1){
displayCompteur.string = [NSString stringWithFormat:@"%i.", compteur];
compteur--;
[self schedule:@selector(compt) interval:1.0f];
}
else if (compteur == 0){
displayCompteur.string = @"GO";
displayCompteur.fontSize = 25;
compteur--;
[self schedule:@selector(compt) interval:1.0f];
}
else{
[self unschedule:@selector(compt)];
[[CCDirector sharedDirector] pushScene:[QuickStartLayer scene:plus10LablesArray:whiteParticlesArray: bombParticlesArray]];
}
}
我创建了一个清理粒子的函数,它是:
- (void)cleanParticles{
for (int i = 0; i < [particleToDelete count]; i++) {
id object = [particleToDelete objectAtIndex:i];
[particleToDelete removeObject:object];
[self removeChild:object cleanup:YES];
}
}
所以我移除了所有的对象,ARC应该清理这些对象的内存,对吗?问题是它没有
谢谢你们的帮助,伙计们:)
编辑1:
我真的被卡住了。。。我删除NSMutableArray中的所有对象,但ARC不会解除分配它们。有人来帮忙吗
Thx这一点似乎很明显。所有粒子都存储在
\u whiteParticlesArray
中,对吗?因此,只要粒子仍在该阵列中,它们就会被阵列保留并保留在内存中
当您移除所有对象并且仪器中的内存使用情况没有(显著)改变时,您可能还存在其他泄漏。您应该在仪器中检查有多少粒子对象是活动的。并确保在更改场景后,场景实例本身已解除分配
顺便说一句,提前创建数百个粒子系统不是很有效。在需要时创建粒子时是否存在性能问题?如果没有,则动态创建它们。请记住,每个粒子系统本身使用大约500个字节,因此仅对于类实例,500个粒子系统的大小大约为250 KB
您仍然可以预加载纹理,这是最重要的部分。您所需要做的只是创建一种使用嵌入纹理的粒子类型,或者使用纹理缓存预加载没有嵌入纹理的粒子系统plists的纹理。我签入了仪器,没有泄漏。。。好的,我将检查有多少粒子对象处于活动状态。你说的是哪一幕?我的预加载场景还是我的游戏场景?是的,我有性能问题,因为在我的游戏中,一旦用户触摸屏幕,就会出现粒子效果。如果你多次触摸屏幕,FPS会下降(约25 FPS),现在是(55-60)。我尝试使用粒子系统管理器,但我的addChild遇到了问题。事实上,我不能再加上同样的问题。我不知道该怎么做你最后的建议。非常感谢。
explosion = [_whiteParticlesArray objectAtIndex:nextWhiteParticle];
nextWhiteParticle++;
explosion.position = ccp(posX, posY);
[particleToDelete addObject:explosion];
[self addChild: explosion];
- (void)cleanParticles{
for (int i = 0; i < [particleToDelete count]; i++) {
id object = [particleToDelete objectAtIndex:i];
[particleToDelete removeObject:object];
[self removeChild:object cleanup:YES];
}
}
[self unschedule:@selector(countDown:)];
[self unschedule:@selector(addAllSprites:)]
[self unschedule:@selector(displayScore:)];
[self unschedule:@selector(cleanParticles)];
[self unschedule:@selector(cleanLabels)];
[_whiteParticlesArray removeAllObjects];
[_bombParticlesArray removeAllObjects];
[_plus10LabelsArray removeAllObjects];
[self removeChild:_spriteSheet cleanup:YES];