(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.3运动的精灵

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 

1.3运动的精灵

 

是时候为我们的精灵们加入一些动作了。需要强调的是,动作正如你想做的那么复杂。在这次的教学中,我们将使用非常简单的动作去创建引人注目的效果。我们创建一个有着蝙蝠飞过恐怖城堡的场景。我们也将使用上一节创建剑芒的方法,来加入一个非常眩的闪电效果。

 
 

 

 

 

做好准备

 

请打开整个 RecipeCollection01项目文件。

 

如何工作:

 

为了代码的整洁,我们省略了一部分代码。

 

执行如下代码:

//SimpleAnimObject.h

@interface SimpleAnimObject : CCSprite

{

    intanimationType;

    CGPointvelocity;

}

@interface Ch1_AnimatingSprites

{

   NSMutableArray *bats;

    CCAnimation*batFlyUp;

    CCAnimation*batGlideDown;

    CCSprite*lightningBolt;

    CCSprite*lightningGlow;

    intlightningRemoveCount;

}

-(CCLayer*) runRecipe

{

    //将.plist文件加入 SpriteFrameCache

   [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"simple_bat.plist"];

    //加入闪电图片

   lightningBolt = [CCSprite spriteWithFile:@"lightning_bolt.png"];

   [lightningBolt setPosition:ccp(240,160)];

   [lightningBolt setOpacity:64];

   [lightningBolt retain];

    // 加入一个精灵来点亮其他区域

   lightningGlow = [CCSpritespriteWithFile:@"lightning_glow.png"];

   [lightningGlow setColor:ccc3(255,255,0)];

   [lightningGlow setPosition:ccp(240,160)];

   [lightningGlow setOpacity:100];

   [lightningGlow setBlendFunc: (ccBlendFunc) { GL_ONE, GL_ONE }];

   [lightningBolt addChild:lightningGlow];

    // 设置一个关于闪电随机状态的计数器

   lightningRemoveCount = 0;

    //初始化蝙蝠数组

    bats =[[NSMutableArray alloc] init];

    // 加入蝙蝠时使用批量渲染

   CCSpriteBatchNode *batch1 = [CCSpriteBatchNodebatchNodeWithFile:@"simple_bat.png" capacity:10];

    [selfaddChild:batch1 z:2 tag:TAG_BATS];

    //让蝙蝠开始飞.

    for(int x=0;x<10; x++)

    {

        //建立蝙蝠简单的动作

       SimpleAnimObject *bat = [SimpleAnimObject spriteWithBatchNode:batch1rect:CGRectMake(0,0,48,48)];

        [batch1addChild:bat];

        [batsetPosition:ccp(arc4random()%400+40, arc4random()%150+150)];

        // 让蝙蝠飞起来,设定动画间隔.

        floatflappingSpeed = [self makeBatFlyUp:bat];

        //设定速度

       bat.velocity = ccp((arc4random()%1000)/500 + 0.2f, 0.1f/flappingSpeed);

        //Add apointer to this bat object to the NSMutableArray

        [batsaddObject:[NSValue valueWithPointer:bat]];

        [batretain];

        //根据x方向的速度设定蝙蝠图像的正反.

       if(bat.velocity.x > 0)

        {

           bat.flipX = YES;

        }

    }

    //循环更新

    [selfschedule:@selector(step:)];

    return self;

}

-(float)makeBatFlyUp:(SimpleAnimObject*)bat

{

   CCSpriteFrameCache * cache = [CCSpriteFrameCachesharedSpriteFrameCache];

    //随机动作速率.

    float delay= (float)(arc4random()%5+5)/80;

    CCAnimation*animation = [[CCAnimation alloc] initWithName:@"simply_bat_fly"delay:delay];

    //随机动作频率.

    int num =arc4random()%4+1;

    for(int i=1;i<=4; i+=1)

    {

      [animationaddFrame:[cache spriteFrameByName:[NSString stringWithFormat:@"simple_bat_0%i.png",num]]];

      num++;

      if(num> 4)

      {

          num =1;

      }

    }

    //停止其他动作并执行飞行动作.

    [batstopAllActions];

    [batrunAction:[CCRepeatForever actionWithAction: [CCAnimateactionWithAnimation:animation]]];

    //保持蝙蝠的飞行状态.

   bat.animationType = BAT_FLYING_UP;

    returndelay;  //我们返回这个频率值.

}

-(void)makeBatGlideDown:(SimpleAnimObject*)bat

{

   CCSpriteFrameCache * cache = [CCSpriteFrameCachesharedSpriteFrameCache];

    //一个简单的单贞下落动作.

    CCAnimation*animation = [[CCAnimation alloc] initWithName:@"simple_bat_glide"delay:100.0f];

    [animationaddFrame:[cache spriteFrameByName:@"simple_bat_01.png"]];

    //停止其他动作并执行下落动作.

    [batstopAllActions];

    [batrunAction:[CCRepeatForever actionWithAction: [CCAnimateactionWithAnimation:animation]]];

    //保持下落的动作状态.

   bat.animationType = BAT_GLIDING_DOWN;

}

-(void)step:(ccTime)delta

{

    CGSize s =[[CCDirector sharedDirector] winSize];

    for(id keyin bats)

    {

        //通过蝙蝠数组获得蝙蝠对象.

       SimpleAnimObject *bat = [key pointerValue];

        //确保蝙蝠不会飞出屏幕

       if(bat.position.x > s.width)

        {

           bat.velocity = ccp(-bat.velocity.x, bat.velocity.y);

           bat.flipX = NO;

        }

        elseif(bat.position.x < 0)

        {

           bat.velocity = ccp(-bat.velocity.x, bat.velocity.y);

           bat.flipX = YES;

        }

        elseif(bat.position.y > s.height)

        {

           bat.velocity = ccp(bat.velocity.x, -bat.velocity.y);

           [self makeBatGlideDown:bat];

        }

        elseif(bat.position.y < 0)

        {

           bat.velocity = ccp(bat.velocity.x, -bat.velocity.y);

           [self makeBatFlyUp:bat];

        }

        //随机更改蝙蝠的动作

       if(arc4random()%100 == 7)

        {

           if(bat.animationType == BAT_GLIDING_DOWN)

            {

               [self makeBatFlyUp:bat];

  bat.velocity = ccp(bat.velocity.x,-bat.velocity.y);

            }

            elseif(bat.animationType == BAT_FLYING_UP)

            {

               [self makeBatGlideDown:bat];

  bat.velocity = ccp(bat.velocity.x,-bat.velocity.y);

            }

        }

        //更新蝙蝠的位置

       bat.position = ccp(bat.position.x + bat.velocity.x, bat.position.y +bat.velocity.y);

    }

    //随机制造闪电

   if(arc4random()%70 == 7)

    {

       if(lightningRemoveCount < 0)

        {

            [selfaddChild:lightningBolt z:1 tag:TAG_LIGHTNING_BOLT];

           lightningRemoveCount = arc4random()%5+5;

        }

    }

    //计数

   lightningRemoveCount -= 1;

    //通过tag删除闪电

   if(lightningRemoveCount == 0)

    {

        [self removeChildByTag:TAG_LIGHTNING_BOLTcleanup:NO];

    }

}

@end

 

 

它是如何工作的?

 

这个教程告诉我们,通过使用SimpleAnimObject类,如何构造动画:

 

        运动对象类的构造:

        当我们从一个动作过渡到另一个动作的时候,持续地跟踪运动物体处于的运动状态是很重要的。在我们这个例子中,我们可以给予对象一个随意的动作状态。我们也能控制一个与速度的Y方向值成反比的动画贞间隔的长短。

@interface SimpleAnimObject : CCSprite

{

    intanimationType;

    CGPointvelocity;

}

随着于你对你动作系统理解的深入,你可以掌握更多的信息,比如,运行CCAnimation单例的指针,逐贞信息,以及刚体的知识等等。

 

更多的事…

 

当你更多的参与到Cocos2d游戏的开发时,你将会更多尝试去使用游戏逻辑和AI的异步操作。也是从CCAction类中派生出来的,这些动作可以被使用到任何有关移动的行为,比如,用CCMoveBy方法移动一个CCNode对象,或者,使CCSprite类进行某个CCAnimate动作。当一个行动开始实施时,一个异步的时间机制在后台开始运转。开始的时候游戏开发者会非常依赖这个功能。当正在运行多个行动时,这个机制将会带来许多额外的内存开销。在接下来的例子中,我们将会使用简单的计数器,来允许我们去规范如何在屏幕上展现持续的闪电。

    //随机制造闪电

   if(arc4random()%70 == 7)

    {

       if(lightningRemoveCount < 0)

        {

           [self addChild:lightningBolt z:1 tag:TAG_LIGHTNING_BOLT];

           lightningRemoveCount = arc4random()%5+5;

        }

    }

    //计数

   lightningRemoveCount -= 1;

    //通过tag删除闪电

   if(lightningRemoveCount == 0)

    {

        [self removeChildByTag:TAG_LIGHTNING_BOLTcleanup:NO];

    }

同步计数器,正如我们在之前这个代码片段中所展示的那样,是比较常见的,更适用于异步行为。当你的游戏需要范围发光的时候,你可以用到这个案例。

 

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值