智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

cocos2d-x 卡牌翻牌效果的实现

Posted on 2013-06-25 10:17  Bill Yuan  阅读(2386)  评论(0编辑  收藏  举报

转自:http://blog.csdn.net/yanghuiliu/article/details/9115833

这个能实现翻牌的action就是CCOrbitCamera。

static CCOrbitCamera* create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX);

参数分别为旋转的时间,起始半径,半径差,起始z角,旋转z角差,起始x角,旋转x角差

下面是我封装的cardSprite

//创建翻牌sprite, 参数:卡片里面的图,卡片的封面,翻牌所花时间  
CardSprite* card = CardSprite::create("1.png", "2.png", 2);  card->setPosition(ccp(size.width * .5, size.height * .5));  addChild(card);  
card->openCard();//开始翻牌

CardSprite.h

//
//  CardSprite.h
//  Cocos2dev.com
//
//  Created by LiuYanghui on 13-6-17.
//
//

#ifndef __Test__CardSprite__
#define __Test__CardSprite__

#include "cocos2d.h"
USING_NS_CC;

class CardSprite : public CCSprite
{
public:
    CardSprite();
    ~CardSprite();
    static CardSprite* create(const char* inCardImageName, const char* outCardImageName, float duration);
    virtual bool init(const char* inCardImageName, const char* outCardImageName, float duration);
    
private:
    bool m_isOpened;
    CCActionInterval* m_openAnimIn;
    CCActionInterval* m_openAnimOut;
    
    
    void initData(const char* inCardImageName, const char* outCardImageName, float duration);
    
public:
    void openCard();
};

#endif /* defined(__Test__CardSprite__) */

CardSprite.cpp

//
//  CardSprite.cpp
//  Cocos2dev.com
//
//  Created by LiuYanghui on 13-6-17.
//
//

#include "CardSprite.h"

#define kInAngleZ        270 //里面卡牌的起始Z轴角度
#define kInDeltaZ        90  //里面卡牌旋转的Z轴角度差

#define kOutAngleZ       0   //封面卡牌的起始Z轴角度
#define kOutDeltaZ       90  //封面卡牌旋转的Z轴角度差

enum {
    tag_inCard = 1,
    tag_outCard
};

CardSprite::CardSprite()
{
    
}

CardSprite::~CardSprite()
{
    m_openAnimIn->release();
    m_openAnimOut->release();
}

CardSprite* CardSprite::create(const char* inCardImageName, const char* outCardImageName, float duration)
{
    CardSprite *pSprite = new CardSprite();
    if (pSprite && pSprite->init(inCardImageName, outCardImageName, duration))
    {
        pSprite->autorelease();
        return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
}

bool CardSprite::init(const char* inCardImageName, const char* outCardImageName, float duration)
{
    if (!CCSprite::init())
    {
        return false;
    }
    initData(inCardImageName, outCardImageName, duration);
    return true;
}

#pragma mark - initData
void CardSprite::initData(const char* inCardImageName, const char* outCardImageName, float duration)
{
    m_isOpened = false;
    
    CCSprite* inCard = CCSprite::create(inCardImageName);
    inCard->setPosition(CCPointZero);
    inCard->setVisible(false);
    inCard->setTag(tag_inCard);
    addChild(inCard);
    
    CCSprite* outCard = CCSprite::create(outCardImageName);
    outCard->setPosition(CCPointZero);
    outCard->setTag(tag_outCard);
    addChild(outCard);
    
    m_openAnimIn = (CCActionInterval*)CCSequence::create(CCDelayTime::create(duration * .5),
                                                         CCShow::create(),
                                                         CCOrbitCamera::create(duration * .5, 1, 0, kInAngleZ, kInDeltaZ, 0, 0),
                                                         NULL);
    m_openAnimIn->retain();
    
    m_openAnimOut = (CCActionInterval *)CCSequence::create(CCOrbitCamera::create(duration * .5, 1, 0, kOutAngleZ, kOutDeltaZ, 0, 0),
                                                           CCHide::create(),
                                                           CCDelayTime::create(duration * .5),
                                                           NULL);
    m_openAnimOut->retain();
}

#pragma mark - public func
void CardSprite::openCard()
{
    CCSprite* inCard = (CCSprite*)getChildByTag(tag_inCard);
    CCSprite* outCard = (CCSprite*)getChildByTag(tag_outCard);
    inCard->runAction(m_openAnimIn);
    outCard->runAction(m_openAnimOut);
}