下面咱们再看看具体的程序代码,首先看一下看HelloWorldScene.h文件,它的代码以下:php
[html] view plaincopyhtml
#ifndef __HELLOWORLD_SCENE_H__ 函数
#define __HELLOWORLD_SCENE_H__ 动画
#include "cocos2d.h" 网站
class HelloWorld : public cocos2d::Layer this
{ spa
bool isPlaying; //播放标识 ① .net
cocos2d::Sprite* sprite; ② orm
public: htm
static cocos2d::Scene* createScene();
virtual bool init();
voidOnAction(cocos2d::Ref* pSender); ③
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
第①行代码是声明一个布尔变量isPlaying,用来保存播放状态,true时候说明正在播放,false时候说明中止播放。第②行代码cocos2d::Sprite*sprite是声明一个精灵变量。第③行声明了一个函数,用来在选择不一样菜单时候的回调。
[html] view plaincopy
HelloWorldScene的实现代码HelloWorldScene.ccp文件,其中HelloWorld::init()函数代码以下:
bool HelloWorld::init()
{
if( !Layer::init() )
{
returnfalse;
}
SizevisibleSize = Director::getInstance()->getVisibleSize();
Pointorigin = Director::getInstance()->getVisibleOrigin();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("run.plist");
autobackground = Sprite::createWithSpriteFrameName("background.png");
background->setAnchorPoint(Point::ZERO);
this->addChild(background,0);
sprite= Sprite::createWithSpriteFrameName("h1.png");
sprite->setPosition(Point(visibleSize.width/2,visibleSize.height /2));
this->addChild(sprite);
isPlaying= false;
//toggle菜单
autogoSprite = Sprite::createWithSpriteFrameName("go.png"); ①
autostopSprite = Sprite::createWithSpriteFrameName("stop.png"); ②
autogoToggleMenuItem = MenuItemSprite::create(goSprite, goSprite); ③
auto stopToggleMenuItem = MenuItemSprite::create(stopSprite,stopSprite); ④
auto toggleMenuItem = MenuItemToggle::createWithCallback(
CC_CALLBACK_1(HelloWorld::OnAction,this),
goToggleMenuItem , stopToggleMenuItem, NULL); ⑤
toggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(930,540))); ⑥
auto mn = Menu::create(toggleMenuItem, NULL);
mn->setPosition(Point::ZERO);
this->addChild(mn);
returntrue;
}
上述代码第①行是建立Go按钮精灵,对应的第③行代码是建立Go按钮(菜单项)。代码第②行是建立Stop按钮精灵,对应的第④行代码是建立Stop按钮(菜单项)。在第⑤行代码是建立Go和Stop是两种状态切换的开关菜单项。第⑥行代码是设置开关菜单项的位置。
HelloWorldScene的实现代码HelloWorldScene.ccp文件,其中HelloWorld::OnAction(Ref*pSender)函数代码以下:
[html] view plaincopy
void HelloWorld::OnAction(Ref* pSender)
{
if(!isPlaying) {
///////////////动画开始//////////////////////
Animation*animation = Animation::create(); ①
for(int i=1; i<= 4; i++)
{
__String*frameName = __String::createWithFormat("h%d.png",i); ②
log("frameName= %s",frameName->getCString());
SpriteFrame*spriteFrame = SpriteFrameCache::getInstance()->
getSpriteFrameByName(frameName->getCString()); ③
animation->addSpriteFrame(spriteFrame); ④
}
animation->setDelayPerUnit(0.15f); //设置两个帧播放时间 ⑤
animation->setRestoreOriginalFrame(true); //动画执行后还原初始状态 ⑥
Animate*action = Animate::create(animation); ⑦
sprite->runAction(RepeatForever::create(action)); ⑧
//////////////////动画结束///////////////////
isPlaying= true;
}else {
sprite->stopAllActions(); ⑨
isPlaying= false;
}
}
上述第①行代码是建立一个Animation对象,它是动画对象,而后咱们要经过循环将各个帧图片放到Animation对象中。第②行是得到帧图片的文件名,String类型是Cocos2d-x字符串数据类型。第③行代码是经过帧名建立精灵帧对象,第④行代码把精灵帧对象添加到Animation对象中。
第⑤行代码是animation->setDelayPerUnit(0.15f)是设置两个帧播放时间,咱们这个动画播放是4帧。第⑥行代码animation->setRestoreOriginalFrame(true)是动画执行完成是否还原到初始状态。第⑦行代码是经过一个Animation对象建立Animate对象,第⑧行代码sprite->runAction(RepeatForever::create(action))是执行动画动做,无限循环方式。
第⑨行代码sprite->stopAllActions()中止全部的动做。
更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:25776038六、327403678