前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >五毛的cocos2d-x学习笔记08-动画

五毛的cocos2d-x学习笔记08-动画

作者头像
用户1737026
发布2018-06-07 15:08:15
4040
发布2018-06-07 15:08:15
举报
文章被收录于专栏:五毛程序员五毛程序员

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

代码语言:javascript
复制
 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 }

运行效果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档