Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Cocos2D-Android-1之源碼詳解:16.ParticleTest

Cocos2D-Android-1之源碼詳解:16.ParticleTest

編輯:關於Android編程

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_images.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";
    }
    }

    static class ParticleDesigner5 extends ParticleDemo {//設計5
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);//有黑背景
    removeChild(background, true);
    background = null;//移除背景圖層精靈

    emitter = CCQuadParticleSystem.particleWithFile("Particles/Comet.plist");//加載文件
    addChild(emitter, 10);
    }
    @Override
    public String title()
    {
    return "PD: Comet";
    }
    }

    static class ParticleDesigner6 extends ParticleDemo {//6
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);//同上
    removeChild(background, true);
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/BurstPipe.plist");
    addChild(emitter, 10);
    }
    @Override
    public String title()
    {
    return "PD: Burst Pipe";
    }
    }

    static class ParticleDesigner7 extends ParticleDemo {//7
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);//黑背景
    removeChild(background, true);
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/BoilingFoam.plist");//文件
    addChild(emitter, 10);
    }
    @Override
    public String title()
    {
    return "PD: Boiling Foam";
    }
    }

    static class ParticleDesigner8 extends ParticleDemo {//8,解釋同理
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);
    removeChild(background, true);
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/Flower.plist");
    addChild(emitter, 10);
    }

    @Override
    public String title() {
    return "PD: Flower";
    }
    }

    static class ParticleDesigner9 extends ParticleDemo {//9,解釋同理
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);
    removeChild(background, true);
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/Spiral.plist");
    addChild(emitter, 10);
    }

    @Override
    public String title()
    {
    return "PD: Blur Spiral";
    }
    }

    static class ParticleDesigner10 extends ParticleDemo {//10,解釋同理
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);
    removeChild(background, true);
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/Galaxy.plist");
    addChild(emitter, 10);
    }

    @Override
    public String title() {
    return "PD: Galaxy";
    }
   
    @Override
    public String subtitle() {
    return "Testing radial & tangential accel";
    }
    }

    static class ParticleDesigner11 extends ParticleDemo {//11,解釋同理
    @Override
    public void onEnter() {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);
    removeChild(background, true);
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/debian.plist");
    addChild(emitter, 10);
    }

    @Override
    public String title() {
    return "PD: Debian";
    }
   
    @Override
    public String subtitle() {
    return "Testing radial & tangential accel";
    }
    }

    static class RadiusMode1 extends ParticleDemo {//12
    @Override
    public void onEnter() {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);//添加背景顏色
    removeChild(background, true);//移除背景圖層
    background = null;

    emitter = new CCQuadParticleSystem(200);//200個粒子的粒子系統
    addChild(emitter, 10);

    emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars_grayscale.png"));
//添加圖案
    // duration
    emitter.setDuration(CCParticleSystem.kCCParticleDurationInfinity);
//永久存活
    // radius mode
    emitter.setEmitterMode(CCParticleSystem.kCCParticleModeRadius);
//半徑模式
    // radius mode: start and end radius in pixels
    emitter.setStartRadius(0);//開始半徑
    emitter.setStartRadiusVar(0);//開始半徑變換速度
    emitter.setEndRadius(160);//結束半徑
    emitter.setEndRadiusVar(0);//結束半徑變換速度

    // radius mode: degrees per second
    emitter.setRotatePerSecond(180);//旋轉180度
    emitter.setRotatePerSecondVar(0);旋轉加速度


    // angle
    emitter.setAngle(90);//設置角度
    emitter.setAngleVar(0);//設置角加速度

    // emitter position
    CGSize size = CCDirector.sharedDirector().winSize();//設置大小
    emitter.setPosition(CGPoint.ccp( size.width/2, size.height/2));//設置位置
    emitter.setPosVar(CGPoint.zero());//點變化速度

    // life of particles
    emitter.setLife(5);//設置生命周期
    emitter.setLifeVar(0);

    // 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(32);//開始大小
    emitter.setStartSizeVar(0);//開始變換速度
    emitter.setEndSize(CCParticleSystem.kCCParticleStartSizeEqualToEndSize);//結束大小

    // emits per second
    emitter.setEmissionRate(emitter.getTotalParticles()/emitter.getLife());//出現的頻率

    // additive
    emitter.setBlendAdditive(false);//不在中心
    }
   
    @Override
    public String title() {
    return "Radius Mode: Spiral";
    }
    }

    static class RadiusMode2 extends ParticleDemo {//2
    @Override
    public void onEnter() {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);//黑色背景
    removeChild(background, true);//移除背景圖層
    background = null;

    emitter = new CCQuadParticleSystem(200);//創建一個200個精靈的圖層
    addChild(emitter, 10);//添加為子類

    emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("stars_grayscale.png"));
//設置紋理
    // duration
    emitter.setDuration(CCParticleSystem.kCCParticleDurationInfinity);
//永久存在的效果
    // radius mode
    emitter.setEmitterMode(CCParticleSystem.kCCParticleModeRadius);
//半徑模式
    // radius mode: 100 pixels from center
    emitter.setStartRadius(100);//開始半徑
    emitter.setStartRadiusVar(0);//開始半徑加速度
    emitter.setEndRadius(CCParticleSystem.kCCParticleStartRadiusEqualToEndRadius);//結束和開始的半徑相等,其參數值為-1
    emitter.setEndRadiusVar(0);// not used when start == end//結束的加速度還是0

    // radius mode: degrees per second
    // 45 * 4 seconds of life = 180 degrees
    emitter.setRotatePerSecond(45);//每秒45度
    emitter.setRotatePerSecondVar(0);//加速度

    // angle
    emitter.setAngle(90);//設置角度90
    emitter.setAngleVar(0);//設置角加速度0

    // emitter position
    CGSize size = CCDirector.sharedDirector().winSize();//設置大小
    emitter.setPosition(CGPoint.ccp( size.width/2, size.height/2));
    emitter.setPosVar(CGPoint.zero());

    // life of particles
    emitter.setLife(4);//設置生命周期
    emitter.setLifeVar(0);//設置生命加速度

    // 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(32);//設置開始大小
    emitter.setStartSizeVar(0);//設置大小加速度
    emitter.setEndSize(CCParticleSystem.kCCParticleStartSizeEqualToEndSize);//設置結束加速度和開始加速度一樣

    // emits per second
    emitter.setEmissionRate(emitter.getTotalParticles()/emitter.getLife());//設置迸射粒子速度

    // additive
    emitter.setBlendAdditive(false);//中心

    }
    @Override
    public String title()
    {
    return "Radius Mode: Semi Circle";
    }
    }

    static class Issue704 extends ParticleDemo {//
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);//黑色
    removeChild(background, true);//移除背景
    background = null;

    emitter = new CCQuadParticleSystem(100);//生成一個有100個粒子的粒子系統
    addChild(emitter, 10);
    emitter.setDuration(CCParticleSystem.kCCParticleDurationInfinity);//永久生存

    // radius mode
    emitter.setEmitterMode(CCParticleSystem.kCCParticleModeRadius);//半徑模式

    // radius mode: 50 pixels from center
    emitter.setStartRadius(50);//開始半徑50
    emitter.setStartRadiusVar(0);//開始半徑加速度0
    emitter.setEndRadius(CCParticleSystem.kCCParticleStartRadiusEqualToEndRadius);//與開始相等
    emitter.setEndRadiusVar(0);// not used when start == end//也相等

    // radius mode: degrees per second
    // 45 * 4 seconds of life = 180 degrees
    emitter.setRotatePerSecond(0);//設置每秒轉角度0
    emitter.setRotatePerSecondVar(0);//每秒轉加速度0

    // angle
    emitter.setAngle(90);//設置角度90
    emitter.setAngleVar(0);//設置角加速度0

    // emitter position
    CGSize size = CCDirector.sharedDirector().winSize();//得到屏幕大小
    emitter.setPosition(CGPoint.ccp(size.width/2, size.height/2));//位於屏幕中間
    emitter.setPosVar(CGPoint.zero());//設置點變化速度

    // life of particles
    emitter.setLife (5);//生命周期
    emitter.setLifeVar(0);//生命周期速度

    // 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(16);//設置開始大小
    emitter.setStartSizeVar(0);//開始大小變化率
    emitter.setEndSize(CCParticleSystem.kCCParticleStartSizeEqualToEndSize);//結束大小和開始大小一樣

    // emits per second
    emitter.setEmissionRate( emitter.getTotalParticles()/emitter.getLife());//每秒的迸發速度

    // additive
    emitter.setBlendAdditive(false);//不在中心

    CCIntervalAction rot = CCRotateBy.action(16, 360);//一個相對旋轉動作,轉換為瞬間動作
    emitter.runAction(CCRepeatForever.action(rot));//讓粒子執行
    }

    @Override
    public String title() {
    return "Issue 704. Free + Rot";
    }
   
    @Override
    public String subtitle() {
    return "Emitted particles should not rotate";
    }
    }

    static class Issue872 extends ParticleDemo {//872
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);
    removeChild(background, true);//移除背景
    background = null;

    emitter = CCQuadParticleSystem.particleWithFile("Particles/Upsidedown.plist");//得到粒子效果從一個文件
    addChild(emitter, 10);
    }

    @Override
    public String title() {
    return "Issue 872. UpsideDown";
    }
   
    @Override
    public String subtitle()
    {
    return "Particles should NOT be Upside Down. M should appear, not W.";
    }
    }

    static class Issue870 extends ParticleDemo {//870
    int index;
   
    @Override
    public void onEnter()
    {
    super.onEnter();

    setColor(ccColor3B.ccBLACK);
    removeChild(background, true);//同理
    background = null;

    CCQuadParticleSystem system = (CCQuadParticleSystem) CCQuadParticleSystem.particleWithFile("Particles/SpinningPeas.plist");//得到一個粒子效果從文件強制轉換為粒子系統

    system.setTexture(CCTextureCache.sharedTextureCache().addImage("particles.png"), CGRect.make(0,0,32,32));//添加紋理
    addChild(system, 10);//同理

    emitter = system;

    index = 0;

    schedule("updateQuads", 2);//更新
    }

    public void updateQuads(float dt) {//被更新的那個方法
    index = (index + 1) % 4;//0-3循環
    CGRect rect = CGRect.make(index*32, 0,32,32);//制作一個區域

    CCQuadParticleSystem system = (CCQuadParticleSystem) emitter;
    system.setTexture(emitter.getTexture(), rect);//生成一個粒子系統
    }

    @Override
    public String title()
    {
    return "Issue 870. SubRect";
    }
   
    @Override
    public String subtitle()
    {
    return "Every 2 seconds the particle should change";
    }
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved