Cocos2d-iphone magic tower 20 Layer 4

Source: Internet
Author: User

Next, we will add the warrior's mobile detection. Before we add the mobile detection, we will parse our own map. First, we will create a Class Based on cocos2d, Class Name: the TitledMap class inherits the CCTMXTiledMap class and is built as follows: Next we will add TitledMap. the code in h Previously I said that our map consists of multiple layers, therefore, in this class, each layer sets an attribute, and there are two warriors on the map described in the previous chapter, in fact, they represent two coordinate points: the upstairs and the downstairs, so we add two more attributes. The Code is as follows: [html] www.2cto.com @ property (nonatomic, retain) CCTMXLayer * wall; @ property (nonatomic, retain) CCTMXLayer * road; @ property (nonatomic, retain) CCTMXLayer * enemy; @ property (nona Tomic, retain) CCTMXLayer * item; @ property (nonatomic, retain) CCTMXLayer * upfloor; @ property (nonatomic, retain) CCTMXLayer * downfloor; @ property (nonatomic, retain) CCTMXLayer * door; @ property (nonatomic, retain) CCTMXLayer * other; @ property (nonatomic, retain) CCTMXLayer * npc; @ property (nonatomic, retain) CCTMXLayer * heroPoint; @ property (nonatomic, assign) CGPoint up; @ property (nonatomic, assign) CGP Oint down; then we add a class method + (TitledMap *) initWithAnalytic :( int) tileMapName; this class method is mainly used to load maps in other files, at the same time, this class method calls the initialization method [html] + (TitledMap *) initWithAnalytic :( int) tileMapName {return [[self alloc] initWithAnalytic: tileMapName] autorelease];}-(id) initWithAnalytic :( int) tileMapName {NSString * string = [NSString stringWithFormat: @ "% d. tmx ", tileMapName]; self = [super initWithTMXFile: string]; if (self) {self. ro Ad = [self layerNamed: @ "road"]; self. upfloor = [self layerNamed: @ "upfloor"]; self. downfloor = [self layerNamed: @ "downfloor"]; self. item = [self layerNamed: @ "item"]; self. enemy = [self layerNamed: @ "enemy"]; self. door = [self layerNamed: @ "door"]; self. npc = [self layerNamed: @ "npc"]; self. other = [self layerNamed: @ "other"]; self. heroPoint = [self layerNamed: @ "heroPoint"];} return self;} Next we will Remove [html]-(void) titledMapAnalytic {for (int x = 0; x <= 10; x ++) {for (int y = 0; y <= 10; y ++) {CGPoint towerLoc = CGPointMake (x, y); int heroPoint_tileGid = [self. heroPoint tileGIDAt: towerLoc]; if (heroPoint_tileGid) {NSDictionary * props = [self propertiesForGID: heroPoint_tileGid]; NSString * value = [props valueForKey: @ "point"]; int type = [value intValue]; if (type = 1) {self. up = TowerLoc;} else self. down = towerLoc; [self. heroPoint removeTileAt: towerLoc] ;}} return ;}here our map resolution is only a part of it. We need to add a simple and important class, it is the game data class GameModel. Next we will add the code in this class. I will not have to explain the code much. If you don't understand it, you can ask GameModel. h file code [html] // map set 1 @ property (nonatomic, retain) NSMutableArray * titleMapArray1; // map set 2 @ property (nonatomic, retain) NSMutableArray * titleMapArray2; // map set 3 @ property (nonatomic, retain) NSMutableArray * TitleMapArray3; + (GameModel *) getGameModel; // load map-(void) initWithMap; GameModel. m file code [html] + (TitledMap *) initWithAnalytic :( int) tileMapName {return [[self alloc] initWithAnalytic: tileMapName] autorelease];}-(id) initWithAnalytic :( int) tileMapName {NSString * string = [NSString stringWithFormat: @ "% d. tmx ", tileMapName]; self = [super initWithTMXFile: string]; if (self) {self. road = [self layerName D: @ "road"]; self. upfloor = [self layerNamed: @ "upfloor"]; self. downfloor = [self layerNamed: @ "downfloor"]; self. item = [self layerNamed: @ "item"]; self. enemy = [self layerNamed: @ "enemy"]; self. door = [self layerNamed: @ "door"]; self. npc = [self layerNamed: @ "npc"]; self. other = [self layerNamed: @ "other"]; self. heroPoint = [self layerNamed: @ "heroPoint"];} [self titledMapAnalytic]; return self;}-(voi D) titledMapAnalytic {for (int x = 0; x <= 10; x ++) {for (int y = 0; y <= 10; y ++) {CGPoint towerLoc = CGPointMake (x, y); int heroPoint_tileGid = [self. heroPoint tileGIDAt: towerLoc]; if (heroPoint_tileGid) {NSDictionary * props = [self propertiesForGID: heroPoint_tileGid]; NSString * value = [props valueForKey: @ "point"]; int type = [value intValue]; if (type = 1) {self. up = towerLoc;} else self. Down = towerLoc; [self. heroPoint removeTileAt: towerLoc] ;}} return ;}next we will add the following code [html] for (int I = 0; I <22; I ++) {TitledMap * titledMap = [TitledMap initWithAnalytic: I]; [self. titleMapArray1 addObject: titledMap];} the code is basically complete after adding our GameModel class. Next we will use the two classes we just added in Game01 // load the game map curTiledMap = [CCTMXTiledMaptiledMapWithTMXFile: @ "1.tmx"]; curTiledMap. scale = _ scale; curTiledMap. position = ccp (LEFTMIN, DOWNMIN); [selfaddChild: curTiledMap]; modified to [html] // initialize data self. model = [GameModel getGameModel]; [model initWithMap]; seltitleMapArray = self. model. titleMapArray1; // load the map self. curtitleMap = [self. model. titleMapArray1 objectAtIndex: 1]; self. curtitleMa P. scale = _ scale; self. curtitleMap. position = ccp (LEFTMIN, DOWNMIN); [self addChild: self. curtitleMap]; after modification, we will start to add Collision Detection [html] // check for obstacles-(void) canMoveTo :( CGPoint) pos {towerLoc = [self tileCoordForPosition: pos]; int road_tileGid = [self. curtitleMap. road tileGIDAt: towerLoc]; int enemy_tileGid = [self. curtitleMap. enemy tileGIDAt: towerLoc]; int item_tileGid = [self. curtitleMap. item tileGIDAt: tow ErLoc]; int door_tileGid = [self. curtitleMap. door tileGIDAt: towerLoc]; int npc_tileGid = [self. curtitleMap. npc tileGIDAt: towerLoc]; int downfloor_tileGid = [self. curtitleMap. downfloor tileGIDAt: towerLoc]; int upfloor_tileGid = [self. curtitleMap. upfloor tileGIDAt: towerLoc]; int other_tileGid = [self. curtitleMap. other tileGIDAt: towerLoc]; if (enemy_tileGid) {canmove = NO;} if (item_tileGid) {[sel F. curtitleMap. item removeTileAt: towerLoc];} if (door_tileGid) {// canmove = NO; [self. curtitleMap. door removeTileAt: towerLoc];} if (other_tileGid) {canmove = NO;} if (upfloor_tileGid) {canmove = NO;} if (downfloor_tileGid) {canmove = NO ;} if (npc_tileGid) {canmove = NO;} if (road_tileGid) {if (canmove) {[self setPlayerPosition: pos] ;}} towerLoc = [self tileCoordForPosition: pos] is to set the current Move the coordinates to map coordinate int road_tileGid = [self. curtitleMap. road tileGIDAt: towerLoc] to check whether the current coordinate has a graph block in the corresponding layer. When the method is ready, we have to call it and replace the content in the if loop in the updateMove method with [html] // determine whether there is an item or a monster [self canMoveTo: playerPoint]; run the command and you will find that the warriors cannot pass through the wall and the monsters, but they cannot fight against the monsters. Next we will add the corresponding response events in the if loop.

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.