Cocos2D-X系列之RPG横版过关游戏完整版实例-2

  以下为几个使player运动的函数的实现

/*

void HelloWorld::pmhcollision()                                                  //

{

    CCPoint playerpoint = player->getPosition();

    //获得玩家的方位

    CCSize playersize = player->getContentSize();

    //获得玩家图片的尺寸

    CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);      //获得地图属性

    int indexx,indexy;

    char mch[256];

    CCTMXLayer* layer = map->layerNamed("logic");

    //获得tmx地图的logic层的layer

    for(int playery = playerpoint.y - playersize.height;playery <= playerpoint.y;playery ++)

    {//转换为tile坐标

       indexx = (playerpoint.x - playersize.width / 2) / map->getTileSize().width;//

       indexy = (playerpoint.y) / map->getTileSize().height;

       CCPoint playerindex = ccp(indexx,indexy);

       int tilegid = layer->tileGIDAt(playerindex); //获取此方位的tilegidtiled定义的图片编号)

       if(tilegid > 0)

       {

          CCMutableDictionary<string,CCString *> *tiledic = map->propertiesForGID(tilegid);

          //获取tilegid上的properties的属性

          CCString *mvalue = tiledic->objectForKey("collion");

           //string转为int

          int mv = mvalue->toInt();                                              //

          if(mv == 2)

          {

             xVel = 0;

             playerpoint.x = (indexx + 1) * map->getTileSize().width + playersize.width / 2;

             player->setPosition(playerpoint);//刷新player坐标

          }

          return;

       }

       indexx = (playerpoint.x + playersize.width / 2 - 1) / map->getTileSize().width;//

       indexy = (playerpoint.y) / map->getTileSize().height;

       playerindex = ccp(indexx,indexy);                                          //

       tilegid = layer->tileGIDAt(playerindex);

       if(tilegid > 0)

       {

          CCMutableDictionary<string,CCString *> *tiledic = map->propertiesForGID(tilegid);

          CCString *mvalue = tiledic->objectForKey("collion");

          int mv = mvalue->toInt();                                               //

          if(mv == 2)

          {

             xVel= 0;

             playerpoint.x = (indexx) * map->getTileSize().width - playersize.width / 2;

             player->setPosition(playerpoint);

          }

          return;

       }

    }

}

void HelloWorld::pmvcollision()                                                    //

{

    CCPoint playerpoint = player->getPosition();

    CCSize playersize = player->getContentSize();

    CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);              //

    int indexx,indexy;

    CCTMXLayer* layer = map->layerNamed("logic");                                  //

    indexx = (playerpoint.x) / map->getTileSize().width;                           //

    indexy = map->getMapSize().height - (playerpoint.y) / map->getTileSize().height;

    CCPoint playerindex = ccp(indexx,indexy);

    int tilegid = layer->tileGIDAt(playerindex);                                   //

    if(tilegid > 0)

    {

       CCMutableDictionary<string,CCString *> *tiledic = map->propertiesForGID(tilegid);

       CCString *mvalue = tiledic->objectForKey("collion");                        //

       int mv = mvalue->toInt();

       if(mv == 1)                                                                 //

       {

           if(yVel < 0)

           {

 

               xVel = 0;

               yVel = 0;

               jump=true;

               playerpoint.y = (map->getMapSize().height - indexy) * map->getTileSize().height;

               player->setPosition(playerpoint);                                    //

           }

           return;

       }

    }

    if(yVel>0)

    {

        jump=false;

    }

 

    yVel -= 0.1;

   

}

*/

Cocos2dx完美支持tiled制作的tmx格式图片,此格式为tilemap图片,方便了场景的制作,降低了工作量。

Player的准备工作已完成,接下来就开始让player人物动起来

你可能感兴趣的:(开发,cocos2dx)