Cocos2d-x 3.1.1 Learning Log 13 -- CocosStudio learning is a must-see

Source: Internet
Author: User

Cocos2d-x 3.1.1 Learning Log 13 -- CocosStudio learning is a must-see

Cocos Studio has been around for a long time, mainly because of bone animation. Currently, it seems that there are only two ways to play an animation in the Cocos2d-x:

The first method is to play a sequence Frame Animation. Each frame of the Animation is loaded into the cache and the Animation class is used for playback when playback is required. This method is simple and violent, when dealing with animation scenarios with low requirements for details, it is easy to do so. However, when the number of animation frames is slightly higher, a large number of images are required, which consumes a lot of resources.

Second: it is the Action class provided by the Cocos2d-x to play the animation, this animation is in the frame loop by adjusting the coordinates of each rendering to hit the animation effect, since the frame loop is refreshed once every 1/60 seconds, this will make the playing of the animation very smooth and does not require the resources of each frame of the image. The disadvantage of this scheme is that the animation node can only load one image resource. When we want to implement the following animation,

If you need to create multiple genie for single-slave code implementation and bind the coordination and association between the elves, it will be very troublesome.

Bone animation can be compatible with the advantages of the above two methods, but does not contain their disadvantages. So more and more companies are using Cocos Studio to make animations. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/release + vS/release/C28yzyc/Su7j2vc/Oqs/release + w7XEMy4ysObS/release + release/e5x/fAtq + release/release + examples + examples/CyrnTwyk8L3A + examples/examples + examples/wrzS1LywxuSw/LqstcTOxLz + examples/qtDCvai1xM/uxL + jrNPSu/samples + examples/LqsxL/Examples/ authorization + CqGhoaHP1tTaztLDx7/J0tS/qsq80LS0 + authorization + 7raostprc68jnz8kju1_vcd4kcjxwcmugy2xhc3m9 "brush: java; "> 1 # ifndef _ HERO_H _ 2 # define _ HERO_H _ 3 # include" cocos2d. h "4 # include" cocos-ext.h "5 # include" CocoStudio. h "6 USING_NS_CC; 7 using namespace cocostudio; 8 USING_NS_CC_EXT; 9 enum DIRECTION {LEFT, RIGHT, NONE}; 10 class Hero: public Sprite11 {12 public: 13 CREATE_FUNC (Hero ); 14 bool init (); 15 void runLeft (float dt); 16 void runRight (float dt); 17 void attack (); 18 void death (); 19 void stop (); 20 21 DIRECTION dir; 22 Size size; 23 Armature * armature; 24 bool isAniRunLeft; 25 bool isAniRunRight; 26}; 27 # endif

We initialize the animation in Hero's init function and call a stop function to load an animation at the moment:

 1 #include "Hero.h" 2  3 bool Hero::init() 4 { 5     Sprite::init(); 6     size = Director::getInstance()->getWinSize(); 7     ArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("Hero0.png", "Hero0.plist", "Hero.ExportJson"); 8     armature = Armature::create("Hero"); 9     armature->setScale(0.7f);10     armature->setPosition(Vec2(size.width / 2, size.height / 2));11     addChild(armature);12     stop();13     14     dir = NONE;15     isAniRunLeft = false;16     isAniRunRight = false;17     return true;18 }19 void Hero::stop()20 {21     armature->getAnimation()->play("loading");22 }23 void Hero::runLeft(float dt)24 {25     float dis = dt * 200;26     setPositionX(getPositionX() - dis);27 }28 void Hero::runRight(float dt)29 {30     float dis = dt * 200;31     setPositionX(getPositionX() + dis);32 }33 void Hero::attack()34 {35     armature->getAnimation()->play("attack");36 }37 void Hero::death()38 {39     armature->getAnimation()->play("death");40 }

Then we need a scenario class to let Hero work in this scenario:

 1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3  4 #include "cocos2d.h" 5 #include "cocos-ext.h" 6 #include "CocoStudio.h" 7 #include "Hero.h" 8 USING_NS_CC_EXT; 9 class menuDelegate10 {11 public:12     virtual void stopstate() = 0;13 };14 class Panel :public Menu15 {16 public:17     menuDelegate* exm;18     MenuItem* getSelectItem()19     {20         return _selectedItem;21     }22     static Panel* create()23     {24         Panel* ret = new Panel;25         ret->init();26         ret->autorelease();27         return ret;28     }29     bool init()30     {31         Menu::init();32         scheduleUpdate();33         return true;34     }35     void update(float dt)36     {37         if (this->getSelectItem() && this->getSelectItem()->isSelected())38             this->getSelectItem()->activate();39         else40         {41             exm->stopstate();42         }43     }44 };45 class HelloWorld : public cocos2d::Layer,public menuDelegate46 {47 public:48     // there's no 'id' in cpp, so we recommend returning the class instance pointer49     static cocos2d::Scene* createScene();50 51     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone52     virtual bool init();53 54     // a selector callback55     void menuCloseCallback(cocos2d::Ref* pSender);56 57     // implement the "static create()" method manually58     CREATE_FUNC(HelloWorld);59 60     void stopstate();61     void update(float dt);62     void moveRight();63     void moveLeft();64     void moveAttack();65     void moveDead();66     void loadMenu();67     Hero* hero;68     Panel* menu;69 };70 #endif // __HELLOWORLD_SCENE_H__

The following is the cpp file of the scenario class:

  1 #include "HelloWorldScene.h"  2   3 USING_NS_CC;  4 using namespace cocostudio;  5 Scene* HelloWorld::createScene()  6 {  7     // 'scene' is an autorelease object  8     auto scene = Scene::create();  9      10     // 'layer' is an autorelease object 11     auto layer = HelloWorld::create(); 12  13     // add layer as a child to scene 14     scene->addChild(layer); 15  16     // return the scene 17     return scene; 18 } 19  20 // on "init" you need to initialize your instance 21 bool HelloWorld::init() 22 { 23     ////////////////////////////// 24     // 1. super init first 25     if ( !Layer::init() ) 26     { 27         return false; 28     } 29      30     Size visibleSize = Director::getInstance()->getVisibleSize(); 31     Vec2 origin = Director::getInstance()->getVisibleOrigin(); 32  33     ///////////////////////////// 34     // 2. add a menu item with "X" image, which is clicked to quit the program 35     //    you may modify it. 36  37     // add a "close" icon to exit the progress. it's an autorelease object 38     auto closeItem = MenuItemImage::create( 39                                            "CloseNormal.png", 40                                            "CloseSelected.png", 41                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 42      43     closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 44                                 origin.y + closeItem->getContentSize().height/2)); 45  46     // create menu, it's an autorelease object 47     auto menu = Menu::create(closeItem, NULL); 48     menu->setPosition(Vec2::ZERO); 49     this->addChild(menu, 1); 50  51     ///////////////////////////// 52     // 3. add your codes below... 53  54     // add a label shows "Hello World" 55     // create and initialize a label 56      57     auto label = LabelTTF::create("Hello World", "Arial", 24); 58      59     // position the label on the center of the screen 60     label->setPosition(Vec2(origin.x + visibleSize.width/2, 61                             origin.y + visibleSize.height - label->getContentSize().height)); 62  63     // add the label as a child to this layer 64     this->addChild(label, 1); 65     loadMenu(); 66     hero = Hero::create(); 67     addChild(hero); 68     scheduleUpdate(); 69     return true; 70 } 71 void HelloWorld::update(float dt) 72 { 73     if (hero->dir == RIGHT) 74     { 75         hero->runRight(dt); 76     } 77     if (hero->dir == LEFT) 78     { 79         hero->runLeft(dt); 80     } 81 } 82 void HelloWorld::loadMenu() 83 { 84     Size size = Director::getInstance()->getWinSize(); 85     auto closeItem1 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_0(HelloWorld::moveLeft, this)); 86     auto closeItem2 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_0(HelloWorld::moveRight, this)); 87     auto closeItem3 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_0(HelloWorld::moveAttack, this)); 88     auto closeItem4 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_0(HelloWorld::moveDead, this)); 89     menu = Panel::create(); 90     menu->addChild(closeItem1); 91     menu->addChild(closeItem2); 92     menu->addChild(closeItem3); 93     menu->addChild(closeItem4); 94     menu->alignItemsHorizontally(); 95     menu->setPositionY(menu->getPositionY() / 2); 96     menu->exm = this; 97     addChild(menu); 98 } 99 100 void HelloWorld::moveRight()101 {102     if (hero->dir == NONE)103         hero->armature->getAnimation()->play("run");104     float num = hero->armature->getRotationY();105     if (num == -180)106     {107         hero->armature->setRotationY(0);108     }109     hero->dir = RIGHT;110 }111 void HelloWorld::moveLeft()112 {113     if (hero->dir == NONE)114         hero->armature->getAnimation()->play("run");115     float num = hero->armature->getRotationY();116     if (num == 0 )117     {118         hero->armature->setRotationY(-180);119     }120     hero->dir = LEFT;121 }122 void HelloWorld::moveDead()123 {124     hero->death();125 }126 void HelloWorld::moveAttack()127 {128     hero->attack();129 }130 void HelloWorld::stopstate()131 {132     if (hero->dir == NONE)133         return;134     float num = hero->armature->getRotationY();135     if (num == 0)136     {137         hero->stop();138         CCLOG("111111");139     }140     else if (num == -180)141     {142         hero->stop();143         hero->armature->setRotationY(-180);144     }145     hero->dir = NONE;146 }147 void HelloWorld::menuCloseCallback(Ref* pSender)148 {149 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)150     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");151     return;152 #endif153 154     Director::getInstance()->end();155 156 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)157     exit(0);158 #endif159 }
End !!!! 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.