我有一个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.
最佳答案
我在其他网站上找到了答案-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
}
关于iphone - Cocos2d-如何使单个粒子跟随层,而不是发射器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7288298/