2011-09-02 25 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

相关问题