cocos2dx實現轉盤旋轉外加粒子效果

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,0,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,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,Vec3(0,0,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,Vec3(0,0,angleZ))));

}

void LotteryTurnTest::onTurnEnd()
{
	m_pElliRtt_1->removeFromParentAndCleanup(true);
	m_pElliRtt_2->removeFromParentAndCleanup(true);
	m_pCircle_1->removeFromParentAndCleanup(true);
	m_pCircle_2->removeFromParentAndCleanup(true);
	
	//彈出抽中獎品
	((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);
	})));

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章