转载备用,后面项目需要用。
http://blog.csdn.net/goichunhye11/article/details/50825126
因为项目需求,需要使用cocos实现3d翻书的效果,刚开始确实没有什么思路,cocos2d做3d的效果这不是开玩笑吗。但是,再难也得做啊,没办法。
开始查资料,在百度,google上搜索了好几天,基本把所有的文章都翻遍了,根本没有人实现过,好吧,我承认有点虚了,这可咋办。
后来想到cocos2dx的例子里有个类似的效果,不过那个是个action,后来看了看感觉还可以,只是效果和我的需求有点差异,但终归是找到了实现的思路,于是开始看cocos2dx的源码,主要用到的是cocos的网格动画,涉及到以下几个文件:
CCGrid.h /cpp :网格数据及渲染,包括基本网格数据和3D网格数据,这是数据的基础。
CCActionGrid.h /cpp :网格基本动画,这是动画的基础。
CCActionGrid3D.h/cpp: 3D网格基本动画,这是3D网格动画的基础。
CCActionTiledGrid.h / cpp :网格衍生动画,这是最终的特效实现。
咱们这里主要用到CCGrid.h进行渲染,其他三个是action的实现。
这里插一句,在此之前,我曾经还想用遮罩去实现类似的翻书效果,因为android上类似效果的实现就是实用遮罩的,可是在cocos中这种方法行不通,最终放弃了。
看CCActionPageTurn3d这个类,cocos的翻页效果的实现就是在这个类中实现的,在这个类中有个update()函数:
/*
* Update each tick
* Time is the percentage of the way through the duration
*/
voidCCPageTurn3D::update(float time)
{
float tt =MAX(0, time -0.25f);
- #include "BoluPageTurn2.h"
- #include "BoluButton.h"
- #include "../BoluMsg/BoluMsgPick.h"
- #include "../BoluStage.h"
- #include "../BoluGame/BoluPropNm.h"
- #include "../BoluPlay/BoluMngTable.h"
- #include "RoundProgress.hpp"
- #include "../BoluUserCfg.h"
- #define FLIP_CARD_NUM "FLIPCARDNUM"
- SuitPosInfo PageTurn2::pos[] = {{60,400,0},{60,263,0},{60,128,0}};
- // implementation of PageTurn2
- PageTurn2::PageTurn2(void):
- m_pForeSprite(NULL),
- m_pBgSprite(NULL),
- m_pBgSpriteMirror(NULL),
- m_pForeSpriteMirror(NULL),
- m_pForeSpriteVertical(NULL),
- m_pBgSpriteVertical(NULL),
- m_pBgSpriteMirrorVertical(NULL),
- m_pForeSpriteMirrorVertical(NULL),
- //pHorizontalItem(NULL),
- //pVerticalAItem(NULL),
- //pVerticalEItem(NULL),
- m_pShowPokerCard(NULL),
- mpHideBtn(nullptr),
- mpMenu(nullptr),
- mpAlarmLabel(nullptr),
- mCurLyr(nullptr)
- {
- EventManager::sharedEventManager()->addObserver(this);
- }
- PageTurn2::~PageTurn2(void)
- {
- EventManager::sharedEventManager()->removeObserver(this);
- }
- bool PageTurn2::init(const PickHAry &ary,int nTableId,int nLeftTime)
- {
- bool bRet = false;
- do
- {
- CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- setContentSize(winSize);
- int num = GetRayInt(FLIP_CARD_NUM,0);
- if (num < 3) {
- num++;
- SetRayInt(FLIP_CARD_NUM, num);
- NtfShowTipsA("{0xFF0000亲,在设置-系统设置中可关闭或开启搓牌哦}");
- }
- mPickAry = ary;
- mNLeftTM = nLeftTime;
- mTableId = nTableId;
- mCurIndex = g_stage->mFlipProgress[mTableId];
- CCLayerColor* pBgLayer = CCLayerColor::create(ccc4(0, 0, 0, 125));
- pBgLayer->setContentSize(winSize);
- pBgLayer->setAnchorPoint(ccp(0, 0));
- pBgLayer->setPosition(ccp(0, 0));
- addChild(pBgLayer);
- CCSprite* pBgSprite = NtfCreateSprite("FarmAndPine/flip_left_bg.png");
- pBgSprite->setAnchorPoint(ccp(0, 0.5));
- pBgSprite->setPosition(ccp(0, winSize.height/2));
- this->addChild(pBgSprite);
- for (int i = 0; i < mPickAry.size(); i++) {
- if (mCurIndex > i)
- {
- String imgName = GetCardFace(mPickAry[i].mGrp, mPickAry[i].mNum);
- CCSprite* sprite = NtfCreateSprite(imgName.c_str());
- sprite->setPosition(ccp(pos[i].fx,pos[i].fy));
- this->addChild(sprite,1,i);
- }
- else if (mCurIndex == i) {
- cocos2d::extension::CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("BuilderAnimate/fanpaidonghuazuixin0.png", "BuilderAnimate/fanpaidonghuazuixin0.plist", "BuilderAnimate/fanpaidonghuazuixin.ExportJson");
- CCArmature* armatureTriangle = cocos2d::extension::CCArmature::create("fanpaidonghuazuixin");
- armatureTriangle->setAnchorPoint(ccp(0.5, 0.5));
- armatureTriangle->setPosition(ccp(pos[i].fx, pos[i].fy));
- armatureTriangle->getAnimation()->playWithIndex(0);
- this->addChild(armatureTriangle,1,mCurIndex);
- }
- else
- {
- CCSprite* sprite = NtfCreateSprite("SmallImg/bkg_playboard06.png");
- sprite->setPosition(ccp(pos[i].fx, pos[i].fy));
- this->addChild(sprite,1,i);
- }
- }
- m_sGridSize = CCSizeMake(50, 50);
- createPokerSprite(ary[mCurIndex].mGrp,ary[mCurIndex].mNum);
- // 玩家头像
- String sImg = g_pSelf->GetSelfIcon();
- if (sImg.empty())
- sImg = "ImgIcon/bkgIconCircle01.png";
- CCClippingNode* clipA = NtfSetHeadIcon(sImg.c_str(), CCSize(56, 56));
- clipA->setAnchorPoint(ccp(0.5, 0.5));
- clipA->setPosition(ccp(60, winSize.height - 118));
- this->addChild(clipA, 30,TAG_AVATAR);
- //搓牌倒计时
- CCSprite* pTimerBg = NtfCreateSprite("SmallImg/bkg_pic_Other.png");
- pTimerBg->setAnchorPoint(ccp(0.5, 0.5));
- pTimerBg->setPosition(ccp(60, winSize.height - 118));
- addChild(pTimerBg);
- g_stage->DisplayAvatar(g_pSelf->GetValue(RVT_ACC_ID), 0);
- std::stringstream s;
- s << mNLeftTM;
- CCNode *vNd = AddNumImg(atoi(s.str().c_str()), 10);
- vNd->setPosition(ccp(60, winSize.height - 118));
- vNd->setAnchorPoint(ccp(0.5, 0.5));
- this->addChild(vNd, 60,TAG_TIMER);
- // mpAlarmLabel = CCLabelTTF::create(s.str().c_str(), "", 22, CCSize(100, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter);
- // mpAlarmLabel->setPosition(ccp(60, winSize.height - 118));
- // mpAlarmLabel->setAnchorPoint(ccp(0.5, 0.5));
- // this->addChild(mpAlarmLabel,60);
- schedule(schedule_selector(PageTurn2::onAlarmUpdate), 1.0f, mNLeftTM, 0.0f);
- mFlipDemoOffset = 0;
- schedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- RoundProgress* pProgress = RoundProgress::create();
- pProgress->setAnchorPoint(ccp(0.5, 0.5));
- pProgress->setPosition(ccp(pTimerBg->getContentSize().width/2,pTimerBg->getContentSize().height/2));
- pTimerBg->addChild(pProgress, 190);
- pProgress->start(mNLeftTM);
- //横置按钮
- SEL_MenuHandler rotate = menu_selector(PageTurn2::OnClickHorizontal);
- CRayButton *pRotateBtn = CRayButton::create("FarmAndPine/btn_rotate_normal.png", "FarmAndPine/btn_rotate_selected.png", NULL);
- pRotateBtn->AttachHandler(this, rotate);
- pRotateBtn->setPosition(ccp(origin.x + winSize.width - pRotateBtn->getContentSize().width / 2 - 100,
- origin.y + pRotateBtn->getContentSize().height / 2 + 120));
- pRotateBtn->EnableAnima(false);
- this->addChild(pRotateBtn);
- mTouchLyr.push_back(pRotateBtn);
- //开牌按钮
- SEL_MenuHandler flip = menu_selector(PageTurn2::OnClickFlip);
- CRayButton *pFlipBtn = CRayButton::create("FarmAndPine/btn_flipcard_normal.png", "FarmAndPine/btn_flipcard_selected.png", NULL);
- pFlipBtn->AttachHandler(this, flip);
- pFlipBtn->setPosition(ccp(origin.x + winSize.width - pFlipBtn->getContentSize().width / 2 - 100,
- origin.y + pFlipBtn->getContentSize().height / 2 + 420));
- pFlipBtn->EnableAnima(false);
- this->addChild(pFlipBtn,5);
- mTouchLyr.push_back(pFlipBtn);
- mpHideBtn = CCControlButton::create("", "", 26);
- mpHideBtn->setBackgroundSpriteForState(CCScale9Sprite::create("FarmAndPine/btn_hide_card_normal.png"), CCControlStateNormal);
- mpHideBtn->setBackgroundSpriteForState(CCScale9Sprite::create("FarmAndPine/btn_hide_card_selected.png"), CCControlStateHighlighted);
- mpHideBtn->setAnchorPoint(ccp(0.5, 0.5));
- mpHideBtn->setPosition(ccp(origin.x + winSize.width - mpHideBtn->getContentSize().width/2 - 95,
- origin.y + mpHideBtn->getContentSize().height/2 + 370));
- mpHideBtn->setContentSize(CCSizeMake(105, 90));
- mpHideBtn->setPreferredSize(CCSizeMake(105, 90));
- mpHideBtn->setZoomOnTouchDown(false);
- addChild(mpHideBtn,100);
- // 按下按钮事件回调
- mpHideBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(PageTurn2::onTouchDownAction), CCControlEventTouchDown);
- // 按钮在其内部拾起事件回调
- mpHideBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(PageTurn2::onTouchUpInsideAction), CCControlEventTouchUpInside);
- //按钮在其外部拾起事件回调
- mpHideBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(PageTurn2::onTouchUpOutsideAction), CCControlEventTouchUpOutside);
- if (mCurIndex == mPickAry.size()) {
- mbIsFliped = true;
- mFlipDemoOffset = 0;
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- //添加盘牌动画
- m_pShowPokerCard = createPokerCard(mPickAry[mCurIndex-1].mGrp, mPickAry[mCurIndex-1].mNum,true);
- m_pShowPokerCard->setAnchorPoint(ccp(0.5, 0.5));
- m_pShowPokerCard->setPosition(ccp(winSize.width/2, winSize.height/2));
- this->addChild(m_pShowPokerCard,2);
- scheduleOnce(schedule_selector(PageTurn2::onRemoveUpdate), 2);
- }
- bRet = true;
- }
- while (0);
- return bRet;
- }
- void PageTurn2::onRemoveUpdate(float ft)
- {
- g_stage->mFlipProgress[mTableId] = 0;
- removeFromParentAndCleanup(true);
- EventManager::sharedEventManager()->notifyEventFinished(EventShowLastThreeCard);
- }
- void PageTurn2::onTouchDownAction(cocos2d::CCObject *sender, CCControlEvent controlEvent)
- {
- if (!mbIsFliped) {
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- mFlipDemoOffset = 0;
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- }
- else
- {
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(false);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(false);
- }
- }
- }
- void PageTurn2::onTouchUpInsideAction(cocos2d::CCObject *sender, CCControlEvent controlEvent)
- {
- if (!mbIsFliped) {
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- mFlipDemoOffset = 0;
- schedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(true);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(true);
- }
- else
- {
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- }
- }
- }
- void PageTurn2::onTouchUpOutsideAction(cocos2d::CCObject *sender, CCControlEvent controlEvent)
- {
- if (!mbIsFliped) {
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- mFlipDemoOffset = 0;
- schedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(true);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(true);
- }
- else
- {
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- }
- }
- }
- void PageTurn2::createPokerSprite(CCrdGrp type, CCrdNum num)
- {
- //水平牌
- m_pForeSprite = createPokerCard(type, num,false);
- m_pForeSprite->setAnchorPoint(ccp(0.5, 0.5));
- m_pForeSprite->setPosition(ccp(m_pForeSprite->getContentSize().width/2, m_pForeSprite->getContentSize().height/2));
- m_pForeSprite->setFlipX(true);
- this->addChild(m_pForeSprite,2);
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror = createPokerCard(type, num,false);
- m_pForeSpriteMirror->setAnchorPoint(ccp(0.5, 0.5));
- m_pForeSpriteMirror->setPosition(ccp(mHOffsetX + m_pForeSpriteMirror->getContentSize().width/2,mHOffsetY + m_pForeSpriteMirror->getContentSize().height/2));
- this->addChild(m_pForeSpriteMirror);
- m_pBgSprite = NtfCreateSprite("Cards/card_bg.png");
- m_pBgSprite->setAnchorPoint(ccp(0.5, 0.5));
- m_pBgSprite->setPosition(ccp(m_pBgSprite->getContentSize().width/2, m_pBgSprite->getContentSize().height/2));
- this->addChild(m_pBgSprite,1);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror = NtfCreateSprite("Cards/card_bg.png");
- m_pBgSpriteMirror->setAnchorPoint(ccp(0.5, 0.5));
- m_pBgSpriteMirror->setPosition(ccp(mHOffsetX + m_pBgSpriteMirror->getContentSize().width/2,mHOffsetY + m_pBgSpriteMirror->getContentSize().height/2));
- this->addChild(m_pBgSpriteMirror);
- //竖直牌
- m_pForeSpriteVertical = createPokerCard(type, num,false);
- m_pForeSpriteVertical->setAnchorPoint(ccp(0.5, 0.5));
- m_pForeSpriteVertical->setPosition(ccp(m_pForeSpriteVertical->getContentSize().height/2 ,m_pForeSpriteVertical->getContentSize().width/2));
- m_pForeSpriteVertical->setFlipX(true);
- this->addChild(m_pForeSpriteVertical,2);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteVertical->setRotation(90);
- m_pForeSpriteMirrorVertical = createPokerCard(type, num,false);
- m_pForeSpriteMirrorVertical->setAnchorPoint(ccp(0.5, 0.5));
- m_pForeSpriteMirrorVertical->setPosition(ccp(mVOffsetX + m_pForeSpriteMirrorVertical->getContentSize().height/2 ,mVOffsetY + m_pForeSpriteMirrorVertical->getContentSize().width/2));
- this->addChild(m_pForeSpriteMirrorVertical);
- m_pForeSpriteMirrorVertical->setRotation(90);
- m_pBgSpriteVertical = NtfCreateSprite("Cards/card_bg.png");
- m_pBgSpriteVertical->setAnchorPoint(ccp(0.5, 0.5));
- m_pBgSpriteVertical->setPosition(ccp(m_pBgSpriteVertical->getContentSize().height/2 ,m_pBgSpriteVertical->getContentSize().width/2));
- this->addChild(m_pBgSpriteVertical,1);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteVertical->setRotation(90);
- m_pBgSpriteMirrorVertical = NtfCreateSprite("Cards/card_bg.png");
- m_pBgSpriteMirrorVertical->setAnchorPoint(ccp(0.5, 0.5));
- m_pBgSpriteMirrorVertical->setPosition(ccp(mVOffsetX + m_pBgSpriteMirrorVertical->getContentSize().height/2 ,mVOffsetY + m_pBgSpriteMirrorVertical->getContentSize().width/2));
- this->addChild(m_pBgSpriteMirrorVertical);
- m_pBgSpriteMirrorVertical->setRotation(90);
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(false);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(false);
- }
- // cocos2d::extension::CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("BuilderAnimate/cuowo-triangle0.png", "BuilderAnimate/cuowo-triangle0.plist", "BuilderAnimate/cuowo-triangle.ExportJson");
- // CCArmature* armatureTriangle = cocos2d::extension::CCArmature::create("cuowo-triangle");
- // armatureTriangle->setAnchorPoint(ccp(1, 0));
- // armatureTriangle->setPosition(ccp(m_pBgSprite->getContentSize().width, 0));
- // armatureTriangle->getAnimation()->playWithIndex(0);
- // m_pBgSpriteMirror->addChild(armatureTriangle);
- //
- //
- // cocos2d::extension::CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("BuilderAnimate/cuopai0.png", "BuilderAnimate/cuopai0.plist", "BuilderAnimate/cuopai.ExportJson");
- // CCArmature* armature = cocos2d::extension::CCArmature::create("cuopai");
- // armature->setAnchorPoint(ccp(0, 0));
- // armature->setPosition(ccp(m_pBgSprite->getContentSize().width, 0));
- // armature->setRotation(-90);
- // armature->getAnimation()->playWithIndex(0);
- // m_pBgSpriteMirrorVertical->addChild(armature);
- //
- // CCArmature* armature2 = cocos2d::extension::CCArmature::create("cuopai");
- // armature2->setAnchorPoint(ccp(0, 0));
- // armature2->setPosition(ccp(0, m_pBgSprite->getContentSize().height));
- // armature2->setRotation(90);
- // armature2->getAnimation()->playWithIndex(0);
- // m_pBgSpriteMirrorVertical->addChild(armature2);
- startWithTarget(m_pForeSprite,true,m_pForeSprite->getContentSize());
- startWithTarget(m_pBgSprite,false,m_pBgSprite->getContentSize());
- startWithTarget(m_pForeSpriteVertical, true, CCSizeMake(m_pForeSpriteVertical->getContentSize().height, m_pForeSpriteVertical->getContentSize().width));
- startWithTarget(m_pBgSpriteVertical, false, CCSizeMake(m_pBgSpriteVertical->getContentSize().height, m_pBgSpriteVertical->getContentSize().width));
- }
- void PageTurn2::onAlarmUpdate(float ft)
- {
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- CCNode* pNode = getChildByTag(TAG_TIMER);
- if (pNode) {
- pNode->removeFromParent();
- mNLeftTM--;
- if (mNLeftTM <= 0) {
- OnClickFlip(nullptr);
- unschedule(schedule_selector(PageTurn2::onAlarmUpdate));
- }
- std::stringstream s;
- s << mNLeftTM;
- CCNode *vNd = AddNumImg(atoi(s.str().c_str()), 10);
- vNd->setPosition(ccp(60, winSize.height - 118));
- vNd->setAnchorPoint(ccp(0.5, 0.5));
- this->addChild(vNd, 60,TAG_TIMER);
- }
- // if (mpAlarmLabel) {
- // mNLeftTM--;
- // if (mNLeftTM <= 0) {
- // OnClickFlip(nullptr);
- // unschedule(schedule_selector(PageTurn2::onAlarmUpdate));
- // }
- // std::stringstream s;
- // s << mNLeftTM;
- //
- //
- // mpAlarmLabel->setString(s.str().c_str());
- // }
- }
- void PageTurn2::onFlipDemoUpdate(float ft)
- {
- if (mFlipDemoOffset > 100) {
- isIncrease = false;
- }
- else if(mFlipDemoOffset <= 0)
- {
- isIncrease = true;
- }
- if (isIncrease)
- {
- mFlipDemoOffset += 2;
- }
- else
- {
- mFlipDemoOffset -= 2;;
- }
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- if (!m_pBgSprite->isVisible()) {
- m_pBgSprite->setVisible(true);
- m_pBgSpriteMirror->setVisible(false);
- }
- if (!m_pForeSprite->isVisible()) {
- m_pForeSprite->setVisible(true);
- m_pForeSpriteMirror->setVisible(false);
- }
- }
- else
- {
- if (!m_pBgSpriteVertical->isVisible()) {
- m_pBgSpriteVertical->setVisible(true);
- m_pBgSpriteMirrorVertical->setVisible(false);
- }
- if (!m_pForeSpriteVertical->isVisible()) {
- m_pForeSpriteVertical->setVisible(true);
- m_pForeSpriteMirrorVertical->setVisible(false);
- }
- }
- calculateHorizontalVertexPoints(mFlipDemoOffset / cosf(M_PI / 4),true);
- }
- PageTurn2* PageTurn2::create(const PickHAry &ary,int nTableId,int nLeftTime)
- {
- PageTurn2 * pRet = new PageTurn2();
- if (pRet && pRet->init(ary,nTableId,nLeftTime))
- {
- pRet->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(pRet);
- }
- return pRet;
- }
- MyGrid3D* PageTurn2::getGrid(bool bReverse,const CCSize& size)
- {
- return MyGrid3D::create(m_sGridSize,size,bReverse);
- }
- void PageTurn2::startWithTarget(CCNode *pTarget, bool bReverse, const CCSize& size)
- {
- CCGridBase *newgrid = this->getGrid(bReverse,size);
- CCNode *t = pTarget;
- CCGridBase *targetGrid = t->getGrid();
- if (targetGrid && targetGrid->getReuseGrid() > 0)
- {
- if (targetGrid->isActive() && targetGrid->getGridSize().width == m_sGridSize.width
- && targetGrid->getGridSize().height == m_sGridSize.height /*&& dynamic_cast<CCGridBase*>(targetGrid) != NULL*/)
- {
- targetGrid->reuse();
- }
- else
- {
- CCAssert(0, "");
- }
- }
- else
- {
- if (targetGrid && targetGrid->isActive())
- {
- targetGrid->setActive(false);
- }
- t->setGrid(newgrid);
- t->getGrid()->setActive(true);
- }
- }
- ccVertex3F PageTurn2::vertex(const CCPoint& position,CCNode *pTarget)
- {
- CCGrid3D *g = (CCGrid3D*)pTarget->getGrid();
- return g->vertex(position);
- }
- ccVertex3F PageTurn2::originalVertex(const CCPoint& position,CCNode *pTarget)
- {
- CCGrid3D *g = (CCGrid3D*)pTarget->getGrid();
- return g->originalVertex(position);
- }
- void PageTurn2::setVertex(const CCPoint& position, const ccVertex3F& vertex,CCNode *pTarget)
- {
- CCGrid3D *g = (CCGrid3D*)pTarget->getGrid();
- g->setVertex(position, vertex);
- }
- void PageTurn2::registerWithTouchDispatcher()
- {
- // CCDirector* pDirec = CCDirector::sharedDirector();
- // pDirec->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
- }
- bool PageTurn2::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- CCPoint point = pTouch->getLocation();
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- float borderXH = winSize.width/2 + m_pBgSprite->getContentSize().width/2;
- float borderYH = winSize.height/2 - m_pBgSprite->getContentSize().height/2;
- float borderXHMin = winSize.width/2 - m_pBgSprite->getContentSize().width/2;
- float borderXVMax = winSize.width/2 + m_pBgSprite->getContentSize().height/2;
- float borderXVMin = winSize.width/2 - m_pBgSprite->getContentSize().height/2;
- float borderYV = mVOffsetY;
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- if (point.x > borderXH - 150 && point.x < borderXH && point.y > borderYH && point.y < borderYH + 150)
- {
- //触摸牌的右下角
- mTouchBegin = pTouch->getLocation();
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- }
- else if (point.x > borderXHMin && point.x < borderXHMin + 150 && point.y > borderYH && point.y < borderYH + 150)
- {//触摸牌的左下角
- mTouchBegin = pTouch->getLocation();
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- }
- else
- {
- mTouchBegin = CCPoint(0, 0);
- }
- }
- else
- {
- if (point.x > borderXVMin && point.x < borderXVMax && point.y > borderYV && point.y < borderYV + 150)
- {
- //垂直情况下触摸牌的下边缘
- mTouchBegin = pTouch->getLocation();
- }
- else
- {
- mTouchBegin = CCPoint(0, 0);
- }
- }
- return false;
- }
- void PageTurn2::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (mTouchBegin.x != 0 && mTouchBegin.y != 0 && !mbIsFliped) {
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- if (!m_pBgSprite->isVisible()) {
- m_pBgSprite->setVisible(true);
- m_pBgSpriteMirror->setVisible(false);
- }
- if (!m_pForeSprite->isVisible()) {
- m_pForeSprite->setVisible(true);
- m_pForeSpriteMirror->setVisible(false);
- }
- }
- else
- {
- if (!m_pBgSpriteVertical->isVisible()) {
- m_pBgSpriteVertical->setVisible(true);
- m_pBgSpriteMirrorVertical->setVisible(false);
- }
- if (!m_pForeSpriteVertical->isVisible()) {
- m_pForeSpriteVertical->setVisible(true);
- m_pForeSpriteMirrorVertical->setVisible(false);
- }
- }
- float offsetX = mTouchBegin.x - pTouch->getLocation().x;
- float offsetY = pTouch->getLocation().y - mTouchBegin.y;
- bool flag;
- if (offsetX > 0) {
- flag = true;
- }
- else
- {
- flag = false;
- }
- float offset = fabs(offsetX) > offsetY ? offsetX : offsetY;
- calculateHorizontalVertexPoints(offset / cosf(M_PI/4), flag);
- calculateVerticalVertexPoints(offsetY);
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- // CMsgTurnCrd::PostToSever(mBacId,g_pSelf->GetValue(RVT_ACC_ID), mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum, mCardShowType, offsetX);
- judgeIsFlipCard(fabsf(offsetX) / cosf(M_PI / 4),mCardShowType,true);
- }
- else if (mCardShowType == VerticalA || mCardShowType == VerticalE)
- {
- // CMsgTurnCrd::PostToSever(mBacId,g_pSelf->GetValue(RVT_ACC_ID), mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum, mCardShowType, offsetY);
- judgeIsFlipCard(offsetY / cosf(M_PI / 4),mCardShowType,true);
- }
- }
- }
- void PageTurn2::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (mTouchBegin.x != 0 && mTouchBegin.y != 0 && !mbIsFliped) {
- calculateHorizontalVertexPoints(0,true);
- calculateVerticalVertexPoints(-mVOffsetY);
- if (mCardShowType == Horizontal || mCardShowType == Horizontal2) {
- // CMsgTurnCrd::PostToSever(mBacId,g_pSelf->GetValue(RVT_ACC_ID), mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum, mCardShowType, 0);
- }
- else if (mCardShowType == VerticalA || mCardShowType == VerticalE)
- {
- // CMsgTurnCrd::PostToSever(mBacId,g_pSelf->GetValue(RVT_ACC_ID), mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum, mCardShowType, -mVOffsetY);
- }
- }
- }
- void PageTurn2::calculateHorizontalVertexPoints(float offsetX,bool flag)
- {
- float R = 50;
- if (flag)
- {
- float theta = (GLfloat)(M_PI / 6.0f);
- float b = (m_pBgSprite->getContentSize().width - offsetX * 1.4f) * sinf(theta);
- for (int i = 0; i <= m_sGridSize.width; ++i)
- {
- for (int j = 0; j <= m_sGridSize.height; ++j)
- {
- // Get original vertex
- ccVertex3F p = originalVertex(ccp(i ,j),m_pForeSprite);
- float x = (p.y + b) / tanf(theta);
- float pivotX = x + (p.x - x) * cosf(theta) * cosf(theta);
- float pivotY = pivotX * tanf(theta) - b;
- float l = (p.x - pivotX) / sinf(theta);
- float alpha = l / R;
- if (l >= 0) {
- if (alpha > M_PI) {
- p.x = (GLfloat)(mHOffsetX + pivotX - R * (alpha - M_PI) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY + R * (alpha - M_PI) * cosf(theta));
- p.z = (GLfloat)(2 * R / 9);
- }
- else if (alpha <= M_PI)
- {
- p.x = (GLfloat)(mHOffsetX + pivotX + R * sinf(alpha) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY - R * sinf(alpha) * cosf(theta));
- p.z = (GLfloat)((R - R * cosf(alpha)) / 9);
- }
- }
- else
- {
- p.x += mHOffsetX;
- p.y += mHOffsetY;
- }
- // Set new coords
- setVertex(ccp(i, j), p,m_pForeSprite);
- }
- }
- for (int i = 0; i <= m_sGridSize.width; ++i)
- {
- for (int j = 0; j <= m_sGridSize.height; ++j)
- {
- // Get original vertex
- ccVertex3F p = originalVertex(ccp(i ,j),m_pBgSprite);
- float x = (p.y + b) / tanf(theta);
- float pivotX = x + (p.x - x) * cosf(theta) * cosf(theta);
- float pivotY = pivotX * tanf(theta) - b;
- float l = (p.x - pivotX) / sinf(theta);
- float alpha = l / R;
- if (l >= 0) {
- if (alpha > M_PI) {
- p.x = (GLfloat)(mHOffsetX + pivotX - R * (alpha - M_PI) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY + R * (alpha - M_PI) * cosf(theta));
- p.z = (GLfloat)(2 * R / 9);
- }
- else if (alpha <= M_PI)
- {
- p.x = (GLfloat)(mHOffsetX + pivotX + R * sinf(alpha) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY - R * sinf(alpha) * cosf(theta));
- p.z = (GLfloat)((R - R * cosf(alpha)) / 9);
- }
- }
- else
- {
- p.x += mHOffsetX;
- p.y += mHOffsetY;
- }
- setVertex(ccp(i, j), p,m_pBgSprite);
- }
- }
- }
- else
- {
- float theta = (GLfloat)(M_PI / 6.0f);
- float b = fabsf(offsetX * 0.8f);
- for (int i = 0; i <= m_sGridSize.width; ++i)
- {
- for (int j = 0; j <= m_sGridSize.height; ++j)
- {
- // Get original vertex
- ccVertex3F p = originalVertex(ccp(i ,j),m_pForeSprite);
- float x = (p.y - b) / -tanf(theta);
- float pivotX = p.x + (x - p.x) * sinf(theta) * sinf(theta);
- float pivotY = pivotX * -tanf(theta) + b;
- float l = (pivotX - p.x) / sinf(theta);
- float alpha = l / R;
- if (l >= 0) {
- if (alpha > M_PI) {
- p.x = (GLfloat)(mHOffsetX + pivotX + R * (alpha - M_PI) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY + R * (alpha - M_PI) * cosf(theta));
- p.z = (GLfloat)(2 * R / 9);
- }
- else if (alpha <= M_PI)
- {
- p.x = (GLfloat)(mHOffsetX + pivotX - R * sinf(alpha) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY - R * sinf(alpha) * cosf(theta));
- p.z = (GLfloat)((R - R * cosf(alpha)) / 9);
- }
- }
- else
- {
- p.x += mHOffsetX;
- p.y += mHOffsetY;
- }
- // Set new coords
- setVertex(ccp(i, j), p,m_pForeSprite);
- }
- }
- for (int i = 0; i <= m_sGridSize.width; ++i)
- {
- for (int j = 0; j <= m_sGridSize.height; ++j)
- {
- // Get original vertex
- ccVertex3F p = originalVertex(ccp(i ,j),m_pBgSprite);
- float x = (p.y - b) / -tanf(theta);
- float pivotX = p.x + (x - p.x) * sinf(theta) * sinf(theta);
- float pivotY = pivotX * -tanf(theta) + b;
- float l = (pivotX - p.x) / sinf(theta);
- float alpha = l / R;
- if (l >= 0) {
- if (alpha > M_PI) {
- p.x = (GLfloat)(mHOffsetX + pivotX + R * (alpha - M_PI) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY + R * (alpha - M_PI) * cosf(theta));
- p.z = (GLfloat)(2 * R / 9);
- }
- else if (alpha <= M_PI)
- {
- p.x = (GLfloat)(mHOffsetX + pivotX - R * sinf(alpha) * sinf(theta));
- p.y = (GLfloat)(mHOffsetY + pivotY - R * sinf(alpha) * cosf(theta));
- p.z = (GLfloat)((R - R * cosf(alpha)) / 9);
- }
- }
- else
- {
- p.x += mHOffsetX;
- p.y += mHOffsetY;
- }
- setVertex(ccp(i, j), p,m_pBgSprite);
- }
- }
- }
- // float R2 = 50;
- // float offsetX2 = mTouchBegin.x - pTouch->getLocation().x;
- // float pivotX2 = m_pForeSpriteVertical->getContentSize().height - offsetX2;
- //
- //
- // for (int i = 0; i <= m_sGridSize.width; ++i)
- // {
- // for (int j = 0; j <= m_sGridSize.height; ++j)
- // {
- // // Get original vertex
- // ccVertex3F p = originalVertex(ccp(i ,j),m_pForeSpriteVertical);
- // float l = p.x - pivotX2;
- // float alpha = l / R2;
- // if (l >= 0) {
- // if (alpha > M_PI) {
- // p.x = mVOffsetX + pivotX2 - R2 * (alpha - M_PI);
- // p.z = 2 * R2 / 9;
- // p.y = p.y + mVOffsetY;
- // }
- // else if (alpha <= M_PI)
- // {
- // p.x = mVOffsetX + pivotX2 + R2 * sinf(alpha);
- // p.z = (R2 - R2 * cosf(alpha))/9;
- // p.y = p.y + mVOffsetY;
- // }
- // }
- // else
- // {
- // p.x = p.x + mVOffsetX;
- // p.y = p.y + mVOffsetY;
- // }
- //
- //
- // // Set new coords
- // setVertex(ccp(i, j), p,m_pForeSpriteVertical);
- //
- //
- // }
- // }
- //
- // for (int i = 0; i <= m_sGridSize.width; ++i)
- // {
- // for (int j = 0; j <= m_sGridSize.height; ++j)
- // {
- // // Get original vertex
- // ccVertex3F p = originalVertex(ccp(i ,j),m_pBgSpriteVertical);
- // float l = p.x - pivotX2;
- // float alpha = l / R2;
- // if (l >= 0) {
- // if (alpha > M_PI) {
- // p.x = mVOffsetX + pivotX2 - R2 * (alpha - M_PI);
- // p.z = 2 * R2 / 9;
- // p.y = p.y + mVOffsetY;
- // }
- // else if (alpha <= M_PI)
- // {
- // p.x = mVOffsetX + pivotX2 + R2 * sinf(alpha);
- // p.z = (R2 - R2 * cosf(alpha))/9;
- // p.y = p.y + mVOffsetY;
- // }
- // }
- // else
- // {
- // p.x = p.x + mVOffsetX;
- // p.y = p.y + mVOffsetY;
- // }
- //
- // // Set new coords
- // setVertex(ccp(i, j), p,m_pBgSpriteVertical);
- //
- //
- //
- // }
- // }
- }
- void PageTurn2::calculateVerticalVertexPoints(float offsetY)
- {
- float R2 = 50;
- float pivotY = offsetY + mVOffsetY;
- for (int i = 0; i <= m_sGridSize.width; ++i)
- {
- for (int j = 0; j <= m_sGridSize.height; ++j)
- {
- // Get original vertex
- ccVertex3F p = originalVertex(ccp(i ,j),m_pForeSpriteVertical);
- float l = pivotY - p.y;
- float alpha = l / R2;
- if (l >= 0) {
- if (alpha > M_PI) {
- p.z = (GLfloat)(2 * R2 / 9);
- p.y = (GLfloat)(mVOffsetY + pivotY + R2 * (alpha - M_PI));
- p.x += mVOffsetX;
- }
- else if (alpha <= M_PI)
- {
- p.z = (GLfloat)((R2 - R2 * cosf(alpha))/9);
- p.y = (GLfloat)(mVOffsetY + pivotY - R2 * sinf(alpha));
- p.x += mVOffsetX;
- }
- }
- else
- {
- p.x += mVOffsetX;
- p.y += mVOffsetY;
- }
- // Set new coords
- setVertex(ccp(i, j), p,m_pForeSpriteVertical);
- }
- }
- for (int i = 0; i <= m_sGridSize.width; ++i)
- {
- for (int j = 0; j <= m_sGridSize.height; ++j)
- {
- // Get original vertex
- ccVertex3F p = originalVertex(ccp(i ,j),m_pBgSpriteVertical);
- float l = pivotY - p.y;
- float alpha = l / R2;
- if (l >= 0) {
- if (alpha > M_PI) {
- p.z = (GLfloat)(2 * R2 / 9);
- p.y = (GLfloat)(mVOffsetY + pivotY + R2 * (alpha - M_PI));
- p.x += mVOffsetX;
- }
- else if (alpha <= M_PI)
- {
- p.z = (GLfloat)((R2 - R2 * cosf(alpha))/9);
- p.y = (GLfloat)(mVOffsetY + pivotY - R2 * sinf(alpha));
- p.x += mVOffsetX;
- }
- }
- else
- {
- p.x += mVOffsetX;
- p.y += mVOffsetY;
- } // Set new coords
- setVertex(ccp(i, j), p,m_pBgSpriteVertical);
- }
- }
- }
- void PageTurn2::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- }
- void PageTurn2::OnEnterEvn(CCNode*) // 进入动画结束执行的事件
- {
- }
- void PageTurn2::OnExitEvn(CCNode*) // 退出动画结束执行的事件
- {
- removeFromParent();
- }
- // 返回
- void PageTurn2::OnClickBack(CCObject* pObject)
- {
- NtfPlayClose();//关闭的音效
- removeFromParent();
- }
- void PageTurn2::OnClickHorizontal(cocos2d::CCObject *pObject)
- {
- NtfPlayDefBtnSnd();
- if (!mbIsFliped) {
- if (mCardShowType != Horizontal && mCardShowType == VerticalE) {
- mFlipDemoOffset = 0;
- schedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- mCardShowType = Horizontal;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(true);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(true);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(false);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(false);
- m_pForeSprite->setRotation(0);
- m_pForeSpriteMirror->setRotation(0);
- m_pBgSprite->setRotation(0);
- m_pBgSpriteMirror->setRotation(0);
- }
- else if (mCardShowType == Horizontal)
- {
- mFlipDemoOffset = 0;
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- mCardShowType = VerticalA;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- m_pForeSpriteVertical->setRotation(-90);
- m_pForeSpriteMirrorVertical->setRotation(-90);
- m_pBgSpriteVertical->setRotation(-90);
- m_pBgSpriteMirrorVertical->setRotation(-90);
- }
- else if (mCardShowType == VerticalA)
- {
- mFlipDemoOffset = 0;
- schedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- mCardShowType = Horizontal2;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(true);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(true);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(false);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(false);
- m_pForeSprite->setRotation(180);
- m_pForeSpriteMirror->setRotation(180);
- m_pBgSprite->setRotation(180);
- m_pBgSpriteMirror->setRotation(180);
- }
- else if(mCardShowType == Horizontal2)
- {
- mFlipDemoOffset = 0;
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- mCardShowType = VerticalE;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- m_pForeSpriteVertical->setRotation(90);
- m_pForeSpriteMirrorVertical->setRotation(90);
- m_pBgSpriteVertical->setRotation(90);
- m_pBgSpriteMirrorVertical->setRotation(90);
- }
- }
- }
- void PageTurn2::OnClickVerticalA(CCObject* pObject)
- {
- NtfPlayDefBtnSnd();
- if (!mbIsFliped) {
- if (mCardShowType != VerticalA) {
- mCardShowType = VerticalA;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- m_pForeSpriteVertical->setRotation(-90);
- m_pForeSpriteMirrorVertical->setRotation(-90);
- m_pBgSpriteVertical->setRotation(-90);
- m_pBgSpriteMirrorVertical->setRotation(-90);
- // if (pHorizontalItem && pVerticalAItem && pVerticalEItem)
- // {
- // pHorizontalItem->unselected();
- // pVerticalAItem->selected();
- // pVerticalEItem->unselected();
- // }
- }
- }
- }
- void PageTurn2::OnClickVerticalE(CCObject* pObject)
- {
- NtfPlayDefBtnSnd();
- if (!mbIsFliped) {
- if (mCardShowType != VerticalE) {
- mCardShowType = VerticalE;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- m_pForeSpriteVertical->setRotation(90);
- m_pForeSpriteMirrorVertical->setRotation(90);
- m_pBgSpriteVertical->setRotation(90);
- m_pBgSpriteMirrorVertical->setRotation(90);
- // if (pHorizontalItem && pVerticalAItem && pVerticalEItem)
- // {
- // pHorizontalItem->unselected();
- // pVerticalAItem->unselected();
- // pVerticalEItem->selected();
- // }
- }
- }
- }
- void PageTurn2::OnClickFlip(cocos2d::CCObject *pObject)
- {
- NtfPlayDefBtnSnd();
- if (!mbIsFliped) {
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- judgeIsFlipCard(winSize.width/2 + 300,mCardShowType,true);
- mCurIndex = 3;
- for (int i = 0; i < sizeof(pos) / sizeof(SuitPosInfo); i++) {
- CCNode* pNode = (CCNode*)getChildByTag(i);
- if (pNode) {
- pNode->removeFromParentAndCleanup(true);
- String imgName = GetCardFace(mPickAry[i].mGrp, mPickAry[i].mNum);
- CCSprite* sprite = NtfCreateSprite(imgName.c_str());
- sprite->setPosition(ccp(pos[i].fx, pos[i].fy));
- this->addChild(sprite,1,i);
- }
- }
- }
- }
- CCSprite* PageTurn2::createPokerCard(CCrdGrp type, CCrdNum num, bool isShowNum)
- {
- CCSize size = CCSizeMake(778, 521);
- //牌类型图标的位置信息
- static SuitPosInfo s_spi1[] = {{389,260,-90}};
- static SuitPosInfo s_spi2[] = {{165,260,-90},{615,260,90}};
- static SuitPosInfo s_spi3[] = {{165,260,-90},{615,260,90},{389,260,-90}};
- static SuitPosInfo s_spi4[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90}};
- static SuitPosInfo s_spi5[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90},{389,260,-90}};
- static SuitPosInfo s_spi6[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90},{389,400,90},{409,125,90}};
- static SuitPosInfo s_spi7[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90},{389,400,-90},{409,125,-90},{290,260,-90}};
- static SuitPosInfo s_spi8[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90},{389,400,90},{409,125,90},{290,260,-90},{503,260,90}};
- static SuitPosInfo s_spi9[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90},{475,400,90},{475,125,90},{308,400,-90},{308,125,-90},{389,260,-90}};
- static SuitPosInfo s_spi10[] = {{165,400,-90},{165,125,-90},{620,400,90},{620,125,90},{475,400,90},{475,125,90},{308,400,-90},{308,125,-90},{250,260,-90},{542,260,90}};
- static SuitPosInfo s_spiS[] = {{389,260,-90},{399,260,90},{177,135,-90},{596,385,90}};
- //牌数字图标的位置信息
- static SuitPosInfo s_spiNumPos[] = {{100,42,-90},{680,480,90}};
- std::string imageName;
- std::string numImgName;
- int nCardNum;
- CCSprite* pForeBg = NtfCreateSprite("Cards/card_fore.png");
- switch (type) {
- case CRDG_SPADE:
- imageName = "Cards/spade.png";
- numImgName = "Cards/spade_";
- break;
- case CRDG_HEART:
- imageName = "Cards/heart.png";
- numImgName = "Cards/heart_";
- break;
- case CRDG_CLUB:
- imageName = "Cards/club.png";
- numImgName = "Cards/club_";
- break;
- case CRDG_DIAMOND:
- imageName = "Cards/diamond.png";
- numImgName = "Cards/diamond_";
- break;
- default:
- break;
- }
- String centerImgName;
- //添加牌型ICON
- switch (num) {
- case CRDN_A:
- nCardNum = 1;
- if (type == CRDG_SPADE) {
- imageName = "Cards/spade_a_center.png";
- }
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi1[i].fx,s_spi1[i].fy));
- pSprite->setRotation(s_spi1[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("a.png");
- break;
- case CRDN_N2:
- nCardNum = 2;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi2[i].fx,s_spi2[i].fy));
- pSprite->setRotation(s_spi2[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("2.png");
- break;
- case CRDN_N3:
- nCardNum = 3;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi3[i].fx,s_spi3[i].fy));
- pSprite->setRotation(s_spi3[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("3.png");
- break;
- case CRDN_N4:
- nCardNum = 4;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi4[i].fx,s_spi4[i].fy));
- pSprite->setRotation(s_spi4[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("4.png");
- break;
- case CRDN_N5:
- nCardNum = 5;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi5[i].fx,s_spi5[i].fy));
- pSprite->setRotation(s_spi5[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("5.png");
- break;
- case CRDN_N6:
- nCardNum = 6;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi6[i].fx,s_spi6[i].fy));
- pSprite->setRotation(s_spi6[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("6.png");
- break;
- case CRDN_N7:
- nCardNum = 7;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi7[i].fx,s_spi7[i].fy));
- pSprite->setRotation(s_spi7[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("7.png");
- break;
- case CRDN_N8:
- nCardNum = 8;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi8[i].fx,s_spi8[i].fy));
- pSprite->setRotation(s_spi8[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("8.png");
- break;
- case CRDN_N9:
- nCardNum = 9;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi9[i].fx,s_spi9[i].fy));
- pSprite->setRotation(s_spi9[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("9.png");
- break;
- case CRDN_N10:
- nCardNum = 10;
- for (int i = 0; i < nCardNum; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spi10[i].fx,s_spi10[i].fy));
- pSprite->setRotation(s_spi10[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("10.png");
- break;
- case CRDN_J:
- nCardNum = 11;
- if (type == CRDG_HEART || type == CRDG_DIAMOND) {
- centerImgName = "Cards/red_j.png";
- }
- else if(type == CRDG_SPADE || type == CRDG_CLUB)
- {
- centerImgName = "Cards/black_j.png";
- }
- for (int i = 0; i < 1; i++) {
- CCSprite* pSprite = NtfCreateSprite(centerImgName.c_str());
- if (i == 0) {
- pSprite->setAnchorPoint(ccp(0.5, 0.5));
- }
- else
- {
- pSprite->setAnchorPoint(ccp(0.5, 0));
- }
- pSprite->setPosition(ccp(s_spiS[i].fx,s_spiS[i].fy));
- pSprite->setRotation(s_spiS[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- for (int i = 2; i < 4; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spiS[i].fx,s_spiS[i].fy));
- pSprite->setRotation(s_spiS[i].fRotate);
- pSprite->setScale(0.8f);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("j.png");
- break;
- case CRDN_Q:
- nCardNum = 12;
- if (type == CRDG_HEART || type == CRDG_DIAMOND) {
- centerImgName = "Cards/red_q.png";
- }
- else if(type == CRDG_SPADE || type == CRDG_CLUB)
- {
- centerImgName = "Cards/black_q.png";
- }
- for (int i = 0; i < 1; i++) {
- CCSprite* pSprite = NtfCreateSprite(centerImgName.c_str());
- if (i == 0) {
- pSprite->setAnchorPoint(ccp(0.5, 0.5));
- }
- else
- {
- pSprite->setAnchorPoint(ccp(0.5, 0));
- }
- pSprite->setPosition(ccp(s_spiS[i].fx,s_spiS[i].fy));
- pSprite->setRotation(s_spiS[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- for (int i = 2; i < 4; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spiS[i].fx,s_spiS[i].fy));
- pSprite->setRotation(s_spiS[i].fRotate);
- pSprite->setScale(0.8f);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("q.png");
- break;
- case CRDN_K:
- nCardNum = 13;
- if (type == CRDG_HEART || type == CRDG_DIAMOND) {
- centerImgName = "Cards/red_k.png";
- }
- else if(type == CRDG_SPADE || type == CRDG_CLUB)
- {
- centerImgName = "Cards/black_k.png";
- }
- for (int i = 0; i < 1; i++) {
- CCSprite* pSprite = NtfCreateSprite(centerImgName.c_str());
- if (i == 0) {
- pSprite->setAnchorPoint(ccp(0.5, 0.5));
- }
- else
- {
- pSprite->setAnchorPoint(ccp(0.5, 0));
- }
- pSprite->setPosition(ccp(s_spiS[i].fx,s_spiS[i].fy));
- pSprite->setRotation(s_spiS[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- for (int i = 2; i < 4; i++) {
- CCSprite* pSprite = NtfCreateSprite(imageName.c_str());
- pSprite->setPosition(ccp(s_spiS[i].fx,s_spiS[i].fy));
- pSprite->setRotation(s_spiS[i].fRotate);
- pSprite->setScale(0.8f);
- pForeBg->addChild(pSprite);
- }
- numImgName.append("k.png");
- break;
- default:
- break;
- }
- //添加牌型数字
- if (isShowNum) {
- for (int i = 0; i < 2; i++) {
- CCSprite* pSprite = NtfCreateSprite(numImgName.c_str());
- pSprite->setPosition(ccp(s_spiNumPos[i].fx,s_spiNumPos[i].fy));
- pSprite->setRotation(s_spiNumPos[i].fRotate);
- pForeBg->addChild(pSprite);
- }
- }
- return pForeBg;
- }
- void PageTurn2::onEventDone(int event, void *data)
- {
- if (event == EventTurnCard) {
- CTurnCrd* pTurnCrd = static_cast<CTurnCrd*>(data);
- if (pTurnCrd) {
- if (pTurnCrd->mRubAc != g_pSelf->GetValue(RVT_ACC_ID) && !mbIsFliped) {
- if (pTurnCrd->mPlace == Horizontal)
- {
- mCardShowType = Horizontal;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(true);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(true);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(false);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(false);
- if (!m_pBgSprite->isVisible()) {
- m_pBgSprite->setVisible(true);
- m_pBgSpriteMirror->setVisible(false);
- }
- if (!m_pForeSprite->isVisible()) {
- m_pForeSprite->setVisible(true);
- m_pForeSpriteMirror->setVisible(false);
- }
- calculateHorizontalVertexPoints(pTurnCrd->mOffset / cosf(M_PI / 4),true);
- judgeIsFlipCard(pTurnCrd->mOffset / cosf(M_PI / 4),mCardShowType,false);
- }
- else
- {
- mCardShowType = VerticalA;
- m_pForeSprite->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSprite->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- m_pForeSpriteVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(true);
- m_pBgSpriteVertical->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(true);
- if (!m_pBgSpriteVertical->isVisible()) {
- m_pBgSpriteVertical->setVisible(true);
- m_pBgSpriteMirrorVertical->setVisible(false);
- }
- if (!m_pForeSpriteVertical->isVisible()) {
- m_pForeSpriteVertical->setVisible(true);
- m_pForeSpriteMirrorVertical->setVisible(false);
- }
- calculateVerticalVertexPoints(pTurnCrd->mOffset / cosf(M_PI / 4));
- judgeIsFlipCard(pTurnCrd->mOffset / cosf(M_PI / 4),mCardShowType,false);
- }
- }
- }
- }
- else if (event == EventOpenAddTableView)
- {
- CCTouchDispatcher* td = CCDirector::sharedDirector()->getTouchDispatcher();
- if (mpMenu) {
- td->setPriority(10000, mpMenu);
- }
- if (mpHideBtn) {
- td->setPriority(10000, mpHideBtn);
- }
- }
- else if (event == EventCloseAddTableView)
- {
- CCTouchDispatcher* td = CCDirector::sharedDirector()->getTouchDispatcher();
- if (mpMenu) {
- td->setPriority(0, mpMenu);
- }
- if (mpHideBtn) {
- td->setPriority(0, mpHideBtn);
- }
- }
- else if(event == EventUpdateAvatar)
- {
- EVENT_UPDATEAVATAR_DATA* peud = (EVENT_UPDATEAVATAR_DATA*)data;
- if (!peud) {
- return;
- }
- if (peud->nAccId == g_pSelf->GetValue(RVT_ACC_ID)) {
- CCNode* pAvatar = this->getChildByTag(TAG_AVATAR);
- if (pAvatar) {
- pAvatar->removeFromParent();
- }
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- CCSprite* sprite = CCSprite::createWithTexture(peud->texture);
- CCClippingNode* clipA = NtfSetHeadIcon(sprite, CCSize(56, 56));
- clipA->setAnchorPoint(ccp(0.5, 0.5));
- clipA->setPosition(ccp(60, winSize.height - 118));
- this->addChild(clipA, 30,TAG_AVATAR);
- }
- }
- }
- void PageTurn2::onFlipAnimFinished(cocos2d::CCNode *pNode)
- {
- // CMsgTurnCrd::PostToSever(mBacId,g_pSelf->GetValue(RVT_ACC_ID), mCurType, mCurNum, TurnOver, 0);
- //
- // mCurIndex++;
- if (mCurIndex < mPickAry.size()) {
- CCNode* node = (CCNode*)getChildByTag(mCurIndex);
- if (node) {
- node->removeFromParentAndCleanup(true);
- cocos2d::extension::CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("BuilderAnimate/fanpaidonghuazuixin0.png", "BuilderAnimate/fanpaidonghuazuixin0.plist", "BuilderAnimate/fanpaidonghuazuixin.ExportJson");
- CCArmature* armatureTriangle = cocos2d::extension::CCArmature::create("fanpaidonghuazuixin");
- armatureTriangle->setAnchorPoint(ccp(0.5, 0.5));
- armatureTriangle->setPosition(ccp(pos[mCurIndex].fx, pos[mCurIndex].fy));
- armatureTriangle->getAnimation()->playWithIndex(0);
- this->addChild(armatureTriangle,1,mCurIndex);
- }
- if (m_pForeSprite) {
- m_pForeSprite->removeFromParentAndCleanup(true);
- }
- if (m_pForeSpriteMirror) {
- m_pForeSpriteMirror->removeFromParentAndCleanup(true);
- }
- if (m_pBgSprite) {
- m_pBgSprite->removeFromParentAndCleanup(true);
- }
- if (m_pBgSpriteMirror) {
- m_pBgSpriteMirror->removeFromParentAndCleanup(true);
- }
- if (m_pForeSpriteVertical) {
- m_pForeSpriteVertical->removeFromParentAndCleanup(true);
- }
- if (m_pForeSpriteMirrorVertical) {
- m_pForeSpriteMirrorVertical->removeFromParentAndCleanup(true);
- }
- if (m_pBgSpriteVertical) {
- m_pBgSpriteVertical->removeFromParentAndCleanup(true);
- }
- if (m_pBgSpriteMirrorVertical) {
- m_pBgSpriteMirrorVertical->removeFromParentAndCleanup(true);
- }
- if (m_pShowPokerCard) {
- m_pShowPokerCard->removeFromParentAndCleanup(true);
- }
- mCardShowType = Horizontal;
- createPokerSprite(mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum);
- mFlipDemoOffset = 0;
- schedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- mbIsFliped = false;
- }
- else
- {
- g_stage->mFlipProgress[mTableId] = 0;
- removeFromParentAndCleanup(true);
- EventManager::sharedEventManager()->notifyEventFinished(EventShowLastThreeCard);
- }
- }
- void PageTurn2::judgeIsFlipCard(float offset,CardShowType showType,bool flag)
- {
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- //判断是否已经翻牌了
- if ((((showType == Horizontal || showType == Horizontal2) && offset > (winSize.width/2 + 250)) || ((showType == VerticalA || showType == VerticalE) && offset > winSize.height/2 + 250)) && !mbIsFliped) {
- mbIsFliped = true;
- unschedule(schedule_selector(PageTurn2::onFlipDemoUpdate));
- m_pBgSprite->setVisible(false);
- m_pForeSprite->setVisible(false);
- m_pBgSpriteVertical->setVisible(false);
- m_pForeSpriteVertical->setVisible(false);
- m_pBgSpriteMirror->setVisible(false);
- m_pForeSpriteMirror->setVisible(false);
- m_pBgSpriteMirrorVertical->setVisible(false);
- m_pForeSpriteMirrorVertical->setVisible(false);
- //添加盘牌动画
- m_pShowPokerCard = createPokerCard(mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum,true);
- m_pShowPokerCard->setAnchorPoint(ccp(0.5, 0.5));
- m_pShowPokerCard->setPosition(ccp(winSize.width/2, winSize.height/2));
- this->addChild(m_pShowPokerCard,2);
- m_pShowPokerCard->setScale(2.0f);
- CCScaleTo* scale = CCScaleTo::create(0.1f, 1.0f);
- CCDelayTime* delay = CCDelayTime::create(2.0f);
- CCArray* ary = CCArray::create();
- ary->addObject(scale);
- ary->addObject(delay);
- if (flag) {
- SEL_CallFuncN func = callfuncN_selector(PageTurn2::onFlipAnimFinished);
- ary->addObject(CCCallFuncN::create(this, func));
- }
- CCSequence* action = CCSequence::create(ary);
- m_pShowPokerCard->runAction(action);
- String imgName = GetCardFace(mPickAry[mCurIndex].mGrp, mPickAry[mCurIndex].mNum);
- CCNode* node = (CCNode*)getChildByTag(mCurIndex);
- if (node) {
- node->removeFromParentAndCleanup(true);
- CCSprite* sprite = NtfCreateSprite(imgName.c_str());
- sprite->setPosition(ccp(pos[mCurIndex].fx,pos[mCurIndex].fy));
- this->addChild(sprite,1,mCurIndex);
- }
- mCurIndex++;
- g_stage->mFlipProgress[mTableId] = mCurIndex;
- }
- }
- //--------------------------------------------------------------------------------
- bool PageTurn2::OnTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
- {
- ccTouchBegan(pTouch, pEvent);
- TouchLyr::iterator it = mTouchLyr.begin();
- while(it != mTouchLyr.end())
- {
- CRayLayer* vLyr = *it;
- if(vLyr->OnTouchBegan(pTouch, pEvent))
- {
- mCurLyr = vLyr;
- break;
- }
- ++it;
- }
- return true;
- }
- //--------------------------------------------------------------------------------
- bool PageTurn2::OnTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
- {
- ccTouchMoved(pTouch, pEvent);
- if (mCurLyr)
- {
- mCurLyr->OnTouchMoved(pTouch, pEvent);
- }
- return true;
- }
- //--------------------------------------------------------------------------------
- bool PageTurn2::OnTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
- {
- ccTouchEnded(pTouch, pEvent);
- if (mCurLyr)
- {
- mCurLyr->OnTouchEnded(pTouch, pEvent);
- }
- mCurLyr = NULL;
- return true;
- }
float deltaAy = (tt * tt *500);
float ay = -100 - deltaAy;
float deltaTheta = - (float)M_PI_2 *sqrtf( time) ;
float theta =/*0.01f */ + (float)M_PI_2 +deltaTheta;
float sinTheta =sinf(theta);
float cosTheta =cosf(theta);
for (int i =0; i <=m_sGridSize.width; ++i)
{
for (int j =0; j <=m_sGridSize.height; ++j)
{
// Get original vertex
ccVertex3F p = originalVertex(ccp(i ,j));
float R = sqrtf((p.x * p.x) + ((p.y - ay) * (p.y - ay)));
float r = R * sinTheta;
float alpha = asinf( p.x / R );
float beta = alpha / sinTheta;
float cosBeta = cosf( beta );
// If beta > PI then we've wrapped around the cone
// Reduce the radius to stop these points interfering with others
if (beta <= M_PI)
{
p.x = ( r *sinf(beta));
}
else
{
// Force X = 0 to stop wrapped
// points
p.x =0;
}
p.y = ( R + ay - ( r * (1 - cosBeta) * sinTheta));
// We scale z here to avoid the animation being
// too much bigger than the screen due to perspective transform
p.z = (r * (1 - cosBeta ) * cosTheta) /7;// "100" didn't work for
// Stop z coord from dropping beneath underlying page in a transition
// issue #751
if( p.z < 0.5f )
{
p.z =0.5f;
}
// Set new coords
setVertex(ccp(i, j), p);
}
}
}
刚开始看这个,完全不知所云,一堆三角函数, ,简直了,后来找了好久,找到一篇论文 正好把源码解释得一清二楚,论文的链接为:http://www.parc.com/content/attachments/turning-pages-3D.pdf |
看完之后才明白,原来是建立了一个数学模型,通过数学模型去计算每个顶点在变换过程中的位置,明白了。到现在才理解大学时老师说的那句话,数学对编程来说很重要。
回到主题,我们要实现的是类似翻书的效果,通过上面的算法,得到的效果和我的需求有点差异,既然他是想象成一个锥体的运到,我们可以想象成一个圆柱体的运到,通过改变圆柱体轴心的位置,来实现翻动的效果,不多说了,直接上源码:
voidPageTurn::calculateHorizontalVertexPoints(float offsetX)
{
float theta = (GLfloat)(M_PI /6.0f);
float R =50;
float b = (m_pBgSprite->getContentSize().width - offsetX *1.4f) * sinf(theta);
for (int i =0; i <=m_sGridSize.width; ++i)
{
for (int j =0; j <=m_sGridSize.height; ++j)
{
// Get original vertex
ccVertex3F p =originalVertex(ccp(i ,j),m_pForeSprite);
float x = (p.y + b) / tanf(theta);
float pivotX = x + (p.x - x) * cosf(theta) * cosf(theta);
float pivotY = pivotX * tanf(theta) - b;
float l = (p.x - pivotX) / sinf(theta);
float alpha = l / R;
if (l >= 0) {
if (alpha > M_PI) {
p.x = (GLfloat)(mHOffsetX + pivotX - R * (alpha -M_PI) * sinf(theta));
p.y = (GLfloat)(mHOffsetY + pivotY + R * (alpha -M_PI) * cosf(theta));
p.z = (GLfloat)(2 * R /9);
}
else if (alpha <= M_PI)
{
p.x = (GLfloat)(mHOffsetX + pivotX + R *sinf(alpha) *sinf(theta));
p.y = (GLfloat)(mHOffsetY + pivotY - R *sinf(alpha) *cosf(theta));
p.z = (GLfloat)((R - R *cosf(alpha))/9);
}
}
else
{
p.x +=mHOffsetX;
p.y +=mHOffsetY;
}
// Set new coords
setVertex(ccp(i, j), p,m_pForeSprite);
}
}
for (int i =0; i <=m_sGridSize.width; ++i)
{
for (int j =0; j <=m_sGridSize.height; ++j)
{
// Get original vertex
ccVertex3F p =originalVertex(ccp(i ,j),m_pBgSprite);
float x = (p.y + b) / tanf(theta);
float pivotX = x + (p.x - x) * cosf(theta) * cosf(theta);
float pivotY = pivotX * tanf(theta) - b;
float l = (p.x - pivotX) / sinf(theta);
float alpha = l / R;
if (l >= 0) {
if (alpha > M_PI) {
p.x = (GLfloat)(mHOffsetX + pivotX - R * (alpha -M_PI) * sinf(theta));
p.y = (GLfloat)(mHOffsetY + pivotY + R * (alpha -M_PI) * cosf(theta));
p.z = (GLfloat)(2 * R /9);
}
else if (alpha <= M_PI)
{
p.x = (GLfloat)(mHOffsetX + pivotX + R *sinf(alpha) *sinf(theta));
p.y = (GLfloat)(mHOffsetY + pivotY - R *sinf(alpha) *cosf(theta));
p.z = (GLfloat)((R - R *cosf(alpha))/9);
}
}
else
{
p.x +=mHOffsetX;
p.y +=mHOffsetY;
}
setVertex(ccp(i, j), p,m_pBgSprite);
}
}
// float R2 = 50;
// float offsetX2 = mTouchBegin.x - pTouch->getLocation().x;
// float pivotX2 = m_pForeSpriteVertical->getContentSize().height - offsetX2;
//
//
// for (int i = 0; i <= m_sGridSize.width; ++i)
// {
// for (int j = 0; j <= m_sGridSize.height; ++j)
// {
// // Get original vertex
// ccVertex3F p = originalVertex(ccp(i ,j),m_pForeSpriteVertical);
// float l = p.x - pivotX2;
// float alpha = l / R2;
// if (l >= 0) {
// if (alpha > M_PI) {
// p.x = mVOffsetX + pivotX2 - R2 * (alpha - M_PI);
// p.z = 2 * R2 / 9;
// p.y = p.y + mVOffsetY;
// }
// else if (alpha <= M_PI)
// {
// p.x = mVOffsetX + pivotX2 + R2 * sinf(alpha);
// p.z = (R2 - R2 * cosf(alpha))/9;
// p.y = p.y + mVOffsetY;
// }
// }
// else
// {
// p.x = p.x + mVOffsetX;
// p.y = p.y + mVOffsetY;
// }
//
//
// // Set new coords
// setVertex(ccp(i, j), p,m_pForeSpriteVertical);
//
//
// }
// }
//
// for (int i = 0; i <= m_sGridSize.width; ++i)
// {
// for (int j = 0; j <= m_sGridSize.height; ++j)
// {
// // Get original vertex
// ccVertex3F p = originalVertex(ccp(i ,j),m_pBgSpriteVertical);
// float l = p.x - pivotX2;
// float alpha = l / R2;
// if (l >= 0) {
// if (alpha > M_PI) {
// p.x = mVOffsetX + pivotX2 - R2 * (alpha - M_PI);
// p.z = 2 * R2 / 9;
// p.y = p.y + mVOffsetY;
// }
// else if (alpha <= M_PI)
// {
// p.x = mVOffsetX + pivotX2 + R2 * sinf(alpha);
// p.z = (R2 - R2 * cosf(alpha))/9;
// p.y = p.y + mVOffsetY;
// }
// }
// else
// {
// p.x = p.x + mVOffsetX;
// p.y = p.y + mVOffsetY;
// }
//
// // Set new coords
// setVertex(ccp(i, j), p,m_pBgSpriteVertical);
//
//
//
// }
// }
}
3d节点的渲染我用的是CCGridBase的子类CCGrid3D,只需要把CCGrid3DAction中渲染的部分摘出来即可,代码在cocos中都有(补充下,我用的是cocos2dx2.2.6),以下是最终效果: