cocosdxnaTweejump学习笔记3

CCSpriteBatchNode spriteManager = CCSpriteBatchNode.batchNodeWithFile("Images/sprites", 10);
addChild(spriteManager, -1, (int)tags.kSpriteManager);
           
CCSprite background = CCSprite.spriteWithTexture(spriteManager.Texture, new CCRect(0, 0, 320, 480));
spriteManager.addChild(background);

background.position = new CCPoint(240, 400);

CCSprite logo = CCSprite.spriteWithFile(@"Images\logo");
logo.position = new CCPoint(240, 650);
addChild(logo);

两种实例化ccsprite对象方法的区别:

1.是建立在spritemanager中所加载的图片的基础上(该图片里有多个元素见Tweejump的image里的sprites)然后用

spriteWithTexture(spriteManager.Texture, new CCRect(0, 0, 320, 480));第二个参数进行具体位置的定位。从而绑定数据。

2.直接绑定到具体某个文件夹下的位置。

一般实例化CCSprite(该对象只有一个,且位置不变,如background、logo等)的步骤,先绑定图片资源,然后具体的position,最后addchild();

如果对象 有多个,且位置不固定,如bunus等。先实例化弄个循环,把对象的个数作为条件完成ccsprites的构建( initPlatforms())。然后再循环的过程中具体化(initPlatform();   )每个对象的数据(spriteManager-getChildByTag...  、  CCSprite x=....  、  spriteManager.addChild(先, 3, xx);)。接着统一构建所有ccsprites的初始位置(不一定是位置)数据( resetPlatforms())。在构建的过程中具体化某个ccsprite(resetPlatform())。如下列代码:

//添加游戏中的平台        

void initPlatforms()        

{            

  currentPlatformTag = (int)tags.kPlatformsStartTag;            

  while (currentPlatformTag < (int)tags.kPlatformsStartTag + kNumPlatforms)            

  {                

    this.initPlatform();                

     currentPlatformTag++;            

  }

      this.resetPlatforms();        

}        

//初始化游戏中的平台数据        

void initPlatform()        

{            

  CCRect rect = new CCRect();     

     switch (random.Next() % 2)            

  {                

    case 0: rect = new CCRect(608, 64, 102, 36); break;                

     case 1: rect = new CCRect(608, 128, 90, 32); break;            

  }

       CCSpriteBatchNode spriteManager = (CCSpriteBatchNode)getChildByTag((int)tags.kSpriteManager);            

   CCSprite platform = CCSprite.spriteWithTexture(spriteManager.Texture, rect);            

   Scale(platform);           

    spriteManager.addChild(platform, 3, currentPlatformTag);        

}

 //3.初始化平台的位置        

void resetPlatforms()        

{            

//先统一设置所有平台的初始位置数据            

  currentPlatformY = -1;         

     currentPlatformTag = (int)tags.kPlatformsStartTag;      

     currentMaxPlatformStep = 60f;            

  currentBonusPlatformIndex = 0;            

  currentBonusType = 0;            

  platformCount = 0;

     while (currentPlatformTag < (int)tags.kPlatformsStartTag + kNumPlatforms)            

  {                

//逐个安排每个平台的位置               

      this.resetPlatform();                

      currentPlatformTag++;           

    }        

}        

//4.重新设置每个平台的位置        

void resetPlatform()        

{            

  if (currentPlatformY < 0)            

  {                

    currentPlatformY = 50f;            

  }            

  else            

  {                

     currentPlatformY += (random.Next() % (int)(currentMaxPlatformStep - kMinPlatformStep) + kMinPlatformStep) * sy;  

               if (currentMaxPlatformStep < kMaxPlatformStep)                

      {                    

        currentMaxPlatformStep += .5f;               

      }            

  }

  ……

}

原文地址:https://www.cnblogs.com/dieaz5/p/2592254.html