大佬教程收集整理的这篇文章主要介绍了Cocos2d-x学习笔记(十六)-------->Cocos2d-x引擎中的网格地图,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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));
layer3->setTileGID(32,ccp(8,7));
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,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,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引擎中的网格地图全部内容,希望文章能够帮你解决Cocos2d-x学习笔记(十六)-------->Cocos2d-x引擎中的网格地图所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。