【Cocosd2d实例教程三】Cocos2d瓦片地图的导入
生活随笔
收集整理的這篇文章主要介紹了
【Cocosd2d实例教程三】Cocos2d瓦片地图的导入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(轉載請注明出處:http://blog.csdn.net/buptgshengod)
? ?上一節講了如何使用Tiled制作瓦片地圖,下面講一下如何將地圖導入游戲中。 ? ? 第一步:將生成的文件導入resource中,如圖,分別為地圖和圖片集 ? ? 第二步:在HelloWorldLayer.h中修改代碼,有一定基礎的人還是比較好理解的。 #import <GameKit/GameKit.h>// When you import this file, you import all the cocos2d classes #import "cocos2d.h"// HelloWorldLayer @interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate> {CCTMXTiledMap *tileMap;//地圖文件CCTMXLayer *background;//地圖文件的一個層 }@property(nonatomic,retain)CCTMXTiledMap *tileMap;//聲明tileMap @property(nonatomic,retain)CCTMXLayer *background;//聲明background // returns a CCScene that contains the HelloWorldLayer as the only child +(CCScene *) scene;@end (注意:background是地圖文件的圖層,有幾個圖層就要聲明幾個CCTMXLayer) ? 第三步:修改HelloWorldLayer.m文件 ? 1)在implentation后加入@synthesize tileMap; @synthesize background; ? 2)在dealloc加入(應該是垃圾處理,因為object-c沒有這種機制,要手動處理) - (void) dealloc {self.tileMap=nil;self.background=nil;// in case you have something to dealloc, do it in this method// in this particular example nothing needs to be released.// cocos2d will automatically release all the children (Label)// don't forget to call "super dealloc"[super dealloc]; } ?3)替換init -(id) init {// always call "super" init// Apple recommends to re-assign "self" with the "super's" return valueif( (self=[super init]) ) {self.tileMap=[CCTMXTiledMap tiledMapWithTMXFile:@"desert.tmx"];//desert.tmx是導入資源名self.background=[tileMap layerNamed:@"Ground"];//Ground是圖層名[self addChild:tileMap z:-1];}return self; } 最后生成程序。如圖
總結
以上是生活随笔為你收集整理的【Cocosd2d实例教程三】Cocos2d瓦片地图的导入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Cocosd2d实例教程二】地图编辑器
- 下一篇: 【Cocosd2d实例教程四】Cocos