Cocos2d粒子系统二

粒子系统的属性:
  • 粒子的发射速度
  • 重力模式(模式A):
    • 重力
    • 方向
    • 速度 +- 变动
    • 切向加速度 +- 变动
    • 径向加速度 +- 变动
  • 半径模式(模式B):
    • 开始半径 +- 变动
    • 结束半径 +- 变动
    • 旋转 +- 变动
  • 所有模式的公用属性:
    • 生命
    • 开始旋转
    • 结束旋转
    • 开始尺寸
    • 结束尺寸
    • 开始颜色
    • 结束颜色
    • 生命
    • 合成函数
    • 质感
Cocos2d支持由Particle Designer生成的粒子(http://71squared.com/particledesigner(尼玛要$50啊))
“半径模式”在Particle Designer中使用的是固定30hz的频率,但是在cocos2d中频率是可变的,不过结果还是能保持一致的。
cocos2d支持Particle Designer中的所有变量,还多了一点点:
  • 粒子旋转(适用于ParticleSystemQuad)
  • 正切加速度(重力模式)
  • 径向加速度(重力模式)
  • 半径方向(半径模式)(Particle Designer只支持向外和向内的方向)
以上的所有属性在运行时都是可变的,如:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. emitter.radialAccel = 15;  
  2. emitter.startSpin = 0;  

不同的地方:

Cocos2d-x-2.xCocos2d-x-3.x
kCCParticleDefaultCapacity kParticleDefaultCapacity
ParticleSystem::Mode kCCParticleModeGravity ParticleSystem::Mode::GRAVITY
ParticleSystem::Mode kCCParticleModeRadius ParticleSystem::Mode::RADIUS
N/A typedef ParticleSystem::PositionType tPositionType
kCCPositionTypeFree /
kPositionTypeFree
ParticleSystem::PositionType::FREE
kCCPositionTypeRelative /
kPositionTypeRelative
ParticleSystem::PositionType::RELATIVE
kCCPositionTypegRrouped /
kPositionTypegRrouped
ParticleSystem::PositionType::GROUPED
kCCParticleDurationInfinity /
kParticleDurationInfinity
ParticleSystem::DURATION_INFINITY
kCCParticleStartSizeEqualToEndSize /
kParticleStartSizeEqualToEndSize
ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE
kCCParticleStartRadiusEqualToEndRadius /
kParticleStartRadiusEqualToEndRadius
ParticleSystem::START_RADIUS_EQUAL_TO_END_RADIUS
  • 20多个内联函数定义在头文件中用来取代像CC_PROPERTY(varType, varName, funName)和CC_PROPERTY_READONLY(varType, varName, funName)这样的宏
Cocos2d-x-2.xCocos2d-x-3.x
CC_PROPERTY(int, m_nEmitterMode, EmitterMode) Mode _emitterMode; inline Mode getEmitterMode() const { return _emitterMode; };inline void setEmitterMode(Mode mode) { _emitterMode = mode; };
CC_PROPERTY(CCParticleBatchNode*, m_pBatchNode, BatchNode) ParticleBatchNode* _batchNode; virtual ParticleBatchNode* getBatchNode() const;virtual void setBatchNode(ParticleBatchNode* batchNode);
CC_PROPERTY_READONLY(unsigned int, m_uParticleCount, ParticleCount) int _particleCount; inline unsigned int getParticleCount() const { return _particleCount; };
CC_PROPERTY(float, m_fDuration, Duration) float _duration; inline float getDuration() const { return _duration; };inline void setDuration(float duration) { _duration = duration; };
CC_PROPERTY_PASS_BY_REF(CCPoint, m_tSourcePosition, SourcePosition) Point _sourcePosition;inline const Point& getSourcePosition() const { return _sourcePosition; };inline void setSourcePosition(const Point& pos) { _sourcePosition = pos; };
CC_PROPERTY_PASS_BY_REF(CCPoint, m_tPosVar, PosVar) Point _posVar; inline const Point& getPosVar() const { return _posVar; };inline void setPosVar(const Point& pos) { _posVar = pos; };
CC_PROPERTY(float, m_fLife, Life) float _life; inline float getLife() const { return _life; };inline void setLife(float life) { _life = life; };
CC_PROPERTY(float, m_fLifeVar, LifeVar) float _lifeVar; inline float getLifeVar() const { return _lifeVar; };inline void setLifeVar(float lifeVar) { _lifeVar = lifeVar; };
CC_PROPERTY(float, m_fAngle, Angle) float _angle; inline float getAngle() const { return _angle; };inline void setAngle(float angle) { _angle = angle; };
CC_PROPERTY(float, m_fAngleVar, AngleVar) float _angleVar; inline float getAngleVar() const { return _angleVar; };inline void setAngleVar(float angleVar) { _angleVar = angleVar; };
CC_PROPERTY(float, m_fStartSize, StartSize) float _startSize; inline float getStartSize() const { return _startSize; };inline void setStartSize(float startSize) { _startSize = startSize; };
CC_PROPERTY(float, m_fStartSizeVar, StartSizeVar) float _startSizeVar; inline float getStartSizeVar() const { return _startSizeVar; };inline void setStartSizeVar(float sizeVar) { _startSizeVar = sizeVar; };
CC_PROPERTY(float, m_fEndSize, EndSize) float _endSize; inline float getEndSize() const { return _endSize; };inline void setEndSize(float endSize) { _endSize = endSize; };
CC_PROPERTY(float, m_fEndSizeVar, EndSizeVar) float _endSizeVar; inline float getEndSizeVar() const { return _endSizeVar; };inline void setEndSizeVar(float sizeVar) { _endSizeVar = sizeVar; };
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tStartColor, StartColor) Color4F _startColor; inline const Color4F& getStartColor() const { return _startColor; };inline void setStartColor(const Color4F& color) { _startColor = color; };
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tStartColorVar, StartColorVar) Color4F _startColorVar;inline const Color4F& getStartColorVar() const { return _startColorVar; };inline void setStartColorVar(const Color4F& color) { _startColorVar = color; };
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tEndColorVar, EndColorVar) Color4F _endColorVar; inline const Color4F& getEndColor() const { return _endColor; };inline void setEndColor(const Color4F& color) { _endColor = color; };
CC_PROPERTY(float, m_fStartSpin, StartSpin) float _startSpin; inline float getStartSpin() const { return _startSpin; };inline void setStartSpin(float spin) { _startSpin = spin; };
CC_PROPERTY(float, m_fStartSpinVar, StartSpinVar) float _startSpinVar; inline float getStartSpinVar() const { return _startSpinVar; };inline void setStartSpinVar(float pinVar) { _startSpinVar = pinVar; };
CC_PROPERTY(float, m_fEndSpin, EndSpin) float _endSpinVar; inline float getEndSpin() const { return _endSpin; };inline void setEndSpin(float endSpin) { _endSpin = endSpin; };
CC_PROPERTY(float, m_fEndSpinVar, EndSpinVar) float _endSpinVar; inline float getEndSpinVar() const { return _endSpinVar; };inline void setEndSpinVar(float endSpinVar) { _endSpinVar = endSpinVar; };
CC_PROPERTY(float, m_fEmissionRate, EmissionRate) float _emissionRate; inline float getEmissionRate() const { return _emissionRate; };inline void setEmissionRate(float rate) { _emissionRate = rate; };
CC_PROPERTY(unsigned int, m_uTotalParticles, TotalParticles) int _totalParticles; virtual int getTotalParticles() const;virtual void setTotalParticles(int totalParticles);
CC_PROPERTY(CCTexture2D*, m_pTexture, Texture) Texture2D* _texture; virtual Texture2D* getTexture() const override;virtual void setTexture(Texture2D *texture) override;
CC_PROPERTY(ccBlendFunc, m_tBlendFunc, BlendFunc) BlendFunc _blendFunc; virtual void setBlendFunc(const BlendFunc &blendFunc) override;virtual const BlendFunc &getBlendFunc() const override;
CC_PROPERTY(bool, m_bOpacityModifyRGB, OpacityModifyRGB) bool _opacityModifyRGB;inline void setOpacityModifyRGB(bool opacityModifyRGB) { _opacityModifyRGB = opacityModifyRGB; };inline bool isOpacityModifyRGB() const { return _opacityModifyRGB; };CC_DEPRECATED_ATTRIBUTE inline bool getOpacityModifyRGB() const { return isOpacityModifyRGB(); }
CC_PROPERTY(tCCPositionType, m_ePositionType, PositionType) PositionType _positionType;inline PositionType getPositionType() const { return _positionType; };inline void setPositionType(PositionType type) { _positionType = type; };
CC_SYNTHESIZE(unsigned int, m_uAtlasIndex, AtlasIndex) int _atlasIndex; inline int getAtlasIndex() const { return _atlasIndex; };inline void setAtlasIndex(int index) { _atlasIndex = index; };
  • 在3.x中新增了两个属性std::string _configName;和int _yCoordFlipped;

CCParticleSystemQuad和ParticleSystemQuad

相似的地方:

继承图
ParticleSystemQuad是ParticleSystem的子类。
它包含了ParticleSystem中所有的特性。
下面是它独有的一些特性和限制:
  • 粒子尺寸可为任意float值;
  • 系统可以缩放;
  • 粒子可以旋转;
  • 支持subrects(不知道怎么译,“子矩”自己看了都不懂)
  • 从1.1版本开始支持批量渲染

不同的地方:

3.x中没啥改变,像在2.x中使用3.x的类就可以了,更多的细节请点击这里

例子:

  • 下面是ParticleSystem的例子:
 
/*
    // Cocos2d-x-2.x
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    ParticleSystemQuad* m_emitter = ParticleSystemQuad::createWithTotalParticles(900);
    m_emitter->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
*/
    // Cocos2d-x-3.x
    auto size = Director::getInstance()->getWinSize();
    auto m_emitter = ParticleSystemQuad::createWithTotalParticles(900);
    m_emitter->setTexture(Director::getInstance()->getTextureCache()->addImage("fire.png"));

    //The code below we can use both in 2.x and 3.x 
    m_emitter->setDuration(-1);
    m_emitter->setGravity(Point(0, -240));  // in Cocos2d-x-2.x CCPoint(0, -240) is used
    m_emitter->setAngle(90);
    m_emitter->setAngleVar(360);
    m_emitter->setRadialAccel(50);
    m_emitter->setRadialAccelVar(0);
    m_emitter->setTangentialAccel(30);
    m_emitter->setTangentialAccelVar(0);
    m_emitter->setPosition(Point(size.width / 2, size.height));
    m_emitter->setPosVar(Point(400, 0));
    m_emitter->setLife(4);
    m_emitter->setLifeVar(2);
    m_emitter->setStartSpin(30);
    m_emitter->setStartSpinVar(60);
    m_emitter->setEndSpin(60);
    m_emitter->setEndSpinVar(60);
    m_emitter->setStartColor(Color4F(255,255,255,1));
    m_emitter->setStartColorVar(Color4F(0,0,0,0));
    m_emitter->setEndColor(Color4F(255, 255, 255, 1));
    m_emitter->setEndColorVar(Color4F(0, 0, 0, 0));
    m_emitter->setStartSize(30);
    m_emitter->setStartSizeVar(0);
    m_emitter->setEndSize(20.0f);
    m_emitter->setEndSizeVar(0);
    m_emitter->setEmissionRate(100);
    addChild(m_emitter,10);
  • 你也可以使用下面这些牛B的子类来轻易创建一些常用的粒子效果
    • ParticleExplosion, ParticleFire, ParticleFireworks, ParticleFlower, ParticleGalaxy, ParticleMeteor, ParticleRain, ParticleSmoke, ParticleSnow, ParticleSpiral, ParticleSun
posted @ 2015-09-23 14:27  &&123  阅读(337)  评论(0编辑  收藏  举报