cocos2d-x Action 动画特效的简单使用

概述:

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()  
{     
   //动画结束 
} 

希望对您有所帮助!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值