CCSprite實現翻牌的效果
今天碰見了一個問題,策劃要求實現按鈕翻牌的效果,我試了試CCScale,CCRotate都沒有成功,於是上網一通搜索,終於找到了實現的方法了。那就是CCOrbitCamera:
/**
@brief CCOrbitCamera action
Orbits the camera around the center of the screen using spherical coordinates
@ingroup Actions
*/
這是cocos2d-x對這個類的描述,說是依據球面座標圍繞屏幕中心轉動。聽起來這個解釋讓人挺不能理解的,接着看它的create函數:
/** creates a CCOrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX */
static CCOrbitCamera* create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX);
我查了查,這幾個參數的意思分別是 旋轉的時間,起始半徑,半徑差,起始z角,旋轉z角差,起始x角,旋轉x角差,
翻牌必須有兩張圖片一張是正面一張是背面,這裏我用兩個頭像,正面顯示一個頭像,背面顯示一個頭像,這樣做翻轉,
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//正面的頭像,添加進Layer
CCSprite *sprite_1 = CCSprite::create("head1001.png");
sprite_1->setPosition(ccp(winSize.width/2,winSize.height/2));
addChild(sprite_1);
然後我們設置動作
CCOrbitCamera * orbit = CCOrbitCamera::create(4,0.5, 0, 0, 180, 0, 0);
//翻轉到一半時,調用一個函數
CCDelayTime *delay = CCDelayTime::create(2);
//在這裏改變CCSprite的displayersprite
CCCallFuncO *call = CCCallFuncO::create(this,callfuncO_selector(ChatScrollView::buttonClick),sprite_1);
CCFiniteTimeAction *seq = CCSequence::create(delay,call,NULL);
CCFiniteTimeAction *spawn = CCSpawn::create(orbit,seq,NULL);
sprite_1->runAction(spawn);
下面就是我們要調用的方法
void ChatScrollView::buttonClick(CCNode *sender)
{
CCSprite *sprite = (CCSprite*)sender;
//換圖片,其實是把CCSprite的Frame替換了
CCSprite *roundSprite = CCSprite::create("head1002.png");
sprite->setDisplayFrame(roundSprite->displayFrame());
//翻轉,這樣就能顯示正常的圖像了
sprite->setScaleX(-1);
}