概述:
cocos2d 做动画特效,利用cocos2d的Action类,非常简单。直接调用node->runAction(),即可实现相应的特效。
先上一张action 类图:
本次暂不介绍ccFollow与ccSpeed。除了这两个类之外,常用的动作父类,即时动作(ccActionInstant)-瞬间完成,延时动作(ccActionInterval)-逐渐完成。
使用示例:
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Node* sp = Sprite::create("HelloWorld.png");
sp->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(sp);
1.执行动作
sp ->runAction(MoveTo::create(1,Vec2(100,100)));
这样就给精灵一个MoveTo动作,执行时间是1s,移动到100,100的位置。
2.动作反转
sp ->runAction(MoveTo::create(1,Vec2(100,100))->reverse());
给Action动作执行一个reverse()方法,就能让动作反转执行
3.动作重复
//如果让Node在1s内旋转180度
sp -runAction(RotateBy::create(1,180));
//重复执行此动作3次:
sp -runAction(Repeate::create(RotateBy::create(1,180)));
//永久重复执行动作
sp -runAction(RepeateForever::create(RotateBy::create(1,180)));
4.混合动作同时执行
sp ->runAction(Spawn::create(MoveTo::create(1,Vec2(100,100)),RotateBy::create(1,360),NULL));
使用Spawn可以实现多个动作同时进行,让label1s内移动到100,100的位置同时自身旋转一周。并且在末尾要写一个NULL,能标识动作组的结束
5.混合动作顺序执行
sp ->runAction(Sequence::create(MoveTo(1,Vec2(100,100)),RotateBy::create(1,360),NULL));
其实Sequence跟Spawn使用方法一样,一个是同时,一个是顺序
Action 动作完成监听
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//获取屏幕的尺寸、位置信息等
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Node* sp = Sprite::create("HelloWorld.png");
sp->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(sp);
//设置精灵,并把精灵添加到舞台的中央
Sprite*sprite=Sprite::create("HelloWorld.png");
sprite->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(sprite);
//动作1
FiniteTimeAction* action1=Blink::create(1.0f,3);
//动作2
FiniteTimeAction* action2=MoveBy::create(1.0f,ccp(0,100));
CallFunc *callFunc=CCCallFunc::create(this,callfunc_selector(HelloWorld::actionCallback));
//合并上述动作,并应用到精灵
FiniteTimeAction* sequence=CCSequence::create(action1,action2,callFunc,NULL);
sprite->runAction(sequence);
return true;
}
void HelloWorld::actionCallback()
{
//动画结束
}
希望对您有所帮助!