源代码下载地址:http://download.csdn.net/detail/danming60520/5296224
本文是参考了无脑码农的视频以及其中(临江仙)的一篇博客http://blog.csdn.net/zhy_cheng/article/details/8308609 写的一个学习笔记,大致内容可以参考他们的博客;Tiled Map Editor是Cocos2d-x支持的地图编辑器,使用Tiled编辑出的地图可以很方便的被Cocos2d-x使用Tiled的官网是http://www.mapeditor.org/。我使用的地图编辑器是QT版本。其中:cocos2d-x的版本是:cocos2d-2.1rc0-x-2.1.2-hotfix + VS2010。
这个实例游戏包含:
1:如何使用Tiled Map Editor地图编辑器
2:人物主角 可以吃西瓜,开箱子
3:添加分数计时器
现在试一试地图编辑器吧:
首先新建一个新地图:文件->新建文件
然后将所要用到的图片文件保存到vs 工程目录Resource 下面,然后选择: 地图 --> 新图块,导入需要的图片,
在这里图片中间和最左边最上边都有黑线,所以绘制偏移1个像素,边距和间距都为一。
然后就是按照“无脑码农”视频里面的讲解一步一步的操作直到完成下面的操作:
这里包含了五个图层:floor, wall, hero, foreground, box; (包含三个图片,一个是基本的地图,一个是西瓜,一个是箱子)
下面贴出主要代码:
有些人看到无脑视频里的人物移动有位置偏差,这里也做了更正,原因是精灵设置的坐标(锚点)和 地图编辑器里的坐标是有差的
bool HelloWorld::init()
{
bool bRet = false;
do
{
//
// super init first
//
CC_BREAK_IF(! CCLayer::init());
pTiledMap = CCTMXTiledMap::create("map.tmx"); //载入地图文件//
this->addChild(pTiledMap);
CCTMXObjectGroup* pObjects = pTiledMap->objectGroupNamed("hero"); //获取 hero 对象层//
CCDictionary* pSpawnPoint = pObjects->objectNamed("MyHero"); //获取对象MyHero//
int nX = pSpawnPoint->valueForKey("x")->floatValue(); //左下角为原点的坐标//
int nY = pSpawnPoint->valueForKey("y")->floatValue();
pSpriteHero = CCSprite::create("Player2.png");
CC_BREAK_IF(!pSpriteHero);
pSpriteHero->setPosition(ccp(nX + pSpriteHero->getContentSize().width/2, nY+ pSpriteHero->getContentSize().height/2)); //
//pSpriteHero->setPosition(ccp(nX, nY));
this->addChild(pSpriteHero);
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false); ///添加触摸响应事件//
// this->setViewPointCenter(pSpriteHero->getPosition());
wallLayer = pTiledMap->layerNamed("wall");
foreGround = pTiledMap->layerNamed("foreground");
box = pTiledMap->layerNamed("box");
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLable = CCLabelTTF::create("","楷体", 28);
pLable->setColor(ccc3(192,14,235));
pLable->setPosition(ccp(9*32, size.height - 16));
addChild(pLable,1);
pLableBox = CCLabelTTF::create("","楷体", 28);
pLableBox->setColor(ccc3(0,0,0));
pLableBox->setPosition(ccp(9*32, size.height - 48));
addChild(pLableBox,1);
pGameOver = CCLabelTTF::create("","楷体", 50);
pGameOver->setColor(ccc3(255,0,0));
pGameOver->setPosition(ccp(size.width / 2, size.height / 2));
addChild(pGameOver,1);
nCount = nCountBox = 0;
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCPoint point = pTouch->getLocation();
CCPoint newPoint;
//如果 点击点的X坐标减去player的X坐标的绝对值 大于 点加点的Y坐标减去player的Y坐标的绝对值//
if (fabs(point.x - pSpriteHero->getPosition().x) > fabs(point.y - pSpriteHero->getPosition().y))
{
if (point.x > pSpriteHero->getPosition().x)
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX() + 32, pSpriteHero->getPositionY()));
newPoint = ccp(pSpriteHero->getPositionX() + 32, pSpriteHero->getPositionY());
}
else
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX() - 32, pSpriteHero->getPositionY()));
newPoint = ccp(pSpriteHero->getPositionX() - 32, pSpriteHero->getPositionY());
}
}
else
{
if (point.y > pSpriteHero->getPosition().y)
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX() , pSpriteHero->getPositionY() + 32));
newPoint = ccp(pSpriteHero->getPositionX() , pSpriteHero->getPositionY() + 32);
}
else
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX(), pSpriteHero->getPositionY() - 32));
newPoint = ccp(pSpriteHero->getPositionX(), pSpriteHero->getPositionY() - 32);
}
}
// CCPoint pointNew = ccp(newPoint.x - 16, newPoint.y - 16);
if (pSpriteHero->getPosition().x <= pTiledMap->getMapSize().width * pTiledMap->getTileSize().width &&
pSpriteHero->getPositionY() <= pTiledMap->getMapSize().height * pTiledMap->getTileSize().height &&
pSpriteHero->getPositionX() >= 0 &&
pSpriteHero->getPositionY() >= 0)
{
this->setPlayerPosition(newPoint);
}
// this->setPlayerPosition(newPoint);
//this->setViewPointCenter(newPoint);
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCPoint point = pTouch->getLocation();
CCPoint newPoint;
//如果 点击点的X坐标减去player的X坐标的绝对值 大于 点加点的Y坐标减去player的Y坐标的绝对值//
if (fabs(point.x - pSpriteHero->getPosition().x) > fabs(point.y - pSpriteHero->getPosition().y))
{
if (point.x > pSpriteHero->getPosition().x)
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX() + 32, pSpriteHero->getPositionY()));
newPoint = ccp(pSpriteHero->getPositionX() + 32, pSpriteHero->getPositionY());
}
else
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX() - 32, pSpriteHero->getPositionY()));
newPoint = ccp(pSpriteHero->getPositionX() - 32, pSpriteHero->getPositionY());
}
}
else
{
if (point.y > pSpriteHero->getPosition().y)
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX() , pSpriteHero->getPositionY() + 32));
newPoint = ccp(pSpriteHero->getPositionX() , pSpriteHero->getPositionY() + 32);
}
else
{
//pSpriteHero->setPosition(ccp(pSpriteHero->getPositionX(), pSpriteHero->getPositionY() - 32));
newPoint = ccp(pSpriteHero->getPositionX(), pSpriteHero->getPositionY() - 32);
}
}
if (pSpriteHero->getPosition().x <= pTiledMap->getMapSize().width * pTiledMap->getTileSize().width &&
pSpriteHero->getPositionY() <= pTiledMap->getMapSize().height * pTiledMap->getTileSize().height &&
pSpriteHero->getPositionX() >= 0 &&
pSpriteHero->getPositionY() >= 0)
{
this->setPlayerPosition(newPoint);
}
}
void HelloWorld::setPlayerPosition(cocos2d::CCPoint Position)
{
CCPoint tiledCoord = this->tileCoordForPosition(Position); //获取瓦片tiled 坐标//
int tiledId = wallLayer->tileGIDAt(tiledCoord); //获取瓦片tiled 在层中的ID标识//
if (tiledId != 0)
{
CCDictionary* properties = pTiledMap->propertiesForGID(tiledId);
if (properties != NULL)
{
CCString sCollision = "";
if (properties->valueForKey("Collisional")->length() > 0 )
sCollision = properties->valueForKey("Collisional")->getCString();
if (sCollision.length() > 0 && sCollision.m_sString == "True") return;
}
}
int tiledWatermelon = foreGround->tileGIDAt(tiledCoord);
if (tiledWatermelon != 0)
{
CCDictionary* watermelonProperty = pTiledMap->propertiesForGID(tiledWatermelon);
if (watermelonProperty != NULL)
{
CCString sWater = "";
if (watermelonProperty->valueForKey("Watermelon")->length() > 0)
{
sWater = watermelonProperty->valueForKey("Watermelon")->getCString();
if (sWater.length() > 0 && sWater.m_sString == "True")
{
foreGround->removeTileAt(tiledCoord);
nCount += 2;
CCString *temp=CCString::createWithFormat("W:%d",nCount);
pLable->setString(temp->getCString());
}
}
}
}
int tiledBox = box->tileGIDAt(tiledCoord);
if (tiledBox != 0)
{
CCDictionary* boxProperty = pTiledMap->propertiesForGID(tiledBox);
if (boxProperty != NULL)
{
CCString sBox = "";
if (boxProperty->valueForKey("box")->length() > 0)
{
sBox = boxProperty->valueForKey("box")->getCString();
if (sBox.length() > 0 && sBox.m_sString == "4" )
{
box->removeTileAt(tiledCoord);
nCountBox += 4;
CCString *temp=CCString::createWithFormat("B:%d",nCountBox);
pLableBox->setString(temp->getCString());
}
else if (sBox.length() > 0 && sBox.m_sString == "3")
{
box->removeTileAt(tiledCoord);
nCountBox += 3;
CCString *temp=CCString::createWithFormat("B:%d",nCountBox);
pLableBox->setString(temp->getCString());
}
else if (sBox.length() > 0 && sBox.m_sString == "2")
{
box->removeTileAt(tiledCoord);
nCountBox += 2;
CCString *temp=CCString::createWithFormat("B:%d",nCountBox);
pLableBox->setString(temp->getCString());
}
else if (sBox.length() > 0 && sBox.m_sString == "1")
{
box->removeTileAt(tiledCoord);
nCountBox ++;
CCString *temp=CCString::createWithFormat("B:%d",nCountBox);
pLableBox->setString(temp->getCString());
}
}
}
}
pSpriteHero->setPosition(Position);
if (nCount == 100 && nCountBox == 100)
{
pGameOver->setString("You Win The Game !!!");
}
}
好下面是程序运行起来的截图:
程序简单说明一下,人物吃一个西瓜就可以赢得2分,吃宝箱分别是赢得的1,2,3,4分 当分数达到100的时候,就赢了。
源代码下载地址:http://download.csdn.net/detail/danming60520/5296224