Cocos2d-x 3.2 Monopoly Game project development-part seventh get the role path _2

Source: Internet
Author: User
Tags random seed

Before writing the Get path method, we first load the animation files that the characters need, and the characters ' files are in PNG and plist format.

Player1_anim.png.plist Player1_anim.png

Player2_anim.png.plist Player2_anim.png

Plist the size of each image in the entire PNG image, we can intercept the desired small image from the entire PNG image by knowing the name of each small image.

Player1_anim.png picture for


Player2_anim.png picture for



The image is represented in the same way, each four sheets representing One Direction


We add roles in Gamebasescene:

1, first define the Addplayeranimation () method, this method is mainly to load the animation file into memory and create the character needs the next and the left and right four direction of animation

void Gamebasescene::addplayeranimation () {//Create Player1 frame cache and load Player1 animated picture to cache Player1_spriteframecache = Spriteframe Cache::getinstance ();p layer1_spriteframecache->addspriteframeswithfile ("Map/player1_anim.plist", "map/       Player1_anim.png "); Create a player2 frame cache and load the Player2 animated picture to the cache Player2_spriteframecache = Spriteframecache::getinstance ();p layer2_ Spriteframecache->addspriteframeswithfile ("Map/player2_anim.plist", "map/player2_anim.png");// Create a Vector vector<spriteframe*> player1_anim_left_vector in the upper and lower left and right four directions of the player1; Vector<spriteframe*> Player1_anim_right_vector; Vector<spriteframe*> Player1_anim_down_vector; Vector<spriteframe*> player1_anim_up_vector;//Create Player2 in the upper and lower left and right four directions vectorvector<spriteframe*> player2_ Anim_left_vector; Vector<spriteframe*> Player2_anim_right_vector; Vector<spriteframe*> Player2_anim_down_vector;        Vector<spriteframe*> Player2_anim_up_vector;} Defines the name array char name[20];memset (name, 0, 20);//第1-4 picture is a left-hand animation that takes these four pictures out of the cache,The vectors of the respective roles are saved for (int i=1; i<=4; i++) {sprintf (name, "Player1_anim_%02d.png", i);p Layer1_anim_left_ Vector.pushback (Player1_spriteframecache->getspriteframebyname (name)); sprintf (name, "Player2_anim_%02d.png", i);p Layer2_anim_left_vector.pushback (player2_spriteframecache->getspriteframebyname (name));} 第5-8 picture is a right animation for (int i=5; i<=8; i++) {sprintf (name, "Player1_anim_%02d.png", i);p layer1_anim_right_ Vector.pushback (Player1_spriteframecache->getspriteframebyname (name)); sprintf (name, "Player2_anim_%02d.png", i);p Layer2_anim_right_vector.pushback (player2_spriteframecache->getspriteframebyname (name));} 第9-12 picture is an animated for (int i=9; i<=12; i++) that represents the downward animation {sprintf (name, "Player1_anim_%02d.png", i);p Layer1_anim_down_ Vector.pushback (Player1_spriteframecache->getspriteframebyname (name)); sprintf (name, "Player2_anim_%02d.png", i);p Layer2_anim_down_vector.pushback (player2_spriteframecache->getspriteframebyname (name));} 第13-16 picture is an animated for (int i=13; i<=16; i++) that represents the upward animation {sprintf (name, "Player1_anim_%02d.png", i);p Layer1_anim_up_vector.pushback (Player1_spriteframecache->getspriteframebyname ( name)); sprintf (name, "Player2_anim_%02d.png", I);} <span style= "White-space:pre" ></span>//create four-direction actions based on the upper and lower four vectors of the character 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 (player1_animation_left);p layer1_animate_right = Animate::create (player1_animation_right);p Layer1_animate_down = Animate::create (player1_animation_down);p layer1_animate_up = Animate::create (player1_animation_up);p layer2_ Animate_left = Animate::create (player2_animation_left);p layer2_animate_right = Animate::create (player2_animation_ right);p Layer2_animate_down = Animate::create (player2_animation_down);p layer2_animate_up = Animate::create (player2 _ANIMATION_UP);}

2, after creating the files required by the role, but also need the role in the map location, because the path can be walked in the way layer, in the Setwaypasstogrid () method, we have to save the road layer in the coordinates of the sprite in the Waylayerpass_vector, This allows you to set the position of the role based on its coordinates.

void  Gamebasescene::setwaypasstogrid () {tmxlayer* Waylayer = _map->layernamed ("the"); 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/tiledh Eight;canpassgrid[row][col] = true;//Gets the coordinates of the position, saved to the object 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. Added roles we first encapsulate the Richerplayer class, which records the role's information, including the role name, money, physical strength, foe


richerplayer* richerplayer::create (char* name,spriteframe* spriteframe,bool enemy,int money,int strength) { richerplayer* player = new Richerplayer ();p layer->init (Name,spriteframe, enemy,money,strength);p layer-> 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. Next, in the Addplayer () method, the coordinates are randomly taken from the container waylayerpass_vector to add the roles.

void Gamebasescene:: Addplayer () {//Specify random number seed, random number based on this seed produces random seed with current time: struct timeval now; Gettimeofday (&now, NULL);     Calculate time seed unsigned rand_seed = (unsigned) (now.tv_sec*1000 + now.tv_usec/1000); Initializes the random number Srand (rand_seed);//Take the first picture from the frame cache image as the initial image of the character spriteframe* spf1 = player1_spriteframecache-> Getspriteframebyname ("Player1_anim_01.png");p layer1 = richerplayer::create ("Player1", spf1,false);// Based on the number of coordinates of the waylayerpass_vector, a random idint _rand1 = rand ()% (Waylayerpass_vector.size ()) is obtained; Log ("Rand%d", _RAND1);//based on the ID, remove the coordinates Vec2 vec2forplayer1 = waylayerpass_vector.at (_RAND1);// This we add a tiledheight height to the vertical position in order to have the character centered on the road vec2forplayer1.y +=tiledheight; Sets the location of the role, as well as the anchor Point Player1->setposition (vec2forplayer1);p Layer1->setanchorpoint (CCP (0,0.5));//log related int col = Vec2forplayer1.x/tiledwidth;int row = Vec2forplayer1.y/tiledheight;log ("player1 position row=%d, col =%d", row,col); Lo G ("Player1 position x=%f, y =%f", vec2forplayer1.x, VEC2FORPLAYER1.Y);//Add role to map scene Addchild (player1), add role 2 to the role 1 methodThe same, no longer to be described ..........} 

Test OK you can already see 2 characters.




To be continued ........ .........


Almost forgot the code.

Click Download code http://download.csdn.net/detail/lideguo1979/8281909

Cocos2d-x 3.2 Monopoly Game project development-part seventh get the role path _2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.