• Cocos2d-x开发实例介绍帧动画使用


    以下我们通过一个实例介绍一下帧动画的使用。这个实比例如以下图所看到的,点击Gobutton開始播放动画,这时候播放button标题变为Stop,点击Stopbutton能够停止播放动画。


    以下我们再看看详细的程序代码,首先看一下看HelloWorldScene.h文件,它的代码例如以下:

    #ifndef __HELLOWORLD_SCENE_H__
    #define __HELLOWORLD_SCENE_H__
     
    #include "cocos2d.h"
     
    class HelloWorld : public cocos2d::Layer
    {
             bool isPlaying; //播放标识                                                                                                  ①
       cocos2d::Sprite* sprite;                                                                                                        ②
    public:
     
       static cocos2d::Scene* createScene();
       virtual bool init(); 
       
        voidOnAction(cocos2d::Ref* pSender);                                                                                       ③
       
       CREATE_FUNC(HelloWorld);
     
    };
     
    #endif // __HELLOWORLD_SCENE_H__

    第①行代码是声明一个布尔变量isPlaying,用来保存播放状态,true时候说明正在播放,false时候说明停止播放。第②行代码cocos2d::Sprite*sprite是声明一个精灵变量。

    第③行声明了一个函数,用来在选择不同菜单时候的回调。

    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;
    }

    上述代码第①行是创建Gobutton精灵,相应的第③行代码是创建Gobutton(菜单项)。代码第②行是创建Stopbutton精灵,相应的第④行代码是创建Stopbutton(菜单项)。

    在第⑤行代码是创建Go和Stop是两种状态切换的开关菜单项。第⑥行代码是设置开关菜单项的位置。

    HelloWorldScene的实现代码HelloWorldScene.ccp文件,当中HelloWorld::OnAction(Ref*pSender)函数代码例如以下:

    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实战 C++卷》现已上线,各大商店均已开售:

    京东:http://item.jd.com/11584534.html

    亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

    当当:http://product.dangdang.com/23606265.html

    互动出版网:http://product.china-pub.com/3770734

    《Cocos2d-x实战 C++卷》源代码及样章下载地址:

    源代码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

    样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

    欢迎关注智捷iOS课堂微信公共平台

  • 相关阅读:
    数据库性能优化摘录
    关于海量用户访问的通用技术架构的一些思考
    Rose建模示例
    ASP.NET2.0下实现分布式StateServer(状态服务器)
    Apache + Tomcat 实现简单集群
    StateServer 解决 Session过期和分布式保存Session
    ASP.NET动态修改浏览器title,keywords,descrtptions,csslink,meta,icon等
    用户登录体验(自动时间锁)
    百度ueditor一些使用技巧
    C# AJAX中弹出提示窗口
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6805642.html
Copyright © 2020-2023  润新知