【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 value
	if( (self=[super init]) ) {
		
		self.tileMap=[CCTMXTiledMap tiledMapWithTMXFile:@"desert.tmx"];//desert.tmx是导入资源名
        self.background=[tileMap layerNamed:@"Ground"];//Ground是图层名
        [self addChild:tileMap z:-1];
	}
	return self;
}

最后生成程序。如图



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值