【问题标题】:Cocos2d - how to make individual particles follow the layer, not the emitter?Cocos2d - 如何使单个粒子跟随层,而不是发射器?
【发布时间】:2013-05-25 08:59:35
【问题描述】:

我有一个 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.

【问题讨论】:

    标签: iphone ios cocos2d-iphone


    【解决方案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
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • Out - 感谢您的建议,但它仍然与精灵一起来回摆动。我真的很困惑为什么翻译和旋转如此不同。我看到的唯一区别是我通过直接更改其位置并通过动作旋转来移动精灵。
      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多