Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH

Source: Internet
Author: User

Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH

Before writing the method for obtaining the path, we first load the animation file required by the role. The file of the role is in png and plist formats.

Player?anim.png.plist player=anim.png

Player2_anim.png.plist player2_anim.png

Plist records the position and size of each image in the whole png image. We only need to know the name of each small image to extract the desired small image from the whole png image.

Playerpolicanim.png:

 

 

Player2_anim.png:


 

 

The image representation is the same, and each of the four images indicates one direction.


 

We add a role in GameBaseScene:

1. First define the addPlayerAnimation () method. This method is mainly used to load the animation file to the memory and create the animation in the upper, lower, and left directions required by the role.

 

Void GameBaseScene: addPlayerAnimation () {// create the player1 frame cache and load the player1 animated image to the cache player1_spriteFrameCache = SpriteFrameCache: getInstance (); example-> example (map/player1_anim.plist, map/player1_anim.png); // create a frame cache for player2 and load the animated image of player2 to the cache example = SpriteFrameCache: getInstance (); player2_spriteFrameCache-> addSpriteFramesWithFile (map/player2_anim.plist, map/player2_anim.png); // create a Vector in the upper, lower, and left directions of player1.
 
  
Playerincluanim_left_vector; Vector
  
   
Playerincluanim_right_vector; Vector
   
    
Playerincluanim_down_vector; Vector
    
     
Playerincluanim_up_vector; // creates a VectorVector in the upper, lower, and left directions of player2.
     
      
Player2_anim_left_vector; Vector
      
        Player2_anim_right_vector; Vector
       
         Player2_anim_down_vector; Vector
        
          Player2_anim_up_vector;} // defines the name array char name [20]; memset (name, 0, 20); // The first 4 pictures represent the animation to the left, extract the four images from the cache and save them to the vector of the corresponding role. for (int I = 1; I <= 4; I ++) {sprintf (name, response, I); Response (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, region, I); Response (response-> getSpriteFrameByName (name ));} // The 5-8 images are animated to the right (int I = 5; I <= 8; I ++) {sprintf (name, player?anim_=02d.png, I ); activities (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, marker, I); Activities (player2_spriteFrameCache-> getSpriteFrameByName (name ));} // The 9-12 pictures represent the down animation for (int I = 9; I <= 12; I ++) {sprintf (name, player?anim_=02d.png, I ); activities (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, marker, I); Activities (player2_spriteFrameCache-> getSpriteFrameByName (name ));} // The 13-16 pictures represent the up animation for (int I = 13; I <= 16; I ++) {sprintf (name, player?anim_=02d.png, I ); animation (response-> getSpriteFrameByName (name); sprintf (name, marker, I);} // create an Animation in four directions based on the upper, lower, and left vectors of the role. * player=animation_left = Animation:: Animation (watermark, 0.1f); Animation * player1_animation_right = Animation: Animation (watermark, 0.1f); Animation * Animation = Animation: createWithSpriteFrames (watermark, 0.1f ); animation * Animation = Animation: createWithSpriteFrames (Animation, 0.1f); Animation * Animation = Animation: createWithSpriteFrames (Animation, 0.1f); Animation * Animation = Animation: Animation, 0.1f); Animation * player2_animation_down = Animation: createWithSpriteFrames (parts, 0.1f); Animation * parts = Animation: createWithSpriteFrames (parts, 0.1f ); /// create an animation in four directions based on the top, bottom, left, and right actions of the role. player?animate_left = Animate: create (animation); player=animate_right = Animate: create (player=animate_down = Animate :: create (player1_animation_down); player1_animate_up = Animate: create (empty); animation = Animate: create (empty); player2_animate_right = Animate: create (empty); animation = Animate :: create (player2_animation_down); player2_animate_up = Animate: create (player2_animation_up );}
        
       
      
     
    
   
  
 

 

2. After creating the files required by the role, the role must be located on the map. Because the paths that can be taken are in the way layer, in the setWayPassToGrid () method, we save the sprite coordinates in the way layer to wayLayerPass_vector, so that we can set the role position based on the coordinates.

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; // obtain the coordinates of the position and save it to Vec2 p = _ sp-> getPosition (); wayLayerPass_vector.push_back (p) in the wayLayerPass_vector object ); log (canPassGrid row = % d, col = % d, canpass = % d, row, col, canPassGrid [row] [col]) ;}} log (setWayPassToGrid finished );}

3. The added role is first encapsulated into a RicherPlayer class, which records the role information, including the role name, capital, physical strength, and friends.

 

 

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. Then, in the addPlayer () method, randomly retrieve the coordinates from the wayLayerPass_vector container and add the role.

 

 

Void GameBaseScene: addPlayer () {// specify the random seed. The Random Seed is generated based on the seed at the current time to generate the Random Seed: struct timeval now; gettimeofday (& now, NULL ); // calculate the time seed unsigned rand_seed = (unsigned) (now. TV _sec * 1000 + now. TV _usec/1000); // initialize the random number srand (rand_seed); // obtain the first image from the frame cache image as the role's initial image SpriteFrame * spf1 = player=spriteframecache-> cached ); player1 = RicherPlayer: create (player1, spf1, false); // Based on wayLayerPass_vect Or, get a random idint _ rand1 = rand () % (wayLayerPass_vector.size (); log (rand % d, _ rand1); // according to id, take out the coordinates Vec2 vec2ForPlayer1 = wayLayerPass_vector.at (_ rand1); // we add a tiledHeight to the vertical position to align the role in the road. vec2ForPlayer1. y + = tiledHeight; // set the position of the role and the anchpoint player1-> setPosition (vec2ForPlayer1); player1-> setAnchorPoint (ccp (0, 0.5 )); // log-related int col = vec2ForPlayer1. x/tiledWidth; int row = vec2ForPlayer1. y/tiledHei Ght; log (player1 position row = % d, col = % d, row, col); log (player1 position x = % f, y = % f, vec2ForPlayer1. x, vec2ForPlayer1. y); // Add the role to the map scenario addChild (player1); add role 2 in the same way as role 1, so do not repeat .....................}

 

Test OK. Now we can see two roles.



 

 

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.