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

    运行效果:

  • 相关阅读:
    将Nginx添加到windows服务中
    springboot使用redis管理session
    GIT常用命令
    阻止360、谷歌浏览器表单自动填充
    谈谈对Spring IOC的理解
    同一个Nginx服务器同一端口配置多个代理服务
    LeetCode 653. Two Sum IV
    109. Convert Sorted List to Binary Search Tree(根据有序链表构造平衡的二叉查找树)
    108. Convert Sorted Array to Binary Search Tree(从有序数组中构造平衡的BST)
    LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树求两点LCA)
  • 原文地址:https://www.cnblogs.com/rainmer/p/4694143.html
Copyright © 2011-2022 走看看