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

    运行效果:

  • 相关阅读:
    Python3 字符串格式化
    TypeError: Object of type 'int32' is not JSON serializable
    论文学习——《Learning to Compose with Professional Photographs on the Web》 (ACM MM 2017)
    python中PIL.Image,OpenCV,Numpy图像格式相互转换
    python 在列表,元组,字典变量前加*号
    python指定概率随机取值 理解np.random.seed()
    论文学习——《Good View Hunting: Learning Photo Composition from Dense View Pairs》
    目标检测知识杂点
    VGG16学习笔记
    python png与jpg的相互转换
  • 原文地址:https://www.cnblogs.com/rainmer/p/4694143.html
Copyright © 2011-2022 走看看