zoukankan      html  css  js  c++  java
  • 五毛的cocos2d-x学习笔记08-动画

    一个例子就够了,单击文本标签,执行动画。我也是小白,写这个demo的时候遇到了问题,单击文本标签游戏就死掉了。今天为了解决这个问题也是一晚没睡,到学习群里问大神,经过大神的指点解决了问题。原来是Animation和Animate的生命周期的关系。先记下。

     1 bool HelloWorld::init()
     2 {
     3     //////////////////////////////
     4     // 1. super init first
     5     if ( !Layer::init() )
     6     {
     7         return false;
     8     }
     9 
    10     SpriteFrameCache *cache = SpriteFrameCache::getInstance();
    11     cache->addSpriteFramesWithFile("a6.plist");
    12 
    13     Vector<SpriteFrame*> vec;
    14     char name[15];
    15     memset(name, 0, 15);
    16 
    17     for (int i = 1; i <=7; i++){
    18         sprintf(name, "a6_%02d.png", i);
    19         vec.pushBack(cache->getSpriteFrameByName(name));
    20     }
    21 
    22     Animation *animation = Animation::createWithSpriteFrames(vec, 0.1f, 1);
    23     
    24     Animate *animate = Animate::create(animation);
    25 
    26     auto sprite = Sprite::create();
    27     addChild(sprite);
    28     sprite->setPosition(Vec2(200, 200));
    29     //sprite->runAction(RepeatForever::create(animate));
    30 
    31     auto label = LabelTTF::create("Touch", "Courier", 30);
    32     label->setPosition(Vec2(500, 500));
    33     addChild(label);
    34 
    35     int i = 10;
    36 
    37     EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    38     listener->onTouchBegan = [=](Touch *t, Event *e){
    39     if (label->getBoundingBox().containsPoint(t->getLocation())){
    40         //notification: pay attention to the life cycle of Animation and Animate
    41         Animation *animation = Animation::createWithSpriteFrames(vec, 0.1f, 1);
    42         Animate *animate = Animate::create(animation);
    43         sprite->runAction(animate);
    44         log("i=%d", i);
    45         return true;
    46     }
    47     return false;
    48     };
    49     //notifation:here is "this" not "label" because if here is "label", Touch *t equals to "label"
    50     Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);
    51     return true;
    52 }
    init

    运行效果:

  • 相关阅读:
    【Vue】 修饰符sync
    【VUE】vue路由跳转的方式
    【Element】elementui的Cascader 级联选择器,在懒加载的时候数据无法回显的解决方案
    【ES6】利用ES6 Set 将数组去重
    【.NETCORE】Refit 框架
    【.NETCORE】ASP.NET Core SignalR
    【Visual Studio Code】驼峰翻译助手
    VueX(Vue状态管理模式)
    hdmi 随笔
    ad 差分布线 等长布线
  • 原文地址:https://www.cnblogs.com/rainmer/p/4694143.html
Copyright © 2011-2022 走看看