读书人

Cocos2d瓦片map的导入

发布时间: 2013-10-15 16:47:37 作者: rapoo

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

  1. #import <GameKit/GameKit.h>
  2. // When you import this file, you import all the cocos2d classes
  3. #import "cocos2d.h"
  4. // HelloWorldLayer
  5. @interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
  6. {
  7. CCTMXTiledMap *tileMap;//地图文件
  8. CCTMXLayer *background;//地图文件的一个层
  9. }
  10. @property(nonatomic,retain)CCTMXTiledMap *tileMap;//声明tileMap
  11. @property(nonatomic,retain)CCTMXLayer *background;//声明background
  12. // returns a CCScene that contains the HelloWorldLayer as the only child
  13. +(CCScene *) scene;
  14. @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. // don't forget to call "super dealloc"
  9. [super dealloc];
  10. }
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. self.tileMap=[CCTMXTiledMap tiledMapWithTMXFile:@"desert.tmx"];//desert.tmx是导入资源名
  7. self.background=[tileMap layerNamed:@"Ground"];//Ground是图层名
  8. [self addChild:tileMap z:-1];
  9. }
  10. return self;
  11. }
最后生成程序。如图Cocos2d瓦片map的导入

读书人网 >操作系统

热点推荐