标签:
CCTMXTiledMap* map = CCTMXTiledMap::create("iso-test1.tmx");
addChild(map, 0);这部分代码将网格文件载入,在x引擎中网格地图对应CCTMXTiledMap类型,通过这一类型的对象可以对网格地图进行各种操作。接下来的代码将网格地图对象作为场景的一个子节点添加到游戏场景当中。CCTMXTiledMap* map = CCTMXTiledMap::create("iso-test1.tmx");
map->setAnchorPoint(ccp(0.5f,0.5f));
map->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
addChild(map, 0);效果:CCTMXTiledMap* map = CCTMXTiledMap::create("iso-test1.tmx");
map->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
map->setAnchorPoint(ccp(0.5f, 0.5f));
CCTMXLayer* layer1 = map->layerNamed("layer1");
CCTMXLayer* layer2 = map->layerNamed("layer2");
CCTMXLayer* layer3 = map->layerNamed("layer3");
layer2->setVisible(false);
addChild(map, 0);CCSprite* sprite; sprite = layer3->tileAt(ccp(8,7)); sprite->setColor(ccc3(200,0,0));
layer3->setTileGID(32, ccp(8,7));
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);然后在HelloWorldScene.cpp中加入:
void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent){
CCTouch* touch = (CCTouch *)pTouches->anyObject();
CCPoint diff = touch->getDelta();
CCNode* node = getChildByTag(1);
CCPoint currentPos = node->getPosition();
node->setPosition(ccpAdd(currentPos, diff));
}这个函数中首先得到了用户的触摸点,在x引擎中使用CCTouch类型代表触摸点信息,接下来使用触摸点对象touch的getDelta方法,得到当前触摸点在两帧间隔时间内在X轴和Y轴方向上的移动距离。得到每次手指滑动的距离后,根据这个位置变化,更新地图的位置坐标。使用getChildByTag(1)这个方法得到网格地图节点,之所以能够通过这个方法取得网格地图对象,是因为在向场景中添加网格地图时,使用addChild(map,0,1),第三个参数指定了这个网格地图的tag标签为1。再通过标签就能够找回网格地图对象,此时它的类型就是一个场景节点,可以通过getPosition()取得此节点的坐标位置,通过setPosition()可以设置它的新位置坐标。layer1->setZOrder(1); layer2->setZOrder(2); layer3->setZOrder(10);参数1、2、10就是每个层的显示级别,数值越大,级别越高,高级别的会遮挡住低级别的物体。
CCSprite* sprite = CCSprite::create("iso-test.png", CCRectMake(274,0,48,114));
sprite->setPosition(ccp(350, 300));
sprite->setAnchorPoint(ccp(0.5f,0.5f));
map->addChild(sprite, 3);
CCActionInterval* move = CCMoveBy::create(3, ccp(200, -200));
CCActionInterval* back = move->reverse();
CCActionInterval* seq = CCSequence::create(move, back, NULL);
sprite->runAction(CCRepeatForever::create(seq));效果:Cocos2d-x学习笔记(十六)-------->Cocos2d-x引擎中的网格地图
标签:
原文地址:http://blog.csdn.net/liang3472/article/details/42248607