2010-10-01 56 views
0

全部 我只是有一個問題,粒子效應並沒有出現在所有的時間。我使用Objective C和cocos2d編碼爲iPhone編碼。 下面是有問題的代碼。cocos2d粒子效應沒有出現

CCParticleExplosion *emitter; 

    emitter = [[CCParticleExplosion alloc] initWithTotalParticles:30]; 
    emitter.texture = [[CCTextureCache sharedTextureCache] addImage:@"particle_bubble.png"]; 
    emitter.position = ccp(MidX,MidY); 
    emitter.life =0.5; 
    emitter.duration = 0.5; 

    emitter.speed = 60; 

    [self addChild:emitter]; 
    emitter.autoRemoveOnFinish = YES; 


    //////////////////////////////////////////////////// 

    CCParticleMeteor *emitter2; 

    emitter2 = [[CCParticleMeteor alloc] initWithTotalParticles:150]; 

    emitter2.texture = [[CCTextureCache sharedTextureCache] addImage:@"fire_particle.png"]; 
    emitter2.position = ccp(MidX,MidY); 
    emitter2.life = 0.5; 
    emitter2.duration = 2; 

    emitter2.speed = 60; 

    id emitMove = [CCMoveTo  actionWithDuration:0.5 position:HUD.moonSprite.position ]; 


    [self addChild:emitter2 z:1]; 

    [emitter2 runAction:[CCSequence actions:emitMove, nil]]; 
    emitter2.autoRemoveOnFinish = YES; 

這段代碼是在相同的函數之後,如圖所示。 但有時第二個粒子效應沒有創建,我不知道爲什麼。第一個粒子效應總是沒有問題,所以我確定它正在進入功能正確,但有時(幾乎50%)的第二個流星發射器不顯示。我曾嘗試搞亂z值,以確保它不會隱藏在其他對象後面,並且看起來不是問題。任何人有任何想法,爲什麼會發生這種情況? 謝謝 G

+0

您是否嘗試過不移動第二個發射器? – jv42 2010-10-01 12:11:19

+0

是的,它的相同的東西,有時它出現並保持在它產卵的地方,其他時間什麼都沒有 – glogic 2010-10-01 16:01:34

+0

當我刪除autoRemoveOnFinish它每次工作。任何人有任何想法爲什麼這是什以及如何我釋放發射器,如果我不使用autoRemoveOnFinish?如果我有多個發射器在飛行中創建,我怎麼能釋放他們沒有autoremoveonfinish? – glogic 2010-10-02 11:12:28

回答

0

嘗試這種情況:

  1. 定義在一個局部變量的發射器(.h)中上面的代碼之前

  2. 調用此:

    if (emitter.parent == self) { 
        NSLog(@"em1 released"); 
        [emitter release]; 
    } 
    
    if (emitter2.parent == self) { 
        NSLog(@"em2 released"); 
        [emitter2 release]; 
    } 
    

    這用來檢查發射器是一個孩子,並刪除它,所以你可以刪除emitter.autoRemoveOnFinish所以你的發射器將顯示每一次