私に同行してください私は長いコードを投稿しようとしています。 私はCCSpriteをサブクラス化しています:CCSpriteのサブクラスでアクションを実行する
@interface Character : CCSprite {
}
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect;
@end
@implementation Character
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
NSLog(@"in initWithTexture");
if((self=[super initWithTexture:texture rect:rect]))
{
}
return self;
}
-(void) dealloc
{
NSLog(@"character dealloced");
[super dealloc];
}
@end
質問#1:それは正しいのですか?
私はこのスプライトをクラス変数として含み、別のCCSpriteを含むレイヤーを持っています。
Character *characterSprite;
CCSprite *burpSprite;
この層はまた、2つのCCAnimationオブジェクトを有する:このクラスで
CCAnimation *burpAnim;
CCAnimation *burpParticlesAnim;
Iは、2つの方法があります - (ボイド)loadBurpAnimationと、 - (void)unloadBurpAnimation;
私はこのように書きされ、スレッドでげっぷアニメーションをロードし、それを再生するには、メインスレッド上でメソッドを呼び出す必要が-(void) loadBurpAnimation {
NSString* burpFile;
NSString* burpFilePVR;
NSString* burpParticlesFile;
NSString* burpParticlesFilePVR;
burpFile = @"latestBurp1-1.plist";
burpFilePVR = @"latestBurp1-1.pvr.ccz";
burpParticlesFile = @"burpParticles1-1.plist";
burpParticlesFilePVR = @"burpParticles1-1.png";
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:burpFile texture:[[CCTextureCache sharedTextureCache] addImage:burpFilePVR]];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:burpParticlesFile texture:[[CCTextureCache sharedTextureCache] addImage:burpParticlesFilePVR]];
NSMutableArray *burpFrames = [NSMutableArray array];
for(int i = 231; i <= 268; ++i) {
[burpFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"burp.%d.png", i]]];
}
burpAnim = [[CCAnimation alloc] initWithFrames:burpFrames delay:0.04f];
[burpFrames removeAllObjects];
//Burp Particles
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:burpParticlesFile];
NSMutableArray *burpParticlesFrames = [NSMutableArray array];
for(int i = 3; i <= 37; ++i) {
[burpParticlesFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Burp_%05d.png", i]]];
}
burpParticlesAnim = [[CCAnimation alloc] initWithFrames:burpParticlesFrames delay:0.04f];
[burpParticlesFrames removeAllObjects];
}
-(void) unloadBurpAnimation {
[burpAnim release];
[burpParticlesAnim release];
}
:
-(void) loadAnimation {
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
[dictLock lock];
if([EAGLContext setCurrentContext:auxEAGLcontext]) {
[self loadBurpAnimation];
[self performSelectorOnMainThread:@selector(burpAnimation) withObject:nil waitUntilDone:NO];
} else {
CCLOG(@"cocos2d: TetureCache: EAGLContext error");
}
[dictLock unlock];
[autoreleasepool drain];
}
私は「個別のアニメーションをロードする方法があります知っていますasynchAddImage "ですが、これはここでの問題ではありません。
-(void) burpAnimation {
CCAction *burpAction = [CCSequence actions:
[CCAnimate actionWithAnimation:burpAnim restoreOriginalFrame:NO],
[CCCallFunc actionWithTarget:self selector:@selector(animationDone)],nil];
CGSize winSize = [CCDirector sharedDirector].winSize;
characterSprite = [[Character alloc] initWithSpriteFrameName:@"burp.231.png"];
characterSprite.position = ccp(winSize.width/2, winSize.height/2);
[characterSprite setScale:1.5];
CCAction *particlesAction = [CCSequence actions:
[CCAnimate actionWithAnimation:burpParticlesAnim restoreOriginalFrame:NO],
nil];
burpSprite = [[CCSprite alloc] initWithSpriteFrameName:@"Burp_00003.png"];
burpSprite.position = ccp(winSize.width/2-50, winSize.height/2-80);
[self addChild:characterSprite];
[characterSprite release];
[self addChild:burpSprite];
[burpSprite release];
[characterSprite runAction:burpAction];
[burpSprite runAction:particlesAction];
}
CCCallFuncセレクタは、アニメーションの後に呼び出されたときに、私が直面しています本当の問題は次のとおりです。
- (void)animationDone {
//Here characterSprite retain count is 2
NSLog(@"B4 stop char rc %d", [characterSprite retainCount]);
[characterSprite stopAllActions];
//Here characterSprite retain count is still 2
NSLog(@"B4 stop char rc %d", [characterSprite retainCount]);
[self removeChild:characterSprite cleanup:YES];
>>> //Here characterSprite retain count is 1 WHY?
>>> NSLog(@"char rc %d", [characterSprite retainCount]);
//Here burpSprite retain count is 2
NSLog(@"particles rc %d", [burpSprite retainCount]);
[burpSprite stopAllActions];
//Here burp retain count is 1
NSLog(@"particles rc %d", [burpSprite retainCount]);
[self removeChild:burpSprite cleanup:YES];
// releases the animation objects
[self unloadBurpAnimation];
//Here burpAnim retainCount = 1
NSLog(@"BURP ANIM rc = %d", [burpAnim retainCount]);
//Here burpParticlesAnim retainCount = 0 , crashes if print here obviously.
//NSLog(@"Particles ANIM rc = %d", [burpParticlesAnim retainCount]);
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
}
二つの問題: 私は1に等しく、その保持カウントを取得characterSpriteを削除したものの。 私はburpAnimをリリースしましたが、このCCSpriteFrameCacheはspriteFramesを削除できないため、テクスチャも削除されないため、保持カウントは1になります。
これらの問題の両方が相互にリンクされていますか? これを読んでいただきありがとうございました、どんな助けも大歓迎です。
ここに問題はありません!私が直面していた問題は何かでした。ちなみに、CCActionを作成してCCCallFuncを使用してスプライト(Xなど)で実行すると、removeChildを呼び出してレイヤーからスプライトを削除しても、実行が@ selectorのメソッド閉じ括弧に当たるまで解放されません。 – Asymptote
あなたは "なぜ前にforループの内容を設定してから、CCAnimationに渡してすぐにRemoveAllObjectsを削除するのですか?CCAnimationが配列をコピーしないと、あなたはただそれを空にしました配列はオートレリースされているので、空にする必要はありません。 なぜccanimationはコピーしません。私は初期化しています。コードに問題はありません.pvr.cczのロードに関するcocos2dのバグがありました。 – Asymptote
"最初の問題は、retainCountを呼び出して、返された数値が意味があると考えていることです。 なぜ意味がありませんか?私の最初のコメントを読んでください。 – Asymptote