You can also add particle effects to the game with a few lines of code.
To create the particle effect, we need to import the image file that will be used by the effect first of all. In this case, it is the fire.png
file. Import it into the project.
To add the particle effect to the project, we will add the following to the bottom of the init
function right after the MainScene.m
file:
CCParticleFire* fire = [[CCParticleFire alloc]initWithTotalParticles:20]; fire.position = CGPointMake(winSize.width/2, winSize.height/2); [self addChild:fire];
Here, we created a fire effect, so we used CCParticleFire
to create the particle system. While initializing the particle system, we need to pass in the number of particles we will initialize with.
Next, we will position the particle system in the middle of the screen, and finally, we will add the particle system to the scene at the end.
Build and run to see the particles added to the scene, as follows:
The fire particle system is just an example. There are more particle types that are available. The following screenshot shows the different types of particle that are available to you:
You can also modify different properties of the particle effect. The following is the code that shows the different parameters that can be modified:
CCParticleSystem *emitter = [[CCParticleSystem alloc] initWithTotalParticles:20]; emitter.texture = [CCTexture textureWithFile:@"hero.png"]; // duration emitter.duration = CCParticleSystemDurationInfinity; // Gravity Mode: gravity emitter.gravity = CGPointZero; // Set "Gravity" mode (default one) emitter.emitterMode = CCParticleSystemModeGravity; // Gravity Mode: speed of particles emitter.speed = 160; emitter.speedVar = 20; // Gravity Mode: radial emitter.radialAccel = -120; emitter.radialAccelVar = 0; // Gravity Mode: tangential emitter.tangentialAccel = 30; emitter.tangentialAccelVar = 0; // angle emitter.angle = 90; emitter.angleVar = 360; // emitter position emitter.position = CGPointMake(winSize.width/2, winSize.height/2); emitter.posVar = CGPointZero; // life of particles emitter.life = 4; emitter.lifeVar = 1; // spin of particles emitter.startSpin = 0; emitter.startSpinVar = 0; emitter.endSpin = 0; emitter.endSpinVar = 0; // color of particles emitter.startColor = [CCColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1]; emitter.startColorVar = [CCColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1]; emitter.endColor = [CCColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2]; emitter.endColorVar = [CCColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2]; // size, in pixels emitter.startSize = 80.0f; emitter.startSizeVar = 40.0f; emitter.endSize = CCParticleSystemStartSizeEqualToEndSize; // emits per second emitter.emissionRate = emitter.totalParticles/emitter.life; // additive emitter.blendAdditive = YES; [self addChild:emitter];
3.144.30.178