cocos2d各种动作的使用(变色、跳动、旋转、闪烁、悬挂、放大缩小、渐变、animation)


用到的知识:

***To: 意味着运动到指定的位置。

***By:意味着运动到按照指定癿 x、y 增量的位置。(x、y 可以是负值)

移动到 – CCMoveTo

移动– CCMoveBy

跳跃到 – CCJumpTo

设置终点位置和跳跃癿高度和次数。

跳跃 – CCJumpBy

设置终点位置和跳跃癿高度和次数。

放大到 – CCScaleTo

设置放大倍数,是浮点型。

放大 – CCScaleBy

旋转到 – CCRotateTo

旋转 – CCRotateBy

闪烁 – CCBlink

色调变化到 – CCTintTo

色调变换 – CCTintBy

变暗到 – CCFadeTo

由无变亮 – CCFadeIn

由亮变无 – CCFadeOut

上代码:
[cpp] view plain copy
  1. /***********添加各种标签***********/  
  2.         //1 helloWorld标签  
  3.         CCLabelTTF *hello = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:30];  
  4.         // 屏幕大小  
  5.         CGSize size = [[CCDirector sharedDirector] winSize];  
  6.         // 标签位置  
  7.         hello.position =  ccp( size.width /2 , size.height/2 );  
  8.         // 添加标签  
  9.         [self addChild: hello];  
  10.           
  11.         //2跳标签   
  12.         CCLabelTTF *jumpL = [CCLabelTTF labelWithString:@"jump" fontName:@"Marker Felt" fontSize:30];  
  13.         jumpL.position = ccp(size.width / 2,jumpL.textureRect.size.height / 2);  
  14.         [self addChild:jumpL];  
  15.           
  16.         //3旋转标签  
  17.         CCLabelTTF *rotateL = [CCLabelTTF labelWithString:@"rotate" fontName:@"Marker Felt" fontSize:30];  
  18.         rotateL.position = ccp(size.width / 2, size.height / 2 + hello.textureRect.size.height);  
  19.         [self addChild:rotateL];  
  20.           
  21.         //4闪烁出现标签  
  22.         CCLabelTTF *blinkL = [CCLabelTTF labelWithString:@"blink" fontName:@"Marker Felt" fontSize:30];  
  23.         blinkL.position = ccp(size.width / 2, size.height - blinkL.textureRect.size.height / 2);  
  24.         [self addChild:blinkL];  
  25.           
  26.         //5悬挂标签  
  27.         CCLabelTTF *hangL = [CCLabelTTF labelWithString:@"hang" fontName:@"Marker Felt" fontSize:30];  
  28.         hangL.position = ccp(hangL.textureRect.size.width / 2, size.height - hangL.textureRect.size.height / 2);  
  29.         [self addChild:hangL];  
  30.           
  31.         //6放大、缩小标签  
  32.         CCLabelTTF *scaleL = [CCLabelTTF labelWithString:@"scale" fontName:@"Marker Felt" fontSize:30];  
  33.         scaleL.position = ccp(size.width - scaleL.textureRect.size.width,size.height - scaleL.textureRect.size.height );  
  34.         [self addChild:scaleL];  
  35.           
  36.         //7有无变有,有有变无标签  
  37.         CCLabelTTF *fadeL = [CCLabelTTF labelWithString:@"fade" fontName:@"Marker Felt" fontSize:30];  
  38.         fadeL.position = ccp(size.width / 4, size.height / 2);  
  39.         [self addChild:fadeL];  
  40.           
  41.         //8动画,小人在走  
  42.         CCSpriteBatchNode *mgr = [CCSpriteBatchNode batchNodeWithFile:@"xiaoren.png" capacity:5];  
  43.         CCSpriteBatchNode *mgr1 = [CCSpriteBatchNode batchNodeWithFile:@"xiaorenzou.png" capacity:5];  
  44.         CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(0, 0, 57, 57)];  
  45.         CCSpriteFrame *frame1 = [CCSpriteFrame frameWithTexture:mgr1.texture rect:CGRectMake(0, 0, 57, 57)];  
  46.         CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[NSArray arrayWithObjects:frame,frame1, nil] delay:0.2f];  
  47.         id action = [CCAnimate actionWithAnimation:animation];  
  48.         CCSprite *sprite = [CCSprite spriteWithSpriteFrame:frame];  
  49.         sprite.position = ccp(size.width / 4,size.height / 4 );  
  50.         [self addChild:sprite];  
  51.         [sprite runAction:[CCRepeatForever actionWithAction:action]];  
  52.         //再叠加一个动作,四周走动  
  53.         CCMoveTo *moveto1 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4,size.height / 4 * 3)];  
  54.         CCMoveTo *moveto2 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4 * 3,size.height / 4 * 3)];  
  55.         CCMoveTo *moveto3 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4 * 3,size.height / 4)];  
  56.         CCMoveTo *moveto4 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4,size.height / 4 )];  
  57.         CCSequence *sequenceMove = [CCSequence actions:moveto1,moveto2,moveto3,moveto4, nil];  
  58.         [sprite runAction:[CCRepeatForever actionWithAction:sequenceMove]];  
  59.           
  60.           
  61.           
  62.         /********标签的各种动作*********/  
  63.         //1、hello world标签变色  
  64.         CCTintTo *tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0];  
  65.         CCTintTo *tint2 = [CCTintTo actionWithDuration:2 red:255 green:255 blue:0];  
  66.         CCTintTo *tint3 = [CCTintTo actionWithDuration:2 red:0 green:255 blue:0];  
  67.         CCTintTo *tint4 = [CCTintTo actionWithDuration:2 red:0 green:255 blue:255];  
  68.         CCTintTo *tint5 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255];  
  69.         CCTintTo *tint6 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:255];  
  70.         //顺序添加到ccsequence类中  
  71.         CCSequence *tintSequence = [CCSequence actions:tint1,tint2,tint3,tint4,tint5,tint6, nil];  
  72.         //不断的循环次动作  
  73.         CCRepeatForever *repeatTint = [CCRepeatForever actionWithAction:tintSequence];  
  74.         //最后运行  
  75.         [hello runAction:repeatTint];  
  76.           
  77.         //2、jump标签跳跳,height是跳的高度,jumps参数是速度越大越快(你可以改大了试试)  
  78.         CCJumpBy *jump = [CCJumpBy actionWithDuration:3 position:CGPointZero height:size.height / 3 jumps:2];  
  79.         CCRepeatForever *repeatJump = [CCRepeatForever actionWithAction:jump];  
  80.         [jumpL runAction: repeatJump];  
  81.           
  82.         //3、旋转字体,angle:360是顺时针旋转,如果是-360就是逆时针旋转  
  83.         CCRotateBy *rotate = [CCRotateBy actionWithDuration:2 angle:360];  
  84.         CCRepeatForever *repeatBounce = [CCRepeatForever actionWithAction:rotate];  
  85.         [rotateL runAction:repeatBounce];  
  86.   
  87.         //4、闪烁出现,10秒中闪20次  
  88.         CCBlink *blink = [CCBlink actionWithDuration:10 blinks:20];  
  89.         CCRepeatForever *repeatBlink = [CCRepeatForever actionWithAction:blink];  
  90.         [blinkL runAction:repeatBlink];  
  91.           
  92.         //5、悬挂下落动作  
  93.         CGPoint hangInTherePosition = CGPointMake(hangL.position.x, size.height - [hangL texture].contentSize.height);  
  94.         CGPoint belowScreenPosition = CGPointMake(hangL.position.x,  -[hangL texture].contentSize.height);  
  95.           
  96.         CCMoveTo *moveHang = [CCMoveTo actionWithDuration:3 position:hangInTherePosition];  
  97.         CCEaseElasticOut *easeHang = [CCEaseElasticOut actionWithAction:moveHang];  
  98.         CCMoveTo *moveEnd = [CCMoveTo actionWithDuration:2 position:belowScreenPosition];  
  99.         CCEaseBackInOut *easeEnd = [CCEaseBackInOut actionWithAction:moveEnd];  
  100.         CCSequence *hangsequence = [CCSequence actions:easeHang,easeEnd, nil];  
  101.         CCRepeatForever *hangRepeat = [CCRepeatForever actionWithAction:hangsequence];  
  102.         [hangL runAction:hangRepeat];  
  103.           
  104.         //6、放大缩小  
  105.         CCScaleTo *scaleBy1 = [CCScaleTo actionWithDuration:2 scale:2.0f];  
  106.         CCScaleTo *scaleTo1 = [CCScaleTo actionWithDuration:2 scale:1.0f];  
  107.         CCSequence *scaleSequence = [CCSequence actions:scaleBy1, scaleTo1, nil];  
  108.         CCRepeatForever *repeatScale = [CCRepeatForever actionWithAction:scaleSequence];  
  109.         [scaleL runAction:repeatScale];  
  110.   
  111.         //7、有变无,无变有  
  112.         CCFadeIn *fadeIn = [CCFadeIn actionWithDuration:2];  
  113.         CCFadeOut *fadeOut = [CCFadeOut actionWithDuration:2];  
  114.         CCSequence *fadeSequence = [CCSequence actions:fadeIn,fadeOut,nil];  
  115.         CCRepeatForever *repeatFade = [CCRepeatForever actionWithAction:fadeSequence];  
  116.         [fadeL runAction:repeatFade];

你可能感兴趣的:(cocos2d各种动作的使用(变色、跳动、旋转、闪烁、悬挂、放大缩小、渐变、animation))