Cocos2D-Android-1之源码详解:16.ParticleTest
package org.cocos2d.tests;
目前成都创新互联公司已为超过千家的企业提供了网站建设、域名、网站空间、网站运营、企业网站设计、忻府网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
import org.cocos2d.actions.UpdateCallback;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.interval.CCIntervalAction;
import org.cocos2d.actions.interval.CCMoveBy;
import org.cocos2d.actions.interval.CCRotateBy;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemFont;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.menus.CCMenuItemToggle;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCLabelAtlas;
import org.cocos2d.nodes.CCParallaxNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.opengl.CCTextureAtlas;
import org.cocos2d.particlesystem.CCParticleExplosion;
import org.cocos2d.particlesystem.CCParticleFire;
import org.cocos2d.particlesystem.CCParticleFireworks;
import org.cocos2d.particlesystem.CCParticleFlower;
import org.cocos2d.particlesystem.CCParticleGalaxy;
import org.cocos2d.particlesystem.CCParticleMeteor;
import org.cocos2d.particlesystem.CCParticleRain;
import org.cocos2d.particlesystem.CCParticleSmoke;
import org.cocos2d.particlesystem.CCParticleSnow;
import org.cocos2d.particlesystem.CCParticleSpiral;
import org.cocos2d.particlesystem.CCParticleSun;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.particlesystem.CCPointParticleSystem;
import org.cocos2d.particlesystem.CCQuadParticleSystem;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor3B;
import org.cocos2d.types.ccColor4B;
import org.cocos2d.types.ccColor4F;
import org.cocos2d.utils.javolution.MathLib;
import org.cocos2d.utils.javolution.TextBuilder;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class ParticleTest extends Activity {
// private static final String LOG_TAG = AtlasSpriteTest.class.getSimpleName();
private CCGLSurfaceView mGLSurfaceView;//建立view
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//没标题..
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//不黑
mGLSurfaceView = new CCGLSurfaceView(this);//建立新的view
setContentView(mGLSurfaceView);//设置view
// attach the OpenGL view to a window
CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把所有的图像附加到view上
// set landscape mode
CCDirector.sharedDirector().setLandscape(false);//横屏
// show FPS
CCDirector.sharedDirector().setDisplayFPS(true);//fps
// frames per second
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);//每1/60秒运行一次
CCScene scene = CCScene.node();//生成场景节点
scene.addChild(nextAction());//场景布置
// Make the Scene active
CCDirector.sharedDirector().runWithScene(scene);//执行
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onPause() {
super.onPause();
CCDirector.sharedDirector().onPause();
}
@Override
public void onResume() {
super.onResume();
CCDirector.sharedDirector().onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
CCDirector.sharedDirector().end();
//CCTextureCache.sharedTextureCache().removeAllTextures();
}
static int sceneIdx = -1;
static Class> transitions[] = {//类集合
DemoFlower.class,
DemoGalaxy.class,
DemoFirework.class,
DemoSpiral.class,
DemoSun.class,
DemoMeteor.class,
DemoFire.class,
DemoSmoke.class,
DemoExplosion.class,
DemoSnow.class,
DemoRain.class,
DemoBigFlower.class,
DemoRotFlower.class,
DemoModernArt.class,
DemoRing.class,
ParallaxParticle.class,
/* we can't work on plist files now
ParticleDesigner1.class,
ParticleDesigner2.class,
ParticleDesigner3.class,
ParticleDesigner4.class,
ParticleDesigner5.class,
ParticleDesigner6.class,
ParticleDesigner7.class,
ParticleDesigner8.class,
ParticleDesigner9.class,
ParticleDesigner10.class,
ParticleDesigner11.class, */
RadiusMode1.class,
RadiusMode2.class,
// Issue704.class,
// Issue872.class,
// Issue870.class,
};
static CCLayer nextAction() {//动作下一步
sceneIdx++;
sceneIdx = sceneIdx % transitions.length;
return restartAction();
}
static CCLayer backAction() {//返回动作
sceneIdx--;
if (sceneIdx < 0)
sceneIdx += transitions.length;
return restartAction();
}
static CCLayer restartAction() {//重置
try {
Class> c = transitions[sceneIdx];
return (CCLayer) c.newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
static abstract class ParticleDemo extends CCColorLayer implements UpdateCallback {
CCTextureAtlas atlas;//文字图集
static final int kTagLabelAtlas = 1;
CCParticleSystem emitter;//创建离子系统
CCSprite background;//创建背景精灵
public ParticleDemo() {
super(new ccColor4B(127,127,127,255));//颜色背景
this.setIsTouchEnabled(true);//可以点击
CGSize s = CCDirector.sharedDirector().winSize();//得到大小
CCLabel label = CCLabel.makeLabel(title(), "DroidSans", 18);//标记
label.setPosition(CGPoint.make(s.width / 2, s.height - 50));//设置位置
addChild(label, 100);//添加子类
String subtitle = subtitle();//得到字符
if( subtitle != null) {
CCLabel l = CCLabel.makeLabel(subtitle, "DroidSerif", 16);//创建标签
addChild(l, 100);//添加子类
l.setPosition(CGPoint.ccp(s.width/2, s.height-80));//设置位置
}
//创建下列3个按钮
CCMenuItemImage item1 = CCMenuItemImage.item("b1.png", "b2.png", this, "backCallback");
CCMenuItemImage item2 = CCMenuItemImage.item("r1.png", "r2.png", this, "restartCallback");
CCMenuItemImage item3 = CCMenuItemImage.item("f1.png", "f2.png", this, "nextCallback");
//添加一个标签按钮,这个标签每次点击会切换字符串
CCMenuItemToggle item4 = CCMenuItemToggle.item(this,"toggleCallback",
CCMenuItemFont.item("Free Movement"),//散开的效果
CCMenuItemFont.item("Grouped Movement"));//圆效果
//把按钮添加进去
CCMenu menu = CCMenu.menu(item1, item2, item3, item4);//添加进菜单
menu.setPosition(CGPoint.make(0, 0));//设置位置
item1.setPosition(CGPoint.make(s.width / 2 - 100, 30));
item2.setPosition(CGPoint.make(s.width / 2, 30));
item3.setPosition(CGPoint.make(s.width / 2 + 100, 30));
item4.setPosition(CGPoint.ccp(0, 100));
item4.setAnchorPoint(CGPoint.ccp(0,0));//设置锚点
addChild(menu, 100);//菜单顶置
CCLabelAtlas labelAtlas = CCLabelAtlas.label("0000", "fps_p_w_picpaths.png", 16, 24, '.');//标签图
addChild(labelAtlas, 100, kTagLabelAtlas);//添加子类
labelAtlas.setPosition(CGPoint.make(s.width-66, 50));//设置顶点
// moving background
background = CCSprite.sprite("background3.png");//创建精灵
addChild(background, 5);//添加子类
background.setPosition(CGPoint.make(s.width/2, s.height-180));
//设置位置
CCIntervalAction move = CCMoveBy.action(4, CGPoint.ccp(300, 0));//移动
CCIntervalAction move_back = move.reverse();//反移动
CCIntervalAction seq = CCSequence.actions(move, move_back);//创建连
background.runAction(CCRepeatForever.action(seq));//执行
this.scheduleUpdate();//设置0优先等级的更新
}
@Override
public boolean ccTouchesBegan(MotionEvent e){//点击事件开始
// claim the touch
return true;
}
@Override
public boolean ccTouchesMoved(MotionEvent e) {//点击移动
return true;
}
@Override
public boolean ccTouchesEnded(MotionEvent e) {//点击结束
CGPoint location = CGPoint.ccp(e.getX(), e.getY());//定义点
CGPoint convertedLocation = CCDirector.sharedDirector().convertToGL(location);
//转换坐标
CGPoint pos = CGPoint.zero();//得到原点0.0
if( background != null)//如果背景层不为空
pos = background.convertToWorldSpace(0, 0);//转换为世界坐标
emitter.setPosition(CGPoint.ccpSub(convertedLocation, pos));//做差
return true;
}
private TextBuilder particleCountString = new TextBuilder();//字符串
// String.format("%4d", emitter.getParticleCount())//这是要格式化成4位
private TextBuilder getParticleCountString(int c) {//得到字符串的方法
int len = MathLib.digitLength(c);//得到c的长度
int zeros = 4 - len;//用4-c
while (zeros-- > 0) {//一直循环-1,只要大于0就继续做
particleCountString.append('0');//补0
}
particleCountString.append(c);//把c添加进去
return particleCountString;//返回字符
}
public void update(float dt) {//重写每帧的执行
CCLabelAtlas atlas = (CCLabelAtlas) getChildByTag(kTagLabelAtlas);//得到标签
particleCountString.reset();//清楚掉字符
// String str = String.format("%4d", emitter.getParticleCount());
atlas.setString(getParticleCountString(emitter.getParticleCount()));//设置标签字符
}
public void toggleCallback(Object sender) {//由刚才那个点击得到这个事件。每次看看自己的参数设置,如果是grop的就设为free的,否则就射为group的
if( emitter.getPositionType() == CCParticleSystem.kCCPositionTypeGrouped )
emitter.setPositionType(CCParticleSystem.kCCPositionTypeFree);
else
emitter.setPositionType(CCParticleSystem.kCCPositionTypeGrouped);
}
public void restartCallback(Object sender) {//重置
// Scene s = Scene.node();
// s.addChild(restartAction());
// Director.sharedDirector().replaceScene(s);
emitter.resetSystem();
}
public void setEmitterPosition() {//设置点
if( CGPoint.equalToPoint( emitter.getCenterOfGravity(), CGPoint.zero() ) ) //如果相等
emitter.setPosition(CGPoint.make(200, 70));//设定顶点
}
public void nextCallback(Object sender) {//2个切换事件
CCScene s = CCScene.node();
s.addChild(nextAction());
CCDirector.sharedDirector().replaceScene(s);
}
public void backCallback(Object sender) {
CCScene s = CCScene.node();
s.addChild(backAction());
CCDirector.sharedDirector().replaceScene(s);
}
public String title() {//得到字符串
return "No title";
}
public String subtitle() {
return "Tap the screen";
}
}
static class DemoFire extends ParticleDemo {//火
public void onEnter() {
super.onEnter();
emitter = CCParticleFire.node();
background.addChild(emitter, 10);//添加系统
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("fire.png"));
//在缓存中添加图片火,并把这作为图案,作为离子的图案
CGPoint p = emitter.getPosition();//得到顶点
emitter.setPosition(CGPoint.ccp(p.x, 100));//设置顶点让y=100
setEmitterPosition();//设置位置
}
public String title() {
return "ParticleFire";
}
}
static class DemoExplosion extends ParticleDemo {//爆炸
static final int kTagLabelAtlas = 1;
static final int kTagEmitter = 2;
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleExplosion.node();//新建爆炸节点
addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars_grayscale.png"));//设置图案
emitter.setAutoRemoveOnFinish(true);//完成后自动消失
setEmitterPosition();
}
@Override
public String title() {
return "ParticleExplosion";
}
}
static class DemoFirework extends ParticleDemo {//×××,也是一种爆炸
static final int kTagLabelAtlas = 1;
static final int kTagEmitter = 2;
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleFireworks.node();
background.addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars.png"));//给个图片
emitter.setBlendAdditive(true);//设置加亮模式
setEmitterPosition();//设置点
}
@Override
public String title() {
return "ParticleFireworks";
}
}
static class DemoSun extends ParticleDemo {//太阳效果
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleSun.node();//生成一个太阳节点
background.addChild(emitter, 10);//添加子类
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("fire.png"));//设置图像
setEmitterPosition();
}
@Override
public String title()
{
return "ParticleSun";
}
}
static class DemoGalaxy extends ParticleDemo {//银河系
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleGalaxy.node();//生成银河系节点
background.addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("fire.png"));//火图片
setEmitterPosition();
}
@Override
public String title() {
return "ParticleGalaxy";
}
}
static class DemoFlower extends ParticleDemo {//花
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleFlower.node();//花的节点
background.addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars_grayscale.png"));
//设置基本粒子图片
setEmitterPosition();
}
@Override
public String title() {
return "ParticleFlower";
}
}
static class DemoBigFlower extends ParticleDemo {//大花
@Override
public void onEnter() {
super.onEnter();
emitter = new CCQuadParticleSystem(50);//粒子系统50个粒子
background.addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars_grayscale.png"));
//设置图片
// duration
emitter.setDuration(CCParticleSystem.kCCParticleDurationInfinity);//粒子发射器设置永远
// Gravity Mode: gravity
emitter.setGravity(CGPoint.zero());//设置重力点在0.0
// Set "Gravity" mode (default one)
emitter.setEmitterMode(CCParticleSystem.kCCParticleModeGravity);//设置为重力模式,还有一种叫半径模式
// Gravity Mode: speed of particles
emitter.setSpeed(160);//速度
emitter.setSpeedVar(20);//加速度
// Gravity Mode: radial
emitter.setRadialAccel(-120);//设置切向速度
emitter.setRadialAccelVar(0);//切向加速度
// Gravity Mode: tagential
emitter.setTangentialAccel(30);//向心速度
emitter.setTangentialAccelVar(0);//向心加速度
// angle
emitter.setAngle(90);//设置角度
emitter.setAngleVar(360);//角度加速度
// emitter position
emitter.setPosition(CGPoint.ccp(160,240));//设置初始位置
emitter.setPosVar(CGPoint.zero());//设置位置变化率
// life of particles
emitter.setLife(4);//存活时期
emitter.setLifeVar(1);//时间变化率
// spin of particles
emitter.setStartSpin(0);//开始自转
emitter.setStartSpinVar(0);//开始转速度
emitter.setEndSpin(0);//结束自转
emitter.setEndSpinVar(0);//结束转速度
// color of particles
ccColor4F startColor = new ccColor4F(0.5f, 0.5f, 0.5f, 1.0f);
emitter.setStartColor(startColor);//设置初始颜色
ccColor4F startColorVar = new ccColor4F(0.5f, 0.5f, 0.5f, 1.0f);
emitter.setStartColorVar(startColorVar);//设置颜色变化率
ccColor4F endColor = new ccColor4F(0.1f, 0.1f, 0.1f, 0.2f);
emitter.setEndColor(endColor);//设置结束颜色
ccColor4F endColorVar = new ccColor4F(0.1f, 0.1f, 0.1f, 0.2f);
emitter.setEndColorVar(endColorVar);//设置结束颜色变化率
// size, in pixels
emitter.setStartSize(80.0f);//设置初始离子大小
emitter.setStartSizeVar(40.0f);//设置初始离子变化率
emitter.setEndSize(CCParticleSystem.kCCParticleStartSizeEqualToEndSize);//等同于初始大小
// emits per second
emitter.setEmissionRate( emitter.getTotalParticles()/emitter.getLife());//设置每秒产生离子率
// additive
emitter.setBlendAdditive(true);//设置在屏幕中间
setEmitterPosition();
}
@Override
public String title() {
return "Big Particles";
}
}
static class DemoRotFlower extends ParticleDemo {//花
@Override
public void onEnter() {
super.onEnter();
emitter = new CCQuadParticleSystem(300);//300个粒子的自定义粒子系统
background.addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars2_grayscale.png"));
//设置纹理
// duration
emitter.setDuration(CCParticleSystem.kCCParticleDurationInfinity);//永久存活
// Set "Gravity" mode (default one)
emitter.setEmitterMode(CCParticleSystem.kCCParticleModeGravity);//重力模式
// Gravity mode: gravity
emitter.setGravity(CGPoint.zero());//设置重力点
// Gravity mode: speed of particles
emitter.setSpeed(160);//设置粒子速度
emitter.setSpeedVar(20);//设置加速度
// Gravity mode: radial
emitter.setRadialAccel(-120);//径向速度
emitter.setRadialAccelVar(0);//径向加速度
// Gravity mode: tagential
emitter.setTangentialAccel(30);//切向速度
emitter.setTangentialAccelVar(0);//切向加速度
// emitter position
emitter.setPosition(CGPoint.ccp(160,240));//出发点
emitter.setPosVar(CGPoint.zero());//点变化速度
// angle
emitter.setAngle(90);//角度
emitter.setAngleVar(360);//角加速度
// life of particles
emitter.setLife(3);//生命周期
emitter.setLifeVar(1);//衰变速度
// spin of particles
emitter.setStartSpin(0);//粒子自己开始角度
emitter.setStartSpinVar(0);//粒子自己旋转的速度,0就是一开始速度为0
emitter.setEndSpin(0);//粒子自己结束时的角度
emitter.setEndSpinVar(2000);//单个粒子自己旋转的速度,2000就转的非常快了
// color of particles
ccColor4F startColor = new ccColor4F(0.5f, 0.5f, 0.5f, 1.0f);//开始的颜色
emitter.setStartColor(startColor);//设置开始的颜色
ccColor4F startColorVar = new ccColor4F(0.5f, 0.5f, 0.5f, 1.0f);//开始颜色变化速度
emitter.setStartColorVar(startColorVar);
ccColor4F endColor = new ccColor4F(0.1f, 0.1f, 0.1f, 0.2f);//结束颜色
emitter.setEndColor(endColor);
ccColor4F endColorVar = new ccColor4F(0.1f, 0.1f, 0.1f, 0.2f);//结束时的速度
emitter.setEndColorVar(endColorVar);
// size, in pixels
emitter.setStartSize(30.0f);//开始大小
emitter.setStartSizeVar(00.0f);//开始速度
emitter.setEndSize(CCParticleSystem.kCCParticleStartSizeEqualToEndSize);//结束大小设置的和一开始一样
// emits per second
emitter.setEmissionRate(emitter.getTotalParticles()/emitter.getLife());//每秒产生粒子的效率,得到全部产生的粒子/生命周期=粒子的产生效率
// additive
emitter.setBlendAdditive(false);//设置在屏幕中间
setEmitterPosition();
}
@Override
public String title() {
return "Spinning Particles";
}
}
static class DemoMeteor extends ParticleDemo {
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleMeteor.node();//粒子流星
background.addChild(emitter, 10);//背景添加粒子流星
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("fire.png"));
//给粒子添加图案
setEmitterPosition();
}
@Override
public String title() {
return "ParticleMeteor";
}
}
static class DemoSpiral extends ParticleDemo {
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleSpiral.node();//颗粒螺旋
background.addChild(emitter, 10);//添加螺旋
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("fire.png"));
//设置画面
setEmitterPosition();
}
@Override
public String title() {
return "ParticleSpiral";
}
}
static class DemoSmoke extends ParticleDemo {
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleSmoke.node();//粒子烟雾
background.addChild(emitter, 10);
CGPoint p = emitter.getPosition();
emitter.setPosition(CGPoint.ccp( p.x, 100));//设置位置
setEmitterPosition();
}
@Override
public String title() {
return "ParticleSmoke";
}
}
static class DemoSnow extends ParticleDemo {
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleSnow.node();//粒子下雪
background.addChild(emitter, 10);
CGPoint p = emitter.getPosition();
emitter.setPosition(CGPoint.ccp( p.x, p.y-110));
emitter.setLife(3);//生命周期3
emitter.setLifeVar(1);//生命速度1
// gravity
emitter.setGravity(CGPoint.ccp(0,-10));
// speed of particles
emitter.setSpeed(130);//设置速度
emitter.setSpeedVar(30);//加速度
ccColor4F startColor = emitter.getStartColor();//得到开始颜色
startColor.r = 0.9f;
startColor.g = 0.9f;
startColor.b = 0.9f;
emitter.setStartColor(startColor);//设置开始颜色,主要为了改透明度
ccColor4F startColorVar = emitter.getStartColorVar();//开始变换速度
startColorVar.b = 0.1f;
emitter.setStartColorVar(startColorVar);//只改变蓝色的变化速度
emitter.setEmissionRate(emitter.getTotalParticles()/emitter.getLife());//粒子产生速度
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("snow.png"));
//设置纹理
setEmitterPosition();
}
@Override
public String title() {
return "ParticleSnow";
}
}
static class DemoRain extends ParticleDemo {//下雨
@Override
public void onEnter(){
super.onEnter();
emitter = CCParticleRain.node();//下雨粒子效果
background.addChild(emitter, 10);
CGPoint p = emitter.getPosition();//设置位置
emitter.setPosition(CGPoint.ccp( p.x, p.y-100));
emitter.setLife(4);//设置生命周期
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("fire.png"));
//设置纹理
setEmitterPosition();//设置位置
}
@Override
public String title() {
return "ParticleRain";
}
}
static class DemoModernArt extends ParticleDemo {
@Override
public void onEnter()
{
super.onEnter();
emitter = new CCPointParticleSystem(1000);//粒子效果1000个
background.addChild(emitter, 10);//添加子类
CGSize s = CCDirector.sharedDirector().winSize();//得到大小
// duration
emitter.setDuration(CCParticleSystem.kCCParticleDurationInfinity);//生命周期永久
// Gravity mode
emitter.setEmitterMode(CCParticleSystem.kCCParticleModeGravity);//重力模式
// Gravity mode: gravity
emitter.setGravity(CGPoint.ccp(0,0));//重心
// Gravity mode: radial
emitter.setRadialAccel(70);//以下几个方法不做赘述
emitter.setRadialAccelVar(10);
// Gravity mode: tagential
emitter.setTangentialAccel(80);
emitter.setTangentialAccelVar(0);
// Gravity mode: speed of particles
emitter.setSpeed(50);
emitter.setSpeedVar(10);
// angle
emitter.setAngle(0);
emitter.setAngleVar(360);
// emitter position
emitter.setPosition(CGPoint.ccp( s.width/2, s.height/2));
emitter.setPosVar(CGPoint.zero());
// life of particles
emitter.setLife(2.0f);
emitter.setLifeVar(0.3f);
// emits per frame
emitter.setEmissionRate(emitter.getTotalParticles()/emitter.getLife());
// color of particles
ccColor4F startColor = new ccColor4F(0.5f, 0.5f, 0.5f, 1.0f);
emitter.setStartColor(startColor);
ccColor4F startColorVar = new ccColor4F(0.5f, 0.5f, 0.5f, 1.0f);
emitter.setStartColorVar(startColorVar);
ccColor4F endColor = new ccColor4F(0.1f, 0.1f, 0.1f, 0.2f);
emitter.setEndColor(endColor);
ccColor4F endColorVar = new ccColor4F(0.1f, 0.1f, 0.1f, 0.2f);
emitter.setEndColorVar(endColorVar);
// size, in pixels
emitter.setStartSize(1.0f);
emitter.setStartSizeVar(1.0f);
emitter.setEndSize(32.0f);
emitter.setEndSizeVar(8.0f);
// texture
// emitter.texture = [[TextureCache sharedTextureCache] addImage:@"fire-grayscale.png"];
// additive
emitter.setBlendAdditive(false);//不在中心
setEmitterPosition();
}
@Override
public String title()
{
return "Varying size";
}
}
static class DemoRing extends ParticleDemo {
@Override
public void onEnter() {
super.onEnter();
emitter = CCParticleFlower.node(500);
background.addChild(emitter, 10);
emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars_grayscale.png"));//设置纹理
emitter.setLifeVar(0);
emitter.setLife(10);
emitter.setSpeed(100);
emitter.setSpeedVar(0);
emitter.setEmissionRate(10000);//设置发射率
setEmitterPosition();
}
@Override
public String title() {
return "Ring Demo";
}
}
static class ParallaxParticle extends ParticleDemo {//视差粒子
@Override
public void onEnter() {
super.onEnter();
background.getParent().removeChild(background, true);//移除背景
background = null;
CCParallaxNode p = CCParallaxNode.node();//一个基础的视差场景
addChild(p, 5);
CCSprite p1 = CCSprite.sprite("background3.png");//得到背景精灵1
background = p1;
CCSprite p2 = CCSprite.sprite("background3.png");//背景精灵2
p.addChild(p1, 1, 0.5f, 1f, 0f, 250f);//把背景精灵添加进来
p.addChild(p2, 2, 1.5f, 1f, 0f, 50f);//添加背景精灵
emitter = CCParticleFlower.node(500);//粒子花
p1.addChild(emitter, 10);//把粒子花添加进来
emitter.setPosition(CGPoint.ccp(250,200));
CCParticleSystem par = CCParticleSun.node(250);//太阳
p2.addChild(par, 10);//添加精灵
par = null;//添加完把原字段置空,因为那个引用还在p2子类里
CCMoveBy move = CCMoveBy.action(4, CGPoint.ccp(300,0));//移动
CCMoveBy move_back = move.reverse();//返回
CCSequence seq = CCSequence.actions(move, move_back);//移动和返回
p.runAction(CCRepeatForever.action(seq));//永久动作
}
@Override
public String title() {
return "Parallax + Particles";
}
}
static class ParticleDesigner1 extends ParticleDemo {//粒子设计
@Override
public void onEnter() {
super.onEnter();
setColor(ccColor3B.ccBLACK);
removeChild(background, true);//移除场景
background = null;
emitter = CCQuadParticleSystem.particleWithFile("Particles/SpookyPeas.plist");//文件粒子
addChild(emitter, 10);//添加粒子层
}
@Override
public String title() {
return "PD: Spooky Peas";
}
}
static class ParticleDesigner2 extends ParticleDemo {//设计2
@Override
public void onEnter() {
super.onEnter();
setColor(ccColor3B.ccBLACK);//设置背景颜色
removeChild(background, true);//移除背景精灵(和上面颜色无关)
background = null;//置空,等待回收
emitter = CCQuadParticleSystem.particleWithFile("Particles/SpinningPeas.plist");//从文件得到离子
addChild(emitter, 10);//添加子类效果
// custom spinning
emitter.setStartSpin(0);//同上属性解释
emitter.setStartSpin(360);
emitter.setEndSpin(720);
emitter.setEndSpinVar(360);
}
@Override
public String title() {
return "PD: Spinning Peas";
}
}
static class ParticleDesigner3 extends ParticleDemo {//设计3
@Override
public void onEnter() {
super.onEnter();
setColor(ccColor3B.ccBLACK);
removeChild(background, true);
background = null;
emitter = CCQuadParticleSystem.particleWithFile("Particles/LavaFlow.plist");//只有文件加载的粒子
addChild(emitter, 10);//添加子类
}
@Override
public String title() {
return "PD: Lava Flow";
}
}
static class ParticleDesigner4 extends ParticleDemo {//设计4
@Override
public void onEnter()
{
super.onEnter();
setColor(ccColor3B.ccBLACK);
emitter = CCQuadParticleSystem.particleWithFile("Particles/ExplodingRing.plist");//从文件读取粒子
addChild(emitter, 10);
removeChild(background, true);//比刚才多移除了背景动作
background = null;
}
@Override
public String title()
{
return "PD: Exploding Ring";
分享标题:Cocos2D-Android-1之源码详解:16.ParticleTest
本文链接:http://ybzwz.com/article/gcieih.html