1. 程式人生 > >cocos2d各種動作的使用(變色、跳動、旋轉、閃爍、懸掛、放大縮小、漸變、animation)

cocos2d各種動作的使用(變色、跳動、旋轉、閃爍、懸掛、放大縮小、漸變、animation)


跳躍 – CCJumpBy

設定終點位置和跳躍癿高度和次數。

放大到 – CCScaleTo

設定放大倍數,是浮點型。

放大 – CCScaleBy

旋轉到 – CCRotateTo

旋轉 – CCRotateBy

閃爍 – CCBlink

色調變化到 – CCTintTo

色調變換 – CCTintBy

變暗到 – CCFadeTo

由無變亮 – CCFadeIn

由亮變無 – CCFadeOut

上程式碼:
  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.         //2跳標籤 
  11.         CCLabelTTF *jumpL = [CCLabelTTF labelWithString:@"jump"
     fontName:@"Marker Felt" fontSize:30];  
  12.         jumpL.position = ccp(size.width / 2,jumpL.textureRect.size.height / 2);  
  13.         [self addChild:jumpL];  
  14.         //3旋轉標籤
  15.         CCLabelTTF *rotateL = [CCLabelTTF labelWithString:@"rotate" fontName:@"Marker Felt" fontSize:30];  
  16.         rotateL.position = ccp(size.width / 2, size.height / 2 + hello.textureRect.size.height);  
  17.         [self addChild:rotateL];  
  18.         //4閃爍出現標籤
  19.         CCLabelTTF *blinkL = [CCLabelTTF labelWithString:@"blink" fontName:@"Marker Felt" fontSize:30];  
  20.         blinkL.position = ccp(size.width / 2, size.height - blinkL.textureRect.size.height / 2);  
  21.         [self addChild:blinkL];  
  22.         //5懸掛標籤
  23.         CCLabelTTF *hangL = [CCLabelTTF labelWithString:@"hang" fontName:@"Marker Felt" fontSize:30];  
  24.         hangL.position = ccp(hangL.textureRect.size.width / 2, size.height - hangL.textureRect.size.height / 2);  
  25.         [self addChild:hangL];  
  26.         //6放大、縮小標籤
  27.         CCLabelTTF *scaleL = [CCLabelTTF labelWithString:@"scale" fontName:@"Marker Felt" fontSize:30];  
  28.         scaleL.position = ccp(size.width - scaleL.textureRect.size.width,size.height - scaleL.textureRect.size.height );  
  29.         [self addChild:scaleL];  
  30.         //7有無變有,有有變無標籤
  31.         CCLabelTTF *fadeL = [CCLabelTTF labelWithString:@"fade" fontName:@"Marker Felt" fontSize:30];  
  32.         fadeL.position = ccp(size.width / 4, size.height / 2);  
  33.         [self addChild:fadeL];  
  34.         //8動畫,小人在走
  35.         CCSpriteBatchNode *mgr = [CCSpriteBatchNode batchNodeWithFile:@"xiaoren.png" capacity:5];  
  36.         CCSpriteBatchNode *mgr1 = [CCSpriteBatchNode batchNodeWithFile:@"xiaorenzou.png" capacity:5];  
  37.         CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(0, 0, 57, 57)];  
  38.         CCSpriteFrame *frame1 = [CCSpriteFrame frameWithTexture:mgr1.texture rect:CGRectMake(0, 0, 57, 57)];  
  39.         CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[NSArray arrayWithObjects:frame,frame1, nil] delay:0.2f];  
  40.         id action = [CCAnimate actionWithAnimation:animation];  
  41.         CCSprite *sprite = [CCSprite spriteWithSpriteFrame:frame];  
  42.         sprite.position = ccp(size.width / 4,size.height / 4 );  
  43.         [self addChild:sprite];  
  44.         [sprite runAction:[CCRepeatForever actionWithAction:action]];  
  45.         //再疊加一個動作,四周走動
  46.         CCMoveTo *moveto1 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4,size.height / 4 * 3)];  
  47.         CCMoveTo *moveto2 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4 * 3,size.height / 4 * 3)];  
  48.         CCMoveTo *moveto3 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4 * 3,size.height / 4)];  
  49.         CCMoveTo *moveto4 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4,size.height / 4 )];  
  50.         CCSequence *sequenceMove = [CCSequence actions:moveto1,moveto2,moveto3,moveto4, nil];  
  51.         [sprite runAction:[CCRepeatForever actionWithAction:sequenceMove]];  
  52.         /********標籤的各種動作*********/
  53.         //1、hello world標籤變色
  54.         CCTintTo *tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0];  
  55.         CCTintTo *tint2 = [CCTintTo actionWithDuration:2 red:255 green:255 blue:0];  
  56.         CCTintTo *tint3 = [CCTintTo actionWithDuration:2 red:0 green:255 blue:0];  
  57.         CCTintTo *tint4 = [CCTintTo actionWithDuration:2 red:0 green:255 blue:255];  
  58.         CCTintTo *tint5 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255];  
  59.         CCTintTo *tint6 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:255];  
  60.         //順序新增到ccsequence類中
  61.         CCSequence *tintSequence = [CCSequence actions:tint1,tint2,tint3,tint4,tint5,tint6, nil];  
  62.         //不斷的迴圈次動作
  63.         CCRepeatForever *repeatTint = [CCRepeatForever actionWithAction:tintSequence];  
  64.         //最後執行
  65.         [hello runAction:repeatTint];  
  66.         //2、jump標籤跳跳,height是跳的高度,jumps引數是速度越大越快(你可以改大了試試)
  67.         CCJumpBy *jump = [CCJumpBy actionWithDuration:3 position:CGPointZero height:size.height / 3 jumps:2];  
  68.         CCRepeatForever *repeatJump = [CCRepeatForever actionWithAction:jump];  
  69.         [jumpL runAction: repeatJump];  
  70.         //3、旋轉字型,angle:360是順時針旋轉,如果是-360就是逆時針旋轉
  71.         CCRotateBy *rotate = [CCRotateBy actionWithDuration:2 angle:360];  
  72.         CCRepeatForever *repeatBounce = [CCRepeatForever actionWithAction:rotate];  
  73.         [rotateL runAction:repeatBounce];  
  74.         //4、閃爍出現,10秒中閃20次
  75.         CCBlink *blink = [CCBlink actionWithDuration:10 blinks:20];  
  76.         CCRepeatForever *repeatBlink = [CCRepeatForever actionWithAction:blink];  
  77.         [blinkL runAction:repeatBlink];  
  78.         //5、懸掛下落動作
  79.         CGPoint hangInTherePosition = CGPointMake(hangL.position.x, size.height - [hangL texture].contentSize.height);  
  80.         CGPoint belowScreenPosition = CGPointMake(hangL.position.x,  -[hangL texture].contentSize.height);  
  81.         CCMoveTo *moveHang = [CCMoveTo actionWithDuration:3 position:hangInTherePosition];  
  82.         CCEaseElasticOut *easeHang = [CCEaseElasticOut actionWithAction:moveHang];  
  83.         CCMoveTo *moveEnd = [CCMoveTo actionWithDuration:2 position:belowScreenPosition];  
  84.         CCEaseBackInOut *easeEnd = [CCEaseBackInOut actionWithAction:moveEnd];  
  85.         CCSequence *hangsequence = [CCSequence actions:easeHang,easeEnd, nil];  
  86.         CCRepeatForever *hangRepeat = [CCRepeatForever actionWithAction:hangsequence];  
  87.         [hangL runAction:hangRepeat];  
  88.         //6、放大縮小
  89.         CCScaleTo *scaleBy1 = [CCScaleTo actionWithDuration:2 scale:2.0f];  
  90.         CCScaleTo *scaleTo1 = [CCScaleTo actionWithDuration:2 scale:1.0f];  
  91.         CCSequence *scaleSequence = [CCSequence actions:scaleBy1, scaleTo1, nil];  
  92.         CCRepeatForever *repeatScale = [CCRepeatForever actionWithAction:scaleSequence];  
  93.         [scaleL runAction:repeatScale];  
  94.         //7、有變無,無變有
  95.         CCFadeIn *fadeIn = [CCFadeIn actionWithDuration:2];  
  96.         CCFadeOut *fadeOut = [CCFadeOut actionWithDuration:2];  
  97.         CCSequence *fadeSequence = [CCSequence actions:fadeIn,fadeOut,nil];  
  98.         CCRepeatForever *repeatFade = [CCRepeatForever actionWithAction:fadeSequence];  
  99.         [fadeL runAction:repeatFade];