您的位置:首页 > 移动开发 > Cocos引擎

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

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

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

SpriteFrameCache *cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("a6.plist");

Vector<SpriteFrame*> vec;
char name[15];
memset(name, 0, 15);

for (int i = 1; i <=7; i++){
sprintf(name, "a6_%02d.png", i);
vec.pushBack(cache->getSpriteFrameByName(name));
}

Animation *animation = Animation::createWithSpriteFrames(vec, 0.1f, 1);

Animate *animate = Animate::create(animation);

auto sprite = Sprite::create();
addChild(sprite);
sprite->setPosition(Vec2(200, 200));
//sprite->runAction(RepeatForever::create(animate));

auto label = LabelTTF::create("Touch", "Courier", 30);
label->setPosition(Vec2(500, 500));
addChild(label);

int i = 10;

EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [=](Touch *t, Event *e){
if (label->getBoundingBox().containsPoint(t->getLocation())){
//notification: pay attention to the life cycle of Animation and Animate
Animation *animation = Animation::createWithSpriteFrames(vec, 0.1f, 1);
Animate *animate = Animate::create(animation);
sprite->runAction(animate);
log("i=%d", i);
return true;
}
return false;
};
//notifation:here is "this" not "label" because if here is "label", Touch *t equals to "label"
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);
return true;
}


init
运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: