2011-09-02 27 views
3

我有一個CCSprite和一個CCParticleSystemQuad,它們都是CCLayer的子級。在我的更新方法中,我將發射器的位置設置爲精靈的位置,以便跟蹤周圍的精靈。煙霧會像你期望的那樣落在精靈的底部,即使你移動精靈,煙霧似乎也是背景層的一部分。Cocos2d - 如何使單個粒子跟隨圖層,而不是發射器?

問題來了,如果我匹配他們的旋轉。現在,例如,如果我的精靈來回晃動,煙霧揮動成弧形,並出現在精靈身上。

如何讓煙霧繼續沿着父層直線而不是隨着精靈旋轉?當我移動它們時,它們不會與精靈一起轉換,那麼爲什麼它們會隨之旋轉呢?

編輯:添加代碼...

- (id)init 
{ 
    if (!(self = [super init])) return nil; 

    self.isTouchEnabled = YES; 

    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 

    sprite = [CCSprite spriteWithFile:@"Icon.png"]; // declared in the header 
    [sprite setPosition:ccp(screenSize.width/2, screenSize.height/2)]; 
    [self addChild:sprite]; 

    id repeatAction = [CCRepeatForever actionWithAction: 
         [CCSequence actions: 
         [CCRotateTo actionWithDuration:0.3f angle:-45.0f], 
         [CCRotateTo actionWithDuration:0.6f angle:45.0f], 
         [CCRotateTo actionWithDuration:0.3f angle:0.0f], 
         nil]]; 
    [sprite runAction:repeatAction]; 

    emitter = [[CCParticleSystemQuad particleWithFile:@"jetpack_smoke.plist"] retain]; // declared in the header - the particle was made in Particle Designer 
    [emitter setPosition:sprite.position]; 
    [emitter setPositionType:kCCPositionTypeFree]; // ...Free and ...Relative seem to behave the same. 
    [emitter stopSystem]; 
    [self addChild:emitter]; 

    [self scheduleUpdate]; 

    return self; 
} 


- (void)update:(ccTime)dt 
{ 
    [emitter setPosition:ccp(sprite.position.x, sprite.position.y-sprite.contentSize.height/2)]; 
    [emitter setRotation:[sprite rotation]]; // if you comment this out, it works as expected. 
} 

// there are touches methods to just move the sprite to where the touch is, and to start the emitter when touches began and to stop it when touches end. 

回答

1

我發現在不同的網站上的答案 - www.raywenderlich.com

我不知道爲什麼,這是事實,但似乎CCParticleSystems不喜歡旋轉,當你移動它們。他們不介意改變他們的角度屬性。實際上,可能有些情況下你需要這種行爲。

無論如何,我做了一個方法,調整發射器的角度屬性,它工作正常。它需要您的觸摸位置並將y分量縮放爲角度。

- (void)updateAngle:(CGPoint)location 
{ 
    float width = [[CCDirector sharedDirector] winSize].width; 
    float angle = location.x/width * 360.0f; 
    CCLOG(@"angle = %1.1f", angle); 
    [smoke_emitter setAngle:angle]; // I added both smoke and fire! 
    [fire_emitter setAngle:angle]; 
    // [emitter setRotation:angle]; // this doesn't work 
} 
0

CCSprite的anchorPoint是{0.5F,0.5F),而發射極直接從CCNode,其具有{0.0F,0.0F}的anchorPoint下降。嘗試設置發射器的anchorPoint以匹配CCSprite的。

+0

出 - 感謝您的建議,但它仍然與精靈一起來回擺動。我真的很困惑,爲什麼翻譯和旋轉的差異如此之大。我看到的唯一區別是我通過直接更改其位置並旋轉動作來移動精靈。 – Steve

相關問題