coco2d地图显示相关类

摘自:知易教程

Cocos2d-iPhone 实现地图显示的有主要 2 组文件:

1) 负责整体地图的显示:CCTMXTiledMap.h, CCTMXTiledMap.m

2) 负责 xml 文件读取和解析:CCTMXXMLParser.h,CCTMXXMLParser.m

 

在实际游戏编程中,我们主要用到以下几个类:

1) CCTMXTiledMap

Layer 类的 init 凼数中通过以下的代码加载地图:(确保 PNG 文件不 tmx 在一起)

// Load level map 
gameWorld = [CCTMXTiledMap tiledMapWithTMXFile:@"Level1.tmx"];
 [self addChild:gameWorld z:0 tag:9];

 TMXTiledMap 是从 CocosNode 直接派生出来的。他的定义比我们预想的要简单:

@interface CCTMXTiledMap : CCNode
{
	CGSize				mapSize_;
	CGSize				tileSize_;
	int					mapOrientation_;
	NSMutableArray		*objectGroups_;
	NSMutableDictionary	*properties_;
	NSMutableDictionary	*tileProperties_;
}

 分析初始化凼数 init 如下:

-(id) initWithTMXFile:(NSString*)tmxFile
{
	NSAssert(tmxFile != nil, @"TMXTiledMap: tmx file should not bi nil");

	if ((self=[super init])) {
		
		[self setContentSize:CGSizeZero];

		CCTMXMapInfo *mapInfo = [CCTMXMapInfo formatWithTMXFile:tmxFile];
		
		NSAssert( [mapInfo.tilesets count] != 0, @"TMXTiledMap: Map not found. Please check the filename.");
		
		mapSize_ = mapInfo.mapSize;
		tileSize_ = mapInfo.tileSize;
		mapOrientation_ = mapInfo.orientation;
		objectGroups_ = [mapInfo.objectGroups retain];
		properties_ = [mapInfo.properties retain];
		tileProperties_ = [mapInfo.tileProperties retain];
				
		int idx=0;

		for( CCTMXLayerInfo *layerInfo in mapInfo.layers ) {
			
			if( layerInfo.visible ) {
				CCNode *child = [self parseLayer:layerInfo map:mapInfo];
				[self addChild:child z:idx tag:idx];
				
				// update content size with the max size
				CGSize childSize = [child contentSize];
				CGSize currentSize = [self contentSize];
				currentSize.width = MAX( currentSize.width, childSize.width );
				currentSize.height = MAX( currentSize.height, childSize.height );
				[self setContentSize:currentSize];
	
				idx++;
			}			
		}		
	}

	return self;
}
 

CCTMXTiledMap 直接由类 CCTMXLayer 来实现地图的每一个层。每一个 CCTMXLayer 的实例都是通过 Cocos2D-iPhone 标准的 AddChild 添加给 CCTMXTiledMap 的。

 

2) CCTMXlayer

CCTMXLayer 类定义如下:

@interface CCTMXLayer : CCSpriteBatchNode
{
	CCTMXTilesetInfo	*tileset_;
	NSString			*layerName_;
	CGSize				layerSize_;
	CGSize				mapTileSize_;
	uint32_t			*tiles_;			// GID are 32 bit
	NSUInteger			layerOrientation_;
	NSMutableArray		*properties_;
	
	unsigned char		opacity_; // TMX Layer supports opacity
	
	NSUInteger			minGID_;
	NSUInteger			maxGID_;
	
	// Only used when vertexZ is used
	NSInteger			vertexZvalue_;
	BOOL				useAutomaticVertexZ_;
	float				alphaFuncValue_;
	
	// used for optimization
	CCSprite		*reusedTile_;
	ccCArray		*atlasIndexArray_;
}
    显然,CCTMXLayer 对于“瓦片”图像块的管理是通过 CCSpriteSheet 来实现的。 因此,地图的每一个“瓦片”图像就是一个 CCSprite 对象。于是,每一个“瓦片”图 像都可以迚行任意的 CCSprite 操作(增加、删除、移劢、放缩、旋转、变色...)。所有 这些操作都是劢态迚行的。这就允许我们在游戏迚行过程中的对地图迚行劢态操作,通过劢态改变地图的状态来反映游戏精灵对环境产生的影响。 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值