cocos2d-x 翻牌效果CCOrbitCamera

前端之家收集整理的这篇文章主要介绍了cocos2d-x 翻牌效果CCOrbitCamera前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
原创地址:<a target=_blank href="http://cn.cocos2d-x.org/tutorial/show?id=2183">http://cn.cocos2d-x.org/tutorial/show?id=2183</a>
cpp

#include "PokerSprite.h"

PokerSprite* PokerSprite::create(const char* front,const char* back){
	PokerSprite* pSrpite = new PokerSprite();

	if (pSrpite->initWithTexture(CCTextureCache::sharedTextureCache()->addImage(front),CCTextureCache::sharedTextureCache()->addImage(back)))
	{
		pSrpite->autorelease();
		return pSrpite;
	}
	delete pSrpite;
	return NULL;
}

PokerSprite* PokerSprite::create(CCSprite* front,CCSprite* back){
	PokerSprite* pSrpite = new PokerSprite();

	if (pSrpite->initWithSprite(front,back))
	{
		pSrpite->autorelease();
		return pSrpite;
	}
	delete pSrpite;
	return NULL;
}

bool PokerSprite::initWithTexture(CCTexture2D* ptFront,CCTexture2D* ptBack){

	front = CCSprite::createWithTexture(ptFront);
	back = CCSprite::createWithTexture(ptBack);

	if (front && back && CCSprite::init())
	{
		this->setContentSize(back->getContentSize());
		//扑克牌正面
		front->setVisible(false);
		CCPoint pos = ccp(this->getContentSize().width / 2,this->getContentSize().height / 2);
		front->setPosition(pos);
		this->addChild(front);
		//扑克牌背面
		back->setPosition(pos);
		this->addChild(back);

		return true;
	}

	return false;
}

bool PokerSprite::initWithSprite(CCSprite* pFornt,CCSprite* pBack){

	if (pFornt && pBack && CCSprite::init())
	{
		front = pFornt;
		back = pBack;

		this->setContentSize(back->getContentSize());
		//扑克牌正面
		front->setVisible(false);
		CCPoint pos = ccp(this->getContentSize().width / 2,this->getContentSize().height / 2);
		front->setPosition(pos);
		this->addChild(front);
		//扑克牌背面
		back->setPosition(pos);
		this->addChild(back);

		return true;
	}
	return false;
}

void PokerSprite::open(float duration){

	if (front && back)
	{
		front->stopAllActions();
		back->stopAllActions();

		// 正面z轴起始角度为90度(向左旋转90度),然后向右旋转90度
		CCOrbitCamera* orbitFront = CCOrbitCamera::create(duration*0.5,1,90,-90,0);
		// 正面z轴起始角度为0度,然后向右旋转90度
		CCOrbitCamera* orbitBack = CCOrbitCamera::create(duration*0.5,0);

		front->setVisible(false);
		// 背面向右旋转90度->正面向左旋转90度
		back->runAction(CCSequence::create(CCShow::create(),orbitBack,CCHide::create(),CCTargetedAction::create(front,CCSequence::create(CCShow::create(),orbitFront,NULL)),NULL));
	}
	else{
		CCLOG("[ERROR] PokerSprite front or back not init.");
	}
}


#ifndef __POKERSPRITE_H__
#define __POKERSPRITE_H__

#include "cocos2d.h"
USING_NS_CC;

/*
具有翻牌效果的CCSprite
*/
class PokerSprite :public CCSprite
{
public:
	/**
	*   创建一个PokerSprite
	*   front   牌的正面
	*   back    牌的背面
	*/
	static PokerSprite* create(const char* front,const char* back);
	/**
	*   创建一个PokerSprite
	*   front   牌的正面
	*   back    牌的背面
	*/
	static PokerSprite* create(CCSprite* front,CCSprite* back);
	/* 翻牌
	* duration  动作时长(秒)
	*/
	void open(float duration = 1.5);
protected:
	bool initWithSprite(CCSprite* fornt,CCSprite* back);
	bool initWithTexture(CCTexture2D* fornt,CCTexture2D* back);
private:
	CCSprite* front; // 正面
	CCSprite* back;     // 背面
};

#endif

调用:

	CCSize size = CCDirector::sharedDirector()->getVisibleSize();
		//创建PokerSrpite,指定正面和背面所使用的图片路径
		poker = PokerSprite::create("10.png","27.png");


		//创建PokerSrpite,指定正面和背面所使用的CCSprite
		//CCSprite* front = CCSprite::create("front.png")
		//front->addChild(img);
		//poker = PokerSprite::create(front,CCSprite::create("back.png"));


		// 设置位置
		poker->setPosition(ccp(size.width*0.5,size.height*0.5));


		// 添加到Layer
		addChild(poker);


		//调用翻牌动作
		poker->open();

猜你在找的Cocos2d-x相关文章