首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

Cocos2d瓦片map的导入

2013-10-15 
Cocos2d瓦片地图的导入 第一步:将生成的文件导入resource中,如图,分别为地图和图片集第二步:在HelloWorldL

Cocos2d瓦片地图的导入
 第一步:将生成的文件导入resource中,如图,分别为地图和图片集Cocos2d瓦片map的导入    第二步:在HelloWorldLayer.h中修改代码,有一定基础的人还是比较好理解的。[objc] view plaincopy

  1. #import <GameKit/GameKit.h>  
  2.   
  3. // When you import this file, you import all the cocos2d classes  
  4. #import "cocos2d.h"  
  5.   
  6. // HelloWorldLayer  
  7. @interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>  
  8. {  
  9.     CCTMXTiledMap *tileMap;//地图文件  
  10.     CCTMXLayer *background;//地图文件的一个层  
  11. }  
  12.   
  13. @property(nonatomic,retain)CCTMXTiledMap *tileMap;//声明tileMap  
  14. @property(nonatomic,retain)CCTMXLayer *background;//声明background  
  15. // returns a CCScene that contains the HelloWorldLayer as the only child  
  16. +(CCScene *) scene;  
  17.   
  18. @end  
(注意:background是地图文件的图层Cocos2d瓦片map的导入,有几个图层就要声明几个CCTMXLayer)  第三步:修改HelloWorldLayer.m文件  1)在implentation后加入
[objc] view plaincopy
  1. @synthesize tileMap;  
  2. @synthesize background;  
  2)在dealloc加入(应该是垃圾处理,因为object-c没有这种机制,要手动处理)[objc] view plaincopy
  1. - (void) dealloc  
  2. {  
  3.     self.tileMap=nil;  
  4.     self.background=nil;  
  5.     // in case you have something to dealloc, do it in this method  
  6.     // in this particular example nothing needs to be released.  
  7.     // cocos2d will automatically release all the children (Label)  
  8.       
  9.     // don't forget to call "super dealloc"  
  10.     [super dealloc];  
  11. }  
 3)替换init[objc] view plaincopy
  1. -(id) init  
  2. {  
  3.     // always call "super" init  
  4.     // Apple recommends to re-assign "self" with the "super's" return value  
  5.     if( (self=[super init]) ) {  
  6.           
  7.         self.tileMap=[CCTMXTiledMap tiledMapWithTMXFile:@"desert.tmx"];//desert.tmx是导入资源名  
  8.         self.background=[tileMap layerNamed:@"Ground"];//Ground是图层名  
  9.         [self addChild:tileMap z:-1];  
  10.     }  
  11.     return self;  
  12. }  
最后生成程序。如图Cocos2d瓦片map的导入

热点排行