cocos2dのパーティクルシステムを使うと、ゲーム画面にリアルなエフェクトを表示できます。
標準エフェクト:花火
デフォルトで用意されているエフェクトを追加してみます。ゲームに使うには少し物足りない感じがします。
CCParticleSystem * particleSystem = [CCParticleFireworks node];
[self addChild:particleSystem];
[self addChild:particleSystem];
設定ファイルを読み込んで表示
PARTICLE DESIGNERで作成したパーティクルを表示してみます。PARTICLE DESIGNERには数百種類のエフェクトが用意されており、そのまま書きだして使ったり、パラメータを調整して使ったりできます。有料(7.99USD)ですが、お金を払わなくてもplistの書き出し以外は利用できるようです。
CCParticleSystem * ps = [CCParticleSystemQuad particleWithFile:@"exploding_ring.plist"];;
タップした場所を中心にエフェクトを表示
exploding_ring.plistのsourcePositionxとsourcePositionyは0にしておき、タップした場所の座標をsourcePositionに設定します。
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(void)registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint location = [self convertTouchToNodeSpace:touch];
CCParticleSystem * ps = [CCParticleSystemQuad particleWithFile:@"exploding_ring.plist"];;
ps.sourcePosition = location;
ps.autoRemoveOnFinish = YES;
[self addChild:ps];
}
-(id) init
{
if( (self=[super init])) {
self.isTouchEnabled = YES;
}
return self;
}
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(void)registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint location = [self convertTouchToNodeSpace:touch];
CCParticleSystem * ps = [CCParticleSystemQuad particleWithFile:@"exploding_ring.plist"];;
ps.sourcePosition = location;
ps.autoRemoveOnFinish = YES;
[self addChild:ps];
}
-(id) init
{
if( (self=[super init])) {
self.isTouchEnabled = YES;
}
return self;
}
ダウンロード
ダウンロード: Particle
Xcodeのプロジェクトファイルです。