Cocos2d-iphone magic Tower, layer 20, part 7

Source: Internet
Author: User

First, we need to make some preparations. Here I will not talk about how to make a game map. You can make it by yourself based on online games, you can also download the game code source I provided, which contains a full set of maps. Now let's get started. Now we define a variable int curfloor (current floor) in Game01.h, and assign it 0: curfloor = 0 before loading the map in Game01.m. Replace 1 in the first line of the loaded map with curfloor. Code: [html] // load map self. curtitleMap = [self. model. titleMapArray1 objectAtIndex: curfloor]; then we modify the warrior's location code as follows: www.2cto.com [html] _ hero. position = CGPointMake (LEFTMIN + (self. curtitleMap. down. x * 32 * _ scale), DOWNMIN + (10-self. curtitleMap. down. y) * 32 * _ scale). After running the code, our warriors enter our order: But you will find that our warriors cannot go up through the fairy, in canMoveTo: In this method, add the following code [html] _ hero in the if (npc_tileGid) loop. isFighting = YES; canmove = NO; NSDictiona Ry * props = [self. curtitleMap propertiesForGID: npc_tileGid]; NSString * value = [props valueForKey: @ "npc"]; int type = [value intValue]; switch (type) {case 0: herohp. yellowKey ++; herohp. blueKey ++; herohp. redKey ++; [herohp updateKey1]; [self. curtitleMap. npc removeTileAt: towerLoc]; _ hero. isFighting = NO; break; case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: br Eak; case 7: break; case 8: break; case 9: break; case 10: break; case 21: break; default: break ;} here, we will not add the npc conversation content, and then add it later. When we meet the fairy, the fairy will give you three keys, so here we add a pair of keys, and then continue to go up. After walking down the stairs, we will not be able to move. Next we will start to add the code for traversing the floor: first we will add a method: [html] // traversing the floor-(void) updateGameLayer :( int) the layer method has a parameter used to change the curfloor (current floor): curfloor + = layer; next, we will add an update method [html]-(void) updateFloor :( int) floor {if (floor = 0) [floorValue setString: @ "collation"]; else [floorValue setString: [NSString stringWithFormat: @ "% d layer", floor];} with the update method, we have to call it, add the update method to the floor crossing method: [html] [herohp updateFloor: curfloor]; Next we will add two generations Code: (critical) [html] [self removeChild: self. curtitleMap cleanup: YES]; [self removeChild: _ hero cleanup: YES]; here we remove the current game map and warriors from the scenario, then, add a new map and the warrior to the scene: [html] self. curtitleMap = [self. model. titleMapArray1 objectAtIndex: curfloor]; self. curtitleMap. scale = _ scale; self. curtitleMap. position = ccp (LEFTMIN, DOWNMIN); [self addChild: self. curtitleMap]; [self. curtitleMap titledMapAnalytic]; [self addChild: _ hero ]; We want to update the positions of the warriors. The corresponding locations are transmitted Based on the upper and lower floors: [html] CGPoint pos; if (layer> 0) {pos = CGPointMake (LEFTMIN + (self. curtitleMap. down. x * 32 * _ scale), DOWNMIN + (10-self. curtitleMap. down. y) * 32 * _ scale);} else pos = CGPointMake (LEFTMIN + (self. curtitleMap. up. x * 32 * _ scale), DOWNMIN + (10-self. curtitleMap. up. y) * 32 * _ scale); [_ hero setPosition: pos]; now the transfer method has been called. The Code is as follows: [html] if (upfloor_tileGid) {canmove = NO; [Self updateGameLayer: 1];} if (downfloor_tileGid) {canmove = NO; [self updateGameLayer:-1];} run the following command to traverse the floor, let's get excited !!! The code added today is not much, but it is very important. digest it first.

Related Article

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.