Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2

来源:互联网 发布:中国大数据峰会 编辑:程序博客网 时间:2024/05/02 22:48

在编写获取路径方法前,我们先把角色需要的动画文件加载进来,角色的文件为png 和 plist格式。

player1_anim.png.plist             player1_anim.png  

player2_anim.png.plist             player2_anim.png

plist分别记录了每张图在整张png图中的位置 大小,我们只要知道每张小图名称即可从整张png图中截取出想要的小图。

player1_anim.png图片为


 

player2_anim.png图片为



图片表示方法相同,每四张表示一个方向


我们在GameBaseScene中添加角色:

1、 首先定义addPlayerAnimation()方法,这个方法主要是加载动画文件到内存 并创建角色需要的上下左右四个方向的动画

void GameBaseScene::addPlayerAnimation(){        //创建player1的帧缓存,并加载player1的动画图片到缓存player1_spriteFrameCache = SpriteFrameCache::getInstance();player1_spriteFrameCache->addSpriteFramesWithFile("map/player1_anim.plist","map/player1_anim.png");       //创建player2的帧缓存,并加载player2的动画图片到缓存player2_spriteFrameCache = SpriteFrameCache::getInstance();player2_spriteFrameCache->addSpriteFramesWithFile("map/player2_anim.plist","map/player2_anim.png");//创建player1的上下左右四个方向的Vector Vector<SpriteFrame*> player1_anim_left_vector ;Vector<SpriteFrame*> player1_anim_right_vector;Vector<SpriteFrame*> player1_anim_down_vector;Vector<SpriteFrame*> player1_anim_up_vector;//创建player2的上下左右四个方向的VectorVector<SpriteFrame*> player2_anim_left_vector;Vector<SpriteFrame*> player2_anim_right_vector;Vector<SpriteFrame*> player2_anim_down_vector;Vector<SpriteFrame*> player2_anim_up_vector;}        //定义name数组char name[20];memset(name, 0, 20);//第1-4张图片是表示向左的动画,把这四张图片从缓存中取出,分别保存到相应角色的vector中for (int i=1; i<=4; i++) {sprintf(name, "player1_anim_%02d.png",i);player1_anim_left_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name));sprintf(name, "player2_anim_%02d.png",i);player2_anim_left_vector.pushBack(player2_spriteFrameCache->getSpriteFrameByName(name));}//第5-8张图片是表示向右的动画for (int i=5; i<=8; i++) {sprintf(name, "player1_anim_%02d.png",i);player1_anim_right_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name));sprintf(name, "player2_anim_%02d.png",i);player2_anim_right_vector.pushBack(player2_spriteFrameCache->getSpriteFrameByName(name));}//第9-12张图片是表示向下的动画for (int i=9; i<=12; i++) {sprintf(name, "player1_anim_%02d.png",i);player1_anim_down_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name));sprintf(name, "player2_anim_%02d.png",i);player2_anim_down_vector.pushBack(player2_spriteFrameCache->getSpriteFrameByName(name));}//第13-16张图片是表示向上的动画for (int i=13; i<=16; i++) {sprintf(name, "player1_anim_%02d.png",i);player1_anim_up_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name));sprintf(name, "player2_anim_%02d.png",i);}<span style="white-space:pre"></span>//根据角色的上下左右四个vector 创建四个方向的动作Animation * player1_animation_left = Animation::createWithSpriteFrames(player1_anim_left_vector,0.1f);Animation * player1_animation_right = Animation::createWithSpriteFrames(player1_anim_right_vector,0.1f);Animation * player1_animation_down = Animation::createWithSpriteFrames(player1_anim_down_vector,0.1f);Animation * player1_animation_up = Animation::createWithSpriteFrames(player1_anim_up_vector,0.1f);Animation * player2_animation_left = Animation::createWithSpriteFrames(player2_anim_left_vector,0.1f);Animation * player2_animation_right = Animation::createWithSpriteFrames(player2_anim_right_vector,0.1f);Animation * player2_animation_down = Animation::createWithSpriteFrames(player2_anim_down_vector,0.1f);Animation * player2_animation_up = Animation::createWithSpriteFrames(player2_anim_up_vector,0.1f);///根据角色的上下左右四个动作  创建四个方向的动画player1_animate_left = Animate::create(player1_animation_left);player1_animate_right = Animate::create(player1_animation_right);player1_animate_down = Animate::create(player1_animation_down);player1_animate_up = Animate::create(player1_animation_up);player2_animate_left = Animate::create(player2_animation_left);player2_animate_right = Animate::create(player2_animation_right);player2_animate_down = Animate::create(player2_animation_down);player2_animate_up = Animate::create(player2_animation_up);}

2、 创建完角色需要的文件后,还需要角色在地图的位置,由于能走的路径都在way图层中,在setWayPassToGrid()方法中,我们把way图层中sprite的坐标保存到wayLayerPass_vector中,这样就可以根据其中的坐标设置角色的位置了

void  GameBaseScene::setWayPassToGrid(){TMXLayer* wayLayer = _map->layerNamed("way");Size _mapSize = wayLayer->getLayerSize(); for (int j = 0;  j < _mapSize.width; j++) {  for (int i = 0;  i < _mapSize.height; i++) {  Sprite* _sp = wayLayer->tileAt(Point(j, i));  if (_sp) {  float x = _sp->getPositionX();float y = _sp->getPositionY();int col = x/tiledWidth;int row = y/tiledHeight;canPassGrid[row][col] = true;//取得该位置的坐标,保存到对象wayLayerPass_vector中Vec2 p = _sp->getPosition();wayLayerPass_vector.push_back(p);log("canPassGrid row=  %d ,col =%d ,canpass = %d" ,row,col,canPassGrid[row][col]);}  }  }  log("setWayPassToGrid finished");}

3、 添加的角色我们先封装成一个RicherPlayer类,该类记录角色的信息,包括角色名称、资金、体力、敌友


RicherPlayer* RicherPlayer::create(char* name,SpriteFrame* spriteFrame,bool enemy,int money,int strength){RicherPlayer* player = new RicherPlayer();player->init(name,spriteFrame, enemy,money,strength);player->autorelease();return player;}bool RicherPlayer::init(char* name,SpriteFrame* spriteFrame,bool enemy,int money,int strength){Sprite::initWithSpriteFrame(spriteFrame);_name = name;_enemy = enemy;_money = money;_strength = strength;return true;}

4、接下来在addPlayer()方法中,从容器wayLayerPass_vector中随机取出坐标,进行角色的添加。

void GameBaseScene:: addPlayer(){//指定随机数种子,随机数依据这个种子产生 采用当前时间生成随机种子:struct timeval now; gettimeofday(&now, NULL); //计算时间种子unsigned rand_seed = (unsigned)(now.tv_sec*1000 + now.tv_usec/1000);     // 初始化随机数   srand(rand_seed);//从帧缓存图片中取第一张,做为角色的初始图片SpriteFrame* spf1 = player1_spriteFrameCache->getSpriteFrameByName("player1_anim_01.png");player1 = RicherPlayer::create("player1",spf1,false);//根据wayLayerPass_vector的坐标数量,取得随机的一个idint _rand1 = rand()%(wayLayerPass_vector.size()); log("rand %d" ,_rand1);//根据id,取出其中的坐标Vec2 vec2ForPlayer1 = wayLayerPass_vector.at(_rand1);//这个我们给纵向位置添加一个tiledHeight高度,目的是为了让角色居中显示在道路中vec2ForPlayer1.y +=tiledHeight; //设置角色的位置,以及锚点player1->setPosition(vec2ForPlayer1);player1->setAnchorPoint(ccp(0,0.5));//log 相关int col = vec2ForPlayer1.x/tiledWidth;int row = vec2ForPlayer1.y/tiledHeight;log("player1 position row=  %d ,col = %d" ,row,col);log("player1 position x=  %f ,y = %f" , vec2ForPlayer1.x, vec2ForPlayer1.y);//添加角色到地图场景addChild(player1);角色2的添加同角色1方法相同,不再累述…………………}

测试ok 已经可以看到2个角色了




未完待续.......................


差点忘了代码 

点击下载代码     http://download.csdn.net/detail/lideguo1979/8281909

3 0
原创粉丝点击