自己用 cocos2d-x封装了个等待的动画,代码如下:
#include <iostream> #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; using namespace std; class TTWaitting:public CCLayer { public: TTWaitting(); ~TTWaitting(); bool initWithBgImage(std::string strImageName); void startAnimation(); void stopAnimation(); static TTWaitting * createWithBgImage(std::string strImageName); public: CCSprite * m_bgSprite; };
#include "TTWaitting.h" TTWaitting::TTWaitting() { } TTWaitting::~TTWaitting() { } bool TTWaitting::initWithBgImage(std::string strImageName) { if (strImageName.empty() ) { return false; } m_bgSprite = CCSprite::create(strImageName.c_str()); m_bgSprite->setPosition(ccp( m_bgSprite->getContentSize().width * 0.5f, m_bgSprite->getContentSize().height * 0.5f )); this->addChild(m_bgSprite); //设置contentSize this->setContentSize(m_bgSprite->getContentSize()); //将锚点设置回0.5,0.5 this->setAnchorPoint(ccp(0.5f,0.5f)); return true; } void TTWaitting::startAnimation() { if (m_bgSprite) { m_bgSprite->stopAllActions(); CCRotateBy * rotateAct = CCRotateBy::create(2.5f, 360); CCRepeatForever * rotateRep = CCRepeatForever::create(rotateAct); m_bgSprite->runAction(rotateRep); } } void TTWaitting::stopAnimation() { if (m_bgSprite) { m_bgSprite->stopAllActions(); } } TTWaitting * TTWaitting::createWithBgImage(std::string strImageName) { if (strImageName.empty() ) { return NULL; } TTWaitting * pRet = new TTWaitting(); if (pRet != NULL && pRet->initWithBgImage(strImageName)) { pRet->autorelease(); } else { delete pRet; pRet = NULL; } return pRet; }
//显示 正在刷新的背景 if (m_waitingCtrl == NULL) { m_waitingCtrl = TTWaitting::createWithBgImage("loading.png"); m_waitingCtrl->setPosition(ccp( m_originPoint.x + 320/2 - m_waitingCtrl->getContentSize().width * 0.5f, this->getContentSize().height * 0.5f - m_waitingCtrl->getContentSize().height * 0.5f )); this->addChild(m_waitingCtrl, 1); } else { m_waitingCtrl->setVisible(true); } m_waitingCtrl->startAnimation();
//停止 动画,隐藏提示 if (m_waitingCtrl) { m_waitingCtrl->stopAnimation(); m_waitingCtrl->setVisible(false); }