在编写获取路径方法前,我们先把角色需要的动画文件加载进来,角色的文件为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的上下左右四个方向的Vector Vector<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,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,i); player1_anim_right_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name)); sprintf(name,i); player2_anim_right_vector.pushBack(player2_spriteFrameCache->getSpriteFrameByName(name)); } //第9-12张图片是表示向下的动画 for (int i=9; i<=12; i++) { sprintf(name,i); player1_anim_down_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name)); sprintf(name,i); player2_anim_down_vector.pushBack(player2_spriteFrameCache->getSpriteFrameByName(name)); } //第13-16张图片是表示向上的动画 for (int i=13; i<=16; i++) { sprintf(name,i); player1_anim_up_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name)); sprintf(name,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,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的坐标数量,取得随机的一个id int _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",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