cocoa2d-x tiled map添加tile翻转功能
今天用cocos2d-1.0.1-x-0.9.2来测试tiled map的功能,结果发现翻转过的tile都不见了,调试代码发现原来代码里不支持,没有对x,y翻转作处理,结果翻转过的tile导致数值多大没有被现实出来。
( 注:最新的Tiled map已经支持flip x和flip y,选中tile按键盘上x键,y键即可,其他旋转功能他们正在添加中。。。最新版本可以从http://www.mapeditor.org/下载)
修改代码实现此功能, CCTMXLayer.cpp文件里针对appendTileForGID方法作出修改,上代码:
// used only when parsing the map. useless after the map was parsed // since lot's of assumptions are no longer true CCSprite * CCTMXLayer::appendTileForGID(unsigned int gid, const CCPoint& pos) { //lancer add for tile flip //setFlipX int flip = gid>>28; int tileid = gid &0x0FFFFFFF; gid = tileid; //end CCRect rect = m_pTileSet->rectForGID(gid); rect = CCRectMake(rect.origin.x / m_fContentScaleFactor, rect.origin.y / m_fContentScaleFactor, rect.size.width/ m_fContentScaleFactor, rect.size.height/ m_fContentScaleFactor); int z = (int)(pos.x + pos.y * m_tLayerSize.width); if( ! m_pReusedTile ) { m_pReusedTile = new CCSprite(); m_pReusedTile->initWithBatchNode(this, rect); } else { m_pReusedTile->initWithBatchNode(this, rect); } m_pReusedTile->setPosition(positionAt(pos)); m_pReusedTile->setVertexZ((float)vertexZForPos(pos)); m_pReusedTile->setAnchorPoint(CCPointZero); m_pReusedTile->setOpacity(m_cOpacity); //lancer add for tile flip //setFlipX cocos2d::CCLog("---gid=%d, flip sign=%d, tileid=%d\n", gid, flip, tileid); #define TILE_FLIP_X 8 #define TILE_FLIP_Y 4 if(flip & TILE_FLIP_X){ cocos2d::CCLog("set flip x\n"); m_pReusedTile->setFlipX(true); } if(flip & TILE_FLIP_Y){ cocos2d::CCLog("set flip y\n"); m_pReusedTile->setFlipY(true); } //end // optimization: // The difference between appendTileForGID and insertTileforGID is that append is faster, since // it appends the tile at the end of the texture atlas unsigned int indexForZ = m_pAtlasIndexArray->num; // don't add it using the "standard" way. addQuadFromSprite(m_pReusedTile, indexForZ); // append should be after addQuadFromSprite since it modifies the quantity values ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ); return m_pReusedTile; }
PS:转载请保留以下信息
Author:smilelance