EllipseBy.h:
#ifndef _ELliPSEBY_H_#define _ELliPSEBY_H_#include "cocos2d.h"USING_NS_CC;#define PI 3.14159//椭圆的参数信息struct EllipseConfig { //椭圆a的长度 float ellipseA; //椭圆b的长度 float ellipseB; //椭圆的中心坐标 Vec2 cenPos; //是否逆时针旋转 bool isAntiClockwise; //目标开始旋转的位置,默认位置是在椭圆长轴右方,即值为0 float startAngle; //目标自身的角度 float selfAngle;};class EllipseBy : public ActionInterval{public: EllipseBy(); ~EllipseBy(); //初始化函数,参数t为持续时间,config为椭圆参数 static EllipseBy * create(float t,const EllipseConfig & config); bool initWithDuration(float t,const EllipseConfig & config); //每帧更新当前椭圆坐标 virtual voID update(float time) overrIDe; //在动作开始前调用 virtual voID startWithTarget(Node *target) overrIDe; //动作的拷贝 virtual EllipseBy * clone() const overrIDe; //动作的逆序 virtual EllipseBy * reverse() const overrIDe;protected: //获得椭圆上当前点坐标 inline Vec2 & getPosWithEllipse(float t) { float angle = 2 * PI * ((m_config.isAntiClockwise ? t : (1 - t)) + m_config.startAngle / 360); return Vec2(m_config.ellipseA * cos(angle),m_config.ellipseB * sin(angle)); }private: EllipseConfig m_config;};#endif
EllipseBy.cpp:
#include "EllipseBy.h"EllipseBy::EllipseBy(){}EllipseBy::~EllipseBy(){}EllipseBy * EllipseBy::create(float t,const EllipseConfig & config){ auto pAction = new EllipseBy(); if (pAction && pAction->initWithDuration(t,config)) { pAction->autorelease(); } else { CC_SAFE_DELETE(pAction); } return pAction;}bool EllipseBy::initWithDuration(float t,const EllipseConfig & config){ if (!ActionInterval::initWithDuration(t)) { return false; } m_config = config; return true;}EllipseBy * EllipseBy::clone() const{ auto pAction = new EllipseBy(); pAction->initWithDuration(_duration,m_config); pAction->autorelease(); return pAction;}EllipseBy * EllipseBy::reverse() const{ EllipseConfig resConfig = m_config; resConfig.isAntiClockwise = !m_config.isAntiClockwise; return EllipseBy::create(_duration,m_config);}voID EllipseBy::startWithTarget(Node *target){ ActionInterval::startWithTarget(target);}voID EllipseBy::update(float time){ if (_target) { Vec2 curPos = this->getPosWithEllipse(time); float tmpAngle = m_config.selfAngle / 180 * PI; float newX = curPos.x * cos(tmpAngle) + curPos.y * sin(tmpAngle); float newY = curPos.y * cos(tmpAngle) - curPos.x * sin(tmpAngle); _target->setposition(m_config.cenPos + Vec2(newX,newY)); }}
LotteryTurnTest.h:
#ifndef _LottERY_TURN_TEST_H_#define _LottERY_TURN_TEST_H_#include "cocos2d.h"USING_NS_CC;class LotteryTurnTest : public cocos2d::Layer{public: LotteryTurntest(); ~LotteryTurntest(); static cocos2d::Scene * create(); virtual bool init();protected: voID onBtnCallback(Ref * obj); voID onTurnEnd();private: Sprite * m_turnBg; MenuItemSprite * m_turnArr; Sprite * m_pBg; ParticleSystemQuad * m_pElliRtt_1; ParticleSystemQuad * m_pElliRtt_2; ParticleSystemQuad * m_pCircle_1; ParticleSystemQuad * m_pCircle_2;};#endif
LotteryTurnTest.cpp:
#include "LotteryTurnTest.h"#include "EllipseBy.h"LotteryTurnTest::LotteryTurntest(): m_turnBg(nullptr),m_turnArr(nullptr),m_pBg(nullptr),m_pElliRtt_1(nullptr),m_pElliRtt_2(nullptr),m_pCircle_1(nullptr),m_pCircle_2(nullptr){}LotteryTurnTest::~LotteryTurntest(){}Scene * LotteryTurnTest::create(){ auto scene = Scene::create(); auto pLayer = new LotteryTurntest(); if (pLayer && pLayer->init()) { pLayer->autorelease(); scene->addChild(pLayer); return scene; } else { CC_SAFE_DELETE(pLayer); return NulL; }}bool LotteryTurnTest::init(){ if (!Layer::init()) { return false; } auto bgSize = Director::getInstance()->getWinSize(); m_pBg = Sprite::create("LotteryTurn/bg_big.png"); m_pBg->setposition(Vec2(bgSize.wIDth / 2,bgSize.height / 2)); this->addChild(m_pBg); //添加标题 auto plabel = Label::createWithTTF("LotteryTurnTest","Fonts/Marker Felt.ttf",30); plabel->setposition(Vec2(bgSize.wIDth / 2,bgSize.height * 0.9)); m_pBg->addChild(plabel); //添加转盘 m_turnBg = Sprite::create("LotteryTurn/turn_bg.png"); m_turnBg->setposition(Vec2(bgSize.wIDth / 2,bgSize.height / 2)); m_pBg->addChild(m_turnBg); //添加指针 auto arrnor = Sprite::create("LotteryTurn/turn_arrow.png"); auto arrSel = Sprite::create("LotteryTurn/turn_arrow.png"); arrSel->setcolor(color3B(190,190,190)); m_turnArr = MenuItemSprite::create(arrnor,arrSel,CC_CALLBACK_1(LotteryTurnTest::onBtnCallback,this)); m_turnArr->setposition(Vec2(bgSize.wIDth / 2,bgSize.height * 0.557)); m_turnArr->setScale(0.7); auto pMenu = Menu::createWithItem(m_turnArr); pMenu->setposition(Vec2::ZERO); m_pBg->addChild(pMenu); //添加中奖之后的简单界面 auto awardLayer = Layercolor::create(color4B(0,100)); awardLayer->setposition(Point::ZERO); awardLayer->setTag(100); m_pBg->addChild(awardLayer,10); awardLayer->setVisible(false); return true;}voID LotteryTurnTest::onBtnCallback(Ref * obj){ //防止多次点击 m_turnArr->setEnabled(false); srand(unsigned(time(NulL))); float angleZ = rand() % 720 + 720; auto pAction = EaseExponentialOut::create(RotateBy::create(4,Vec3(0,angleZ))); m_turnBg->runAction(Sequence::create(pAction,CallFunc::create(CC_CALLBACK_0(LotteryTurnTest::onTurnEnd,this)),NulL)); //添加椭圆旋转粒子效果 m_pElliRtt_1 = ParticleSystemQuad::create("LotteryTurn/whiteBall.pList"); m_pBg->addChild(m_pElliRtt_1); m_pElliRtt_2 = ParticleSystemQuad::create("LotteryTurn/yellowBall.pList"); m_pBg->addChild(m_pElliRtt_2); //椭圆旋转 EllipseConfig config; config.ellipseA = 100; config.ellipseB = 50; config.cenPos = m_turnBg->getposition(); config.isAntiClockwise = true; config.startAngle = 0; config.selfAngle = 45; m_pElliRtt_1->runAction(RepeatForever::create( EllipseBy::create(2.5,config))); config.startAngle = 180; config.selfAngle = -45; m_pElliRtt_2->runAction(RepeatForever::create(EllipseBy::create(2.5,config))); //圈圈闪烁粒子效果 m_pCircle_1 = ParticleSystemQuad::create("LotteryTurn/bigCircle.pList"); m_pCircle_1->setposition(m_turnBg->getposition()); m_pBg->addChild(m_pCircle_1); m_pCircle_1->runAction(RepeatForever::create(RotateBy::create(5,angleZ)))); m_pCircle_2 = ParticleSystemQuad::create("LotteryTurn/smallCircle.pList"); m_pCircle_2->setposition(m_turnBg->getposition()); m_pBg->addChild(m_pCircle_2); m_pCircle_2->runAction(RepeatForever::create(RotateBy::create(5,angleZ))));}voID LotteryTurnTest::onTurnEnd(){ m_pElliRtt_1->removeFromParentAndCleanup(true); m_pElliRtt_2->removeFromParentAndCleanup(true); m_pCircle_1->removeFromParentAndCleanup(true); m_pCircle_2->removeFromParentAndCleanup(true); //d出抽中奖品 ((Layercolor *)m_pBg->getChildByTag(100))->setVisible(true); auto award = Sprite::create("LotteryTurn/award.png"); award->setAnchorPoint(Vec2(0.5,0)); award->setposition(Vec2(m_pBg->getpositionX(),m_pBg->getpositionY() * 2)); this->addChild(award); auto bounce = EaseBounceOut::create(MoveBy::create(2,Vec2(0,-m_pBg->getpositionX() * 2))); award->runAction(Sequence::createWithTwoActions(bounce,CallFuncN::create([=](Node * node){ award->removeFromParentAndCleanup(true); ((Layercolor *)m_pBg->getChildByTag(100))->setVisible(false); m_turnArr->setEnabled(true); })));}总结
以上是内存溢出为你收集整理的cocos2dx实现转盘旋转外加粒子效果全部内容,希望文章能够帮你解决cocos2dx实现转盘旋转外加粒子效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)