Cocos2d-x 3.2 大富翁遊戲項目開發-第七部分 擷取角色路徑_2,cocos2d-x_2

來源:互聯網
上載者:User

Cocos2d-x 3.2 大富翁遊戲項目開發-第七部分 擷取角色路徑_2,cocos2d-x_2

在編寫擷取路徑方法前,我們先把角色需要的動畫檔案載入進來,角色的檔案為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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.