仔细看过老G博客中的内容后,总结出了几种类型动作。。所以编写了个小例子,至于网格动画...还有很多不能理解的地方,所以也总结不出来
http://4137613.blog.51cto.com/4127613/d-1/p-3 老G的博客
还是分步骤来:
1.在类的声明中声明各种需要用到的成员函数和成员变量:
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
// a selector callback
virtual void LJCallback(CCObject* pSender);
virtual void CXCallback(CCObject* pSender);
virtual void BJCallback(CCObject* pSender);
virtual void Initback(CCObject* pSender);
virtual void GZCallback(CCObject* pSender);
virtual void JDCallback(CCObject* pSender);
virtual void onExit();
// implement the "static node()" method manually
LAYER_NODE_FUNC(HelloWorld);
protected:
cocos2d::CCSprite* m_tamara;
cocos2d::CCProgressTimer *m_left;
cocos2d::CCSize s;
};
2. init()函数中把菜单实现,并且初始化各种变量...代码中有很多宏判断,是因为需要转换字符才能显示中文,后面会告诉大家方法...
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCMenuItemFont::setFontName("Thonburi");//设置菜单字体
CCMenuItemFont::setFontSize(25);//设置菜单字体大小
s = CCDirector::sharedDirector()->getWinSize();
std::string menuItemStr = "立即动画";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");//调用字符转换函数
#endif
CCMenuItem *shuenjian = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,
menu_selector(HelloWorld::LJCallback)); //创建菜单项,并绑定触发函数..
menuItemStr = "持续动画";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");
#endif
CCMenuItem* chixu = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,
menu_selector(HelloWorld::CXCallback));
menuItemStr = "补间动画";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");
#endif
CCMenuItem* bujian = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,
menu_selector(HelloWorld::BJCallback));
menuItemStr = "跟踪动画";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");
#endif
CCMenuItem* genzong = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,
menu_selector(HelloWorld::GZCallback));
menuItemStr = "进度动作";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");
#endif
CCMenuItem* jingdu = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,
menu_selector(HelloWorld::JDCallback));
menuItemStr = "初始化精灵";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");
#endif
CCMenuItem* InitItem = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,
menu_selector(HelloWorld::Initback));
CCMenu* pMenu = CCMenu::menuWithItems(shuenjian,chixu,bujian,genzong,jingdu,InitItem,NULL);//创建菜单
pMenu->alignItemsVertically();//设置菜单的显示为垂直列表
CC_BREAK_IF(! pMenu);
pMenu->setPosition(s.width/2+s.width/4, s.height/2);//设置菜单显示位置
this->addChild(pMenu, 1);//添加菜单进入
//初始化精灵
m_tamara = CCSprite::spriteWithFile("jl.png");
m_tamara->retain();
addChild(m_tamara,1);
m_tamara->setPosition(CCPointMake(s.width/2, s.height/2));
//初始化背景图片,这里用的图片是960X360..也就是两个屏幕宽
CCSprite* bg = CCSprite::spriteWithFile("bg_24.png");
bg->setPosition(ccp(s.width/2 + s.width/2, s.height/2));
this->addChild(bg,-1);
bRet = true;
} while (0);
return bRet;
}
3. 规范代码,养成好习惯,所以我们添加<Tools.h> 和 <Tools.cpp> 文件,把字符转换的处理函数写在里面:
--Tools.h
#ifndef _TOOLS_H_
#define _TOOLS_H_
#include "cocos2d.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "iconv.h"
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);
#endif
#endif
--Tools.cpp
#include "tools.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode,toCode);
if(iconvH == 0)
{
return -1;
}
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf;
memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
{
iconv_close(iconvH);
return -1;
}
gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}
#endif
4. 添加附加依赖项 ,不添加的话,会提示找不到的外部符号....
对工程右键-> 属性 -> 连接器 -> 输入 -> 附加依赖项 栏目->后面有个按钮,点击打开,换一行加入libiconv.lib,或者在最后空一格加上libiconv.lib也行
5. 对号入座的实现各个小功能的回调函数和退出的处理函数:
//立即动画,不需要时间,直接完成
void HelloWorld::LJCallback(CCObject* pSender)
{
CCActionInstant* Action1 = CCPlace::actionWithPosition(ccp(100,100));
m_tamara->runAction(Action1);
}
//持续动画
void HelloWorld::CXCallback(CCObject* pSender)
{
//创建一个同时动作,同时移动和旋转
CCFiniteTimeAction* actionsGo = CCSpawn::actions(
CCMoveTo::actionWithDuration(1.0f,ccp(0,0)),
CCRotateBy::actionWithDuration(1.0f,360),NULL);
//创建一个同时动作,同时移动和旋转
CCFiniteTimeAction* actionsBack = CCSpawn::actions(
CCMoveTo::actionWithDuration(1.0f, CCPointMake(s.width/2,s.height/2)),
CCRotateBy::actionWithDuration( 2, 720),NULL);
//创建一个组合动作,执行完第一个动作后执行下一个
CCFiniteTimeAction* All = CCSequence::actions(actionsGo,actionsBack,NULL);
//使用动作包装把组合动作变成永久循环执行..需要转换成CCActionInterval *
m_tamara->runAction(CCRepeatForever::actionWithAction((CCActionInterval*)All));
}
//补间动画
void HelloWorld::BJCallback(CCObject* pSender)
{
CCActionInterval* actionTo = CCRotateBy::actionWithDuration(4.0f,360);
m_tamara->runAction(CCEaseBackOut::actionWithAction(actionTo));
}
//跟踪动画
void HelloWorld::GZCallback(CCObject* pSender)
{
CCActionInterval* Moveto = CCMoveBy::actionWithDuration(3.0f,CCPointMake(s.width*2+100, 0));
CCActionInterval* MoveBack = Moveto->reverse();
CCFiniteTimeAction* All = CCSequence::actions(Moveto,MoveBack,NULL);
CCAction* rep = CCRepeatForever::actionWithAction((CCActionInterval*)All);//包装动作为永久循环
m_tamara->runAction(rep);//执行rep动作
this->runAction(CCFollow::actionWithTarget(m_tamara,CCRectMake(0,0,s.width*2, s.height)));//执行跟踪动作
}
//进度动作
void HelloWorld::JDCallback(CCObject* pSender)
{
CCProgressTo *to1 = CCProgressTo::actionWithDuration(2,100);
CCProgressTimer *left = CCProgressTimer::progressWithFile("jl.png");
left->setType(kCCProgressTimerTypeHorizontalBarLR);
addChild(left);
left->setPosition(CCPointMake(100,s.height/2));
left->runAction(CCRepeatForever::actionWithAction(to1));
}
//初始化精灵
void HelloWorld::Initback(CCObject* pSender)
{
m_tamara->stopAllActions();//停止所有动作
m_tamara->setRotation(0.0f);//设置旋转为0,即为没有旋转的状态
m_tamara->setPosition(ccp(s.width/2, s.height/2));//设置位置为中心
}
void HelloWorld::onExit()
{
m_tamara->release();
CCLayer::onExit();
}