CCFastTMXTiledMap解析

本文介绍了一个名为CC_DLLTMXTiledMap的类,它用于创建和操作TMX瓦片地图。该类提供了创建地图、获取特定层和组、获取属性等关键功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:

class CC_DLL TMXTiledMap : public Node
{
public:
    /** Creates a TMX Tiled Map with a TMX file.
     *
     * @return An autorelease object.
     */
	 /// 创建一个TMX 瓦片地图,通过tmx文件
    static TMXTiledMap* create(const std::string& tmxFile);

    /** Initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources. 
     *
     * @param tmxString A TMX formatted XML string.
     * @param resourcePath A path to TMX resources.
     * @return An autorelease object.
     */
	 /// 通过xml创建一个瓦片地图
    static TMXTiledMap* createWithXML(const std::string& tmxString, const std::string& resourcePath);

    /** Return the FastTMXLayer for the specific layer. 
     * 
     * @return Return the FastTMXLayer for the specific layer.
     */
	 /// 得到TXMLLayer通过名字
    TMXLayer* getLayer(const std::string& layerName) const;

    /** Return the TMXObjectGroup for the specific group. 
     * 
     * @return Return the TMXObjectGroup for the specific group.
     */
	 /// 得到TMXObjectGroup,通过制定的组
    TMXObjectGroup* getObjectGroup(const std::string& groupName) const;

    /** Return the value for the specific property name.
     *
     * @return Return the value for the specific property name.
     */
	 /// 得到特性的值
    Value getProperty(const std::string& propertyName) const;

    /** Return properties dictionary for tile GID.
     *
     * @return Return properties dictionary for tile GID.
     */
	 /// 通过GID得到特性的值
    Value getPropertiesForGID(int GID) const;

    /** The map's size property measured in tiles. 
     *
     * @return The map's size property measured in tiles.
     */
	 /// 得到地图的尺寸
    inline const Size& getMapSize() const { return _mapSize; };
    
    /** Set the map's size property measured in tiles.
     *
     * @param mapSize The map's size property measured in tiles.
     */
	 /// 设置地图的尺寸
    inline void setMapSize(const Size& mapSize) { _mapSize = mapSize; };

    /** The tiles's size property measured in pixels.
     *
     * @return The tiles's size property measured in pixels.
     */
	 /// 得到瓦片的尺寸
    inline const Size& getTileSize() const { return _tileSize; };
    
    /** Set the tiles's size property measured in pixels. 
     *
     * @param tileSize The tiles's size property measured in pixels.
     */
	 /// 设置瓦片的尺寸
    inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; };

    /** Get map orientation. 
     *
     * @return The map orientation.
     */
	 /// 得到地图的方向
    inline int getMapOrientation() const { return _mapOrientation; };
    
    /** Set map orientation. 
     *
     * @param mapOrientation The map orientation.
     */
	 /// 设置地图的方向
    inline void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; };

    /** Get object groups. 
     *
     * @return Object groups.
     */
	 /// 得到对象组
    inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; };
    inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
    
    /** Set object groups. 
     *
     * @param groups An object groups.
     */
	 /// 设置对象组
    inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
        _objectGroups = groups;
    };
    
    /** Get properties.
     *
     * @return Properties.
     */
	 /// 得到所有的属性
    inline const ValueMap& getProperties() const { return _properties; };
    
    /** Set properties. 
     *
     * @param properties An ValueMap Properties.
     */
	 /// 设置属性
    inline void setProperties(const ValueMap& properties) {
        _properties = properties;
    };
/// 得到描述
    virtual std::string getDescription() const override;

protected:
    /**
     * @js ctor
     */
    TMXTiledMap();
    /**
     * @js NA
     * @lua NA
     */
    virtual ~TMXTiledMap();

    /** initializes a TMX Tiled Map with a TMX file */
	/// 初始化
    bool initWithTMXFile(const std::string& tmxFile);

    /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
	/// 初始化
    bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
    
	/// 解析tmx
    TMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
    TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
    void buildWithMapInfo(TMXMapInfo* mapInfo);

    /** the map's size property measured in tiles */
    Size _mapSize;
    /** the tiles's size property measured in pixels */
    Size _tileSize;
    /** map orientation */
    int _mapOrientation;
    /** object groups */
    Vector<TMXObjectGroup*> _objectGroups;
    /** properties */
    ValueMap _properties;
    
    //! tile properties
    ValueMapIntKey _tileProperties;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(TMXTiledMap);

};



tIDE是多国语言的免费软件,但是!竟然没有中文,关于它的介绍自己看就不说了,下面来创建第一张地图,用于我们的TD游戏。 你现在需要一张这样的tile图片,来做完地图的基本元件 在Explorer面板Map节点地下的Tile Sheets右键Add,然后浏览选择上面的或者你自己的tile文件。 OK弹出Tile Sheet Properties对话框设置一下Tile Size让Tile完美的切割。这里的值是60x60。 ok,然后会在Tile picher面板里就会出现一个个分割好的Tile。 有了这些tile的支持就可以在map上玩拼图了。在map Explorer面板下的Map节点的Layers上右键Add,在弹出的Layer Properties设置地图的大小Layer Size,因为我现在只打算做一个最简单的也就是一屏幕而且不能拖动所以这里设置14x8 (800/60 x 480/60),OK。 为了方便绘制在工具面板的右上角有一个显示网格的按钮,点下去它 右边的绘图工具栏上有一个很好使用的按钮区域绘制,它能把选择区域填充出当前选择的tile,下面就先在tile Picker选择一块浅色的草地区域绘制到整个地图。然后画上我们的路。 Save as为 tmx类型的文件 中间如果出现警告确定就好了 这里出现了最关键的一步,这里一定要选Base64(gzip compressed),ccx现在的版本还只支持这一种。 把生成的tmx文件以及tile图片加入我们的项目中。 这里可能需要调整一下tmx文件中的Image source的路径 在程序中显示TMX地图 先显示出来看看努力的效果,之后再慢慢完善。 把GameScreen构造函数中的说明Label删除,添加CCTMXTiledMap 代码很简单如下 private GameScreen() { CCTMXTiledMap tmxmap = CCTMXTiledMap.tiledMapWithTMXFile("Map/Level1"); addChild(tmxmap); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值