Cocos2d - 如何使单个粒子跟随层,而不是发射器?

时间:2011-09-02 19:20:48

标签: iphone ios cocos2d-iphone

我有一个CCSprite和一个CCParticleSystemQuad,它们都是CCLayer的子代。在我的更新方法中,我将发射器的位置设置为精灵的位置,因此它会跟踪精灵。烟雾就像你期望的那样落在精灵的底部,即使你移动精灵,烟雾似乎也是背景层的一部分。

如果我匹配他们的轮换,问题就来了。现在,例如,如果我的精灵来回摇摆,那烟雾就会以弧形摆动并出现在雪碧身上。

如何让烟雾沿着父线沿着直线继续,而不是用精灵旋转?我移动时它们不会用精灵转换,那么为什么它们随之旋转?

编辑:添加代码......

- (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.

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

CCSprite的anchorPoint是{0.5f,0.5f),而发射器直接从CCNode下降,CCNode的anchorPoint为{0.0f,0.0f}。尝试设置发射器的anchorPoint以匹配CCSprite。