cocos2d各種動作的使用(變色、跳動、旋轉、閃爍、懸掛、放大縮小、漸變、animation)
阿新 • • 發佈:2019-02-06
跳躍 – CCJumpBy
設定終點位置和跳躍癿高度和次數。
放大到 – CCScaleTo
設定放大倍數,是浮點型。
放大 – CCScaleBy
旋轉到 – CCRotateTo
旋轉 – CCRotateBy
閃爍 – CCBlink
色調變化到 – CCTintTo
色調變換 – CCTintBy
變暗到 – CCFadeTo
由無變亮 – CCFadeIn
由亮變無 – CCFadeOut
上程式碼:- /***********新增各種標籤***********/
- //1 helloWorld標籤
- CCLabelTTF *hello = [CCLabelTTF labelWithString:@"Hello World"
- // 螢幕大小
- CGSize size = [[CCDirector sharedDirector] winSize];
- // 標籤位置
- hello.position = ccp( size.width /2 , size.height/2 );
- // 新增標籤
- [self addChild: hello];
- //2跳標籤
- CCLabelTTF *jumpL = [CCLabelTTF labelWithString:@"jump"
- jumpL.position = ccp(size.width / 2,jumpL.textureRect.size.height / 2);
- [self addChild:jumpL];
- //3旋轉標籤
- CCLabelTTF *rotateL = [CCLabelTTF labelWithString:@"rotate" fontName:@"Marker Felt" fontSize:30];
- rotateL.position = ccp(size.width / 2, size.height / 2 + hello.textureRect.size.height);
- [self addChild:rotateL];
- //4閃爍出現標籤
- CCLabelTTF *blinkL = [CCLabelTTF labelWithString:@"blink" fontName:@"Marker Felt" fontSize:30];
- blinkL.position = ccp(size.width / 2, size.height - blinkL.textureRect.size.height / 2);
- [self addChild:blinkL];
- //5懸掛標籤
- CCLabelTTF *hangL = [CCLabelTTF labelWithString:@"hang" fontName:@"Marker Felt" fontSize:30];
- hangL.position = ccp(hangL.textureRect.size.width / 2, size.height - hangL.textureRect.size.height / 2);
- [self addChild:hangL];
- //6放大、縮小標籤
- CCLabelTTF *scaleL = [CCLabelTTF labelWithString:@"scale" fontName:@"Marker Felt" fontSize:30];
- scaleL.position = ccp(size.width - scaleL.textureRect.size.width,size.height - scaleL.textureRect.size.height );
- [self addChild:scaleL];
- //7有無變有,有有變無標籤
- CCLabelTTF *fadeL = [CCLabelTTF labelWithString:@"fade" fontName:@"Marker Felt" fontSize:30];
- fadeL.position = ccp(size.width / 4, size.height / 2);
- [self addChild:fadeL];
- //8動畫,小人在走
- CCSpriteBatchNode *mgr = [CCSpriteBatchNode batchNodeWithFile:@"xiaoren.png" capacity:5];
- CCSpriteBatchNode *mgr1 = [CCSpriteBatchNode batchNodeWithFile:@"xiaorenzou.png" capacity:5];
- CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(0, 0, 57, 57)];
- CCSpriteFrame *frame1 = [CCSpriteFrame frameWithTexture:mgr1.texture rect:CGRectMake(0, 0, 57, 57)];
- CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[NSArray arrayWithObjects:frame,frame1, nil] delay:0.2f];
- id action = [CCAnimate actionWithAnimation:animation];
- CCSprite *sprite = [CCSprite spriteWithSpriteFrame:frame];
- sprite.position = ccp(size.width / 4,size.height / 4 );
- [self addChild:sprite];
- [sprite runAction:[CCRepeatForever actionWithAction:action]];
- //再疊加一個動作,四周走動
- CCMoveTo *moveto1 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4,size.height / 4 * 3)];
- CCMoveTo *moveto2 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4 * 3,size.height / 4 * 3)];
- CCMoveTo *moveto3 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4 * 3,size.height / 4)];
- CCMoveTo *moveto4 = [CCMoveTo actionWithDuration:2 position:ccp(size.width / 4,size.height / 4 )];
- CCSequence *sequenceMove = [CCSequence actions:moveto1,moveto2,moveto3,moveto4, nil];
- [sprite runAction:[CCRepeatForever actionWithAction:sequenceMove]];
- /********標籤的各種動作*********/
- //1、hello world標籤變色
- CCTintTo *tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0];
- CCTintTo *tint2 = [CCTintTo actionWithDuration:2 red:255 green:255 blue:0];
- CCTintTo *tint3 = [CCTintTo actionWithDuration:2 red:0 green:255 blue:0];
- CCTintTo *tint4 = [CCTintTo actionWithDuration:2 red:0 green:255 blue:255];
- CCTintTo *tint5 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255];
- CCTintTo *tint6 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:255];
- //順序新增到ccsequence類中
- CCSequence *tintSequence = [CCSequence actions:tint1,tint2,tint3,tint4,tint5,tint6, nil];
- //不斷的迴圈次動作
- CCRepeatForever *repeatTint = [CCRepeatForever actionWithAction:tintSequence];
- //最後執行
- [hello runAction:repeatTint];
- //2、jump標籤跳跳,height是跳的高度,jumps引數是速度越大越快(你可以改大了試試)
- CCJumpBy *jump = [CCJumpBy actionWithDuration:3 position:CGPointZero height:size.height / 3 jumps:2];
- CCRepeatForever *repeatJump = [CCRepeatForever actionWithAction:jump];
- [jumpL runAction: repeatJump];
- //3、旋轉字型,angle:360是順時針旋轉,如果是-360就是逆時針旋轉
- CCRotateBy *rotate = [CCRotateBy actionWithDuration:2 angle:360];
- CCRepeatForever *repeatBounce = [CCRepeatForever actionWithAction:rotate];
- [rotateL runAction:repeatBounce];
- //4、閃爍出現,10秒中閃20次
- CCBlink *blink = [CCBlink actionWithDuration:10 blinks:20];
- CCRepeatForever *repeatBlink = [CCRepeatForever actionWithAction:blink];
- [blinkL runAction:repeatBlink];
- //5、懸掛下落動作
- CGPoint hangInTherePosition = CGPointMake(hangL.position.x, size.height - [hangL texture].contentSize.height);
- CGPoint belowScreenPosition = CGPointMake(hangL.position.x, -[hangL texture].contentSize.height);
- CCMoveTo *moveHang = [CCMoveTo actionWithDuration:3 position:hangInTherePosition];
- CCEaseElasticOut *easeHang = [CCEaseElasticOut actionWithAction:moveHang];
- CCMoveTo *moveEnd = [CCMoveTo actionWithDuration:2 position:belowScreenPosition];
- CCEaseBackInOut *easeEnd = [CCEaseBackInOut actionWithAction:moveEnd];
- CCSequence *hangsequence = [CCSequence actions:easeHang,easeEnd, nil];
- CCRepeatForever *hangRepeat = [CCRepeatForever actionWithAction:hangsequence];
- [hangL runAction:hangRepeat];
- //6、放大縮小
- CCScaleTo *scaleBy1 = [CCScaleTo actionWithDuration:2 scale:2.0f];
- CCScaleTo *scaleTo1 = [CCScaleTo actionWithDuration:2 scale:1.0f];
- CCSequence *scaleSequence = [CCSequence actions:scaleBy1, scaleTo1, nil];
- CCRepeatForever *repeatScale = [CCRepeatForever actionWithAction:scaleSequence];
- [scaleL runAction:repeatScale];
- //7、有變無,無變有
- CCFadeIn *fadeIn = [CCFadeIn actionWithDuration:2];
- CCFadeOut *fadeOut = [CCFadeOut actionWithDuration:2];
- CCSequence *fadeSequence = [CCSequence actions:fadeIn,fadeOut,nil];
- CCRepeatForever *repeatFade = [CCRepeatForever actionWithAction:fadeSequence];
- [fadeL runAction:repeatFade];