Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH 2, cocos2d-x_2

Source: Internet
Author: User

Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH 2, cocos2d-x_2

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 (); images-> images ("map/player?anim.plist", "map/player=anim.png"); // create a frame cache for player2 and load the animated image of player2 to the cache watermark = SpriteFrameCache :: getInstance (); transform-> transform ("map/player2_anim.plist", "map/player2_anim.png"); // create a Vector <SpriteFrame *> Vector in the upper, lower, and left directions of player1; vector <SpriteFrame *> Vector; Vector <SpriteFrame *> playerincluanim_down_vector; Vector <SpriteFrame *> Vector; // create a VectorVector in the upper, lower, and left directions of player2 <SpriteFrame *> Vector; vector <SpriteFrame *> memory; 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 images represent the animation to the left, and these four images are taken out of the cache, save them to the vector of the corresponding role for (int I = 1; I <= 4; I ++) {sprintf (name, "player?anim_=02d.png", I ); audience (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, "audience", I); audience (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 ); audience (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, "audience", I); audience (response-> getSpriteFrameByName (name ));} // The 9-12 pictures represent the down animation for (int I = 9; I <= 12; I ++) {sprintf (name, "player?anim_=02d.png", I ); audience (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, "audience", I); audience (response-> getSpriteFrameByName (name ));} // The 13-16 pictures represent the up animation for (int I = 13; I <= 16; I ++) {sprintf (name, "player?anim_=02d.png", I ); activities (playerincluspriteframecache-> getSpriteFrameByName (name); sprintf (name, "player2_anim_%02d.png", I);} <span style = "white-space: pre "> </span> // create an Animation * Animation = Animation: createWithSpriteFrames (Animation, 0.1f) based on the upper, lower, and left vectors of the role. The Animation * playeranimation animation_right = Animation:: Animation (Animation, 0.1f); Animation * Animation = Animation: createWithSpriteFrames (Animation, 0.1f); Animation * Animation = Animation: createWithSpriteFrames (Animation, 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_up = Animation: createWithSpriteFrames (watermark, 0.1f); // create an Animation player1_animate_left = Animate :: create (animation); player1_animate_right = Animate: create (player1_animation_right); player1_animate_down = Animate: create (animation); player1_animate_up = Animate: create (animation); animation = Animate :: create (animation); player2_animate_right = Animate: create (player2_animation_right); player2_animate_down = Animate: create (animation); player2_animate_up = Animate: create (animation );}

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, as the role's initial image SpriteFrame * spf1 = role-> getSpriteFrameByName ("player?anim_01.png"); player1 = RicherPlayer: create ("player1", spf1, false); // according _ Returns the number of coordinates of a vector. A Random idint _ rand1 = rand () % (wayLayerPass_vector.size (); log ("rand % d", _ rand1); // Based on the 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/ti LedHeight; 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.




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


I almost forgot the code.

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

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.