`
浮生长恨
  • 浏览: 208271 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Action(动作)(转)

阅读更多
原地址:http://hi.baidu.com/you5a_com/item/d3c151ff37f57154c9f3374d


通过动作让节点移动,旋转,缩放,着色,淡进淡出和干很多其它的事情


移动到– CCMoveTo
移动– CCMoveBy

CCSprite*grossini = [CCSpritespriteWithFile:@"grossini.png"];
[selfaddChild:grossini];
[grossinisetPosition:ccp(100,100)];//初始位置
//横向右移动(慢速)
CCAction*moveAction = [CCMoveByactionWithDuration:5.0f
                               position:CGPointMake(300.0f,0.0f)];
[grossinirunAction:moveAction];
跳跃到 – CCJumpTo   设置终点位置和跳跃癿高度和次数。
跳跃 – CCJumpBy    设置终点位置和跳跃癿高度和次数。
贝塞尔 – CCBezierBy 支持 3 次贝塞尔曲线:P0-起点,P1-起点切线方向,P2-终点切线方向,P3-终点。


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

- (void)bugMe:(CCNode*)node
{
    [nodestopAllActions]; //停止动作
    [noderunAction:[CCScaleToactionWithDuration:2scale:2]];
}
放大 – CCScaleBy
[grossini runAction:[CCScaleBy actionWithDuration:2 scale:2]];
旋转到 – CCRotateTo
旋转 – CCRotateBy
闪烁 – CCBlink     设定闪烁次数
idaction1 = [CCBlinkactionWithDuration:3blinks:10];
色调变化到 – CCTintTo
idaction1 = [CCTintToactionWithDuration:2red:255green:0blue:255];
色调变换 – CCTintBy
id action2 = [CCTintBy actionWithDuration:2 red:-127 green:-255blue:-127];
变暗到 – CCFadeTo
由无变亮 – CCFadeIn
idaction1 = [CCFadeInactionWithDuration:1.0f];
由亮变无 – CCFadeOut
idaction2 = [CCFadeOutactionWithDuration:1.0f];


重复动作 (Repeating Actions):

你可以让动作或者一系列动作重复运行到永远。你可以通过这个特性生成循环动画。

以下代码会让一个节点永远旋转下去,就像一个永远旋转的轮子:
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:2 angle:360];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy];
[myNode runAction:repeat];


舒缓动作 (Ease Actions) 允许你改变在一段时间内发生的动作效果:

// 让myNode在3秒钟之内移动到100,200坐标点
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)];
// 节点应该慢慢启动,然后在移动过程中减速
CCEaseInOut* ease = [CCEaseInOut actionWithAction:move rate:4];
[myNode runAction:ease];

注:在上述例子中,舒缓动作是在节点上运行的,而不是在移动动作上运行。当你使用动作时,很容易忘记runAction那行代码里的动作。即使最有经验的cocos2d开发者也会犯这样的错误。如果你看到动作没有如你期望的那样工作的话,记得检查一下你是在运行正确的动作。如果你确定选择了正确的动作,但还是没有得到想要的结果,请确认正确的节点上执行动作。这是另一个很容易犯的错误。

Cocos2d实现了以下CCEaseAction类:
CCEaseBackIn, CCEaseBackInOut, CCEaseBackOut
CCEaseBounceIn, CCEaseBounceInOut, CCEaseBounceOut
CCEaseElasticIn, CCEaseElasticInOut, CCEaseElasticOut
CCEaseExponentialIn, CCEaseExponentialInOut, CCEaseExponentialOut
CCEaseIn, CCEaseInOut, CCEaseOut
CCEaseSineIn, CCEaseSineInOut, CCEaseSineOut


动作序列 (Action Sequences):

以下代码让一个标签的颜色从红色变为蓝色,最后变为绿色:

CCTintTo* tint1 = [CCTintTo actionWithDuration:4 red:255 green:0 blue:0];
CCTintTo* tint2 = [CCTintTo actionWithDuration:4 red:0 green:0 blue:255];
CCTintTo* tint3 = [CCTintTo actionWithDuration:4 red:0 green:255 blue:0];
CCSequence* sequence = [CCSequence actions:tint1, tint2, tint3, nil];
[label runAction:sequence];

可以将动作序列与CCRepeatForever动作结合使用:
CCSequence* sequence = [CCSequence actions:tint1, tint2, tint3, nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
[label runAction:repeat];


即时动作(Instant Actions):

即时动作的存在是为动作序列服务的。有时候在一个动作序列里你必须改变某 些节点的属性,像可视性或者位置,改变完成以后会继续当前的动作序列。即 时动作让这样的应用变得可能。其中用的最多的可能是CCCallFunc动作。
当你使用一个动作序列时,你可能需要在某个时间得到通知。比如当一个动作 序列完成运行以后,你想知道这个动作序列已经完成,得到通知以后,你就可 以接着继续另一个动作序列。以下代码重写了之前的颜色改变动作序列的例子, 它会在每次完成一个CCTintTo动作以后调用三个CCCallFunc动作中的一个来发送信息:
CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onCallFunc)];
CCCallFuncN* funcN = [CCCallFuncN actionWithTarget:self selector:@selector(onCallFuncN:)];
CCCallFuncND* funcND = [CCCallFuncND actionWithTarget:self selector:@selector(onCallFuncND:data:) data:(void*)self];
CCSequence* seq = [CCSequence actions:tint1, func, tint2, funcN, tint3, funcND, nil];
[label runAction:seq];

上述动作序列将调用以下代码来发送信息。sender这个参数继承自CCNode;这 是运行动作的节点。data参数可以是任何值,结构或者指针。只是你必须正确 地转换data指针的类型。
-(void) onCallFunc
{
    CCLOG(@"end of tint1!");
    //注:和菜单项一样,一串动作最后总是要用nil来结束。如果你忘记在最后用nil结束参数的话,CCSequence这串代码将会崩溃!
}
-(void) onCallFuncN:(id)sender {
     CCLOG(@"end of tint2! sender: %@", sender); }
-(void) onCallFuncND:(id)sender data:(void*)data {
    // 如下转换指针的方式要求data必须是一个CCSprite
    CCSprite* sprite = (CCSprite*)data;
    CCLOG(@"end of sequence! sender: %@ - data: %@", sender, sprite);
}

当然,CCCallFunc也可以和CCRepeatForever一起使用。这样,你所指定的方法 将会被重复调用。


停止动作、清除对象:

stopAllActions  停止的是所有的action动作,不清除对象。

cleanup 停止的对象是调用cleanup的对象。

移除Sprite:

-(void)spriteMoveFinished:(id)sender {

    CCSprite *sprite = (CCSprite *)sender;

    [self removeChild:sprite cleanup:YES];
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics