Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5406298
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: iOS平台

2015-09-21 18:11:49

1. MoveTo 和 MoveBy

MoveTo 就是移动到指定坐标的一个动作。

MoveTo::create(float duration, const Vec2& deltaPosition)

float duration :动作执行持续时间,单位为秒。

const Vec2& deltaPosition:指定移动的目的坐标。


MoveBy就是移动距离

MoveBy::create(float duration, const Vec2& deltaPosition)


float duration :动作执行持续时间,单位为秒。

const Vec2& deltaPosition:指定移动的距离。


  1. auto sprite = Sprite::create("sprite.png");  
  2.   
  3. // position the sprite on the center of the screen  
  4. sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  5.   
  6. // add the sprite as a child to this layer  
  7. this->addChild(sprite, 0);  
  8.      
  9. //创建MoveTo动作对象  
  10. auto moveto = MoveTo::create(0.9f,Point(100,100));  
  11.   
  12. //创建Moveby动作对象  
  13. auto moveby = MoveBy::create(0.9f,Point(100,100));  
  14.   
  15. //精灵执行动作  
  16. //sprite->runAction(moveto);  
  17.   
  18. sprite->runAction(moveby);  

2. ScaleTo 和ScaleBy


scaleTo:不管精灵当前的拉伸倍数是多少,它都会把精灵缩放到指定的倍数。

ScaleTo::create(float duration, float sx, float sy)

float duration:动作的持续时间,单位为秒

float sx :X方向的拉伸值

float sy:Y方向的拉伸值



scaleBy:是在精灵当前的拉伸倍数的基础上,再次进行拉伸。例如精灵当前的拉伸倍数2.0,指定精灵x拉伸2.0f,y拉伸1.0f,则精灵的当前x拉伸倍数变为4.0f,就是在原来的2.0的基础上x再放大两倍。

ScaleBy::create(float duration, float sx, float sy)

float duration:动作的持续时间,单位为秒

float sx :X方向的拉伸值

float sy:Y方向的拉伸值

  1. auto sprite = Sprite::create("sprite.png");  
  2. // position the sprite on the center of the screen  
  3. sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  4.       
  5. auto sprite2 = Sprite::create("sprite.png");  
  6. // position the sprite on the center of the screen  
  7. sprite2->setPosition(Vec2(visibleSize.width/2 + 200, visibleSize.height/2 + origin.y));  
  8. sprite2->setScale(2.0f);  
  9.   
  10. auto sprite3 = Sprite::create("sprite.png");  
  11. // position the sprite on the center of the screen  
  12. sprite3->setPosition(Vec2(visibleSize.width/2 - 200, visibleSize.height/2 + origin.y));  
  13. sprite3->setScale(2.0f);  
  14.   
  15. // add the sprite as a child to this layer  
  16. this->addChild(sprite, 0);  
  17. this->addChild(sprite2, 1);  
  18. this->addChild(sprite3);  
  19.      
  20. //创建ScaleTo对象  
  21. auto scaleTo = ScaleTo::create(2.8f,1.0f,1.0f);  
  22.   
  23. //创建ScaleBy对象  
  24. auto scaleBy = ScaleBy::create(2.8f,2.0f,1.0f);  
  25.   
  26. //精灵执行动作  
  27.   
  28. sprite3->runAction(scaleTo);  
  29. sprite2->runAction(scaleBy);  


3. Blink 精灵闪烁

Blink::create(float duration, int blinks)

float duration:动作持续时间,单位为秒

int blinks:闪烁次数


  1. auto sprite4 = Sprite::create("sprite.png");  
  2. sprite4->setPosition(Vec2(visibleSize.width/2 - 300, visibleSize.height/2 + origin.y));  
  3. sprite4->setScale(2.0f);  
  4. this->addChild(sprite4);  
  5.   
  6. //创建Blink动作对象  
  7. auto blink = Blink::create(3.0f,3);  
  8.   
  9. sprite4->runAction(blink);  


4.BezierTo和BezierBy


创建贝塞尔曲线动作需要一个ccBezierConfig 对象,找个对象有3个属性:

bezier.controlPoint_1 :波谷偏向值
bezier.controlPoint_2 :波峰偏小值
bezier.endPosition :动作终点位置

BezierTo 直接移动到各个坐标点

BezierBy 是在精灵当前位置为基准进行移动的。


  1. //创建贝塞尔曲线的配置  
  2. ccBezierConfig bezier;  
  3. bezier.controlPoint_1 = Point(100,0);  
  4. bezier.controlPoint_2 = Point(200,250);  
  5. bezier.endPosition = Point(300,0);  
  6. //创建BezierTo动作对象  
  7. auto bezierTo = BezierTo::create(3.0f,bezier);  
  8.   
  9. //创建BezierBy动作对象  
  10. //auto bezierBy = BezierBy::create(3.0f,bezier);  
  11.   
  12. sprite5->runAction(bezierTo);  
  13. //sprite5->runAction(bezierBy);  


5. repeatForever 和 repeat


repeatForever :创建一个永久性重复动作。

repeat:创建一个指定次数的动作。


  1. auto sprite6 = Sprite::create("sprite.png");  
  2. sprite6->setPosition(Vec2(visibleSize.width/2 - 300, visibleSize.height/2 +100));  
  3. this->addChild(sprite6);  
  4.   
  5. auto jumpBy = JumpBy::create(3.0f,Point(50,1),100,1);  
  6. //以jumpBy为参数,创建个永久性的重复动作  
  7. auto repeatForever = RepeatForever::create(jumpBy);  
  8.   
  9. //以jumpBy为参数,创建个指定次数的重复动作  
  10. auto repeat = Repeat::create(jumpBy,3);  
  11.   
  12. //sprite6->runAction(repeatForever);  
  13. sprite6->runAction(repeat);  


6. Sequence和Spawn


Spawn:让所有的动作一起播放

Sequence:让动作一个一个播放


  1. auto sprite = Sprite::create("sprite.png");  
  2. sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  3. this->addChild(sprite, 0);  
  4.   
  5. //创建一个移动动作对象  
  6. auto moveBy = MoveBy::create(2.2f,Point(40,20));  
  7.   
  8. //创建一个弹跳动作  
  9. auto  jumpBy = JumpBy::create(3.0f,Point(50,1),100,5);  
  10.   
  11. //创建一个旋转动作  
  12. auto rotateBy = RotateBy::create(2.5f,220,10);  
  13.   
  14. auto actions = Sequence::create(moveBy,jumpBy,rotateBy,NULL);  
  15.   
  16. //auto actions = Spawn::create(moveBy,jumpBy,rotateBy,NULL);  
  17.   
  18. //循环播放  
  19. //auto repeat = RepeatForever::create(actions);  
  20. sprite->runAction(actions);  


7. 动作监听


CallFunc 它的动作就是回调一个函数。使用CC_CALLBACK_0创建要回调的函数。


  1. bool ActionTest::init()  
  2. {  
  3.     // 1. super init first  
  4.     if ( !Layer::init() )  
  5.     {  
  6.         return false;  
  7.     }  
  8.       
  9.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  10.     Vec2 origin = Director::getInstance()->getVisibleOrigin();  
  11.   
  12.     auto sprite = Sprite::create("sprite.png");  
  13.     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  14.     this->addChild(sprite, 0);  
  15.       
  16.     //创建一个移动动作对象  
  17.     auto moveBy = MoveBy::create(2.2f,Point(40,20));  
  18.   
  19.     //创建一个弹跳动作  
  20.     auto  jumpBy = JumpBy::create(3.0f,Point(50,1),100,5);  
  21.   
  22.     //创建一个旋转动作  
  23.     auto rotateBy = RotateBy::create(2.5f,220,10);  
  24.   
  25.     //auto actions = Sequence::create(moveBy,jumpBy,rotateBy,NULL);  
  26.   
  27.     //auto actions = Spawn::create(moveBy,jumpBy,rotateBy,NULL);  
  28.   
  29.     //循环播放  
  30.     //auto repeat = RepeatForever::create(actions);  
  31.   
  32.   
  33.     auto moveto = MoveTo::create(10.0f,Vec2(visibleSize.width, visibleSize.height/2 + origin.y));  
  34.   
  35.     auto callfunc = CallFunc::create(CC_CALLBACK_0(ActionTest::backhome,this));  
  36.   
  37.     auto actions = Sequence::create(moveto,callfunc,NULL);  
  38.     sprite->runAction(actions);  
  39.     return true;  
  40. }  
  41.   
  42. void ActionTest::backhome()  
  43. {  
  44.     auto lable = LabelTTF::create("I am Home!","Arial",35);  
  45.     lable->setPosition(Point(300,300));  
  46.     this->addChild(lable);  
  47. }  
阅读(2325) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~