cocos2d-x学习日志(1)--视角跟随主角
2014-01-14 10:53
387 查看
本篇讲解一个主视角跟随主角的效果,先上效果图。
很简单,就是整个视角(屏幕)随主角的移动而移动,这在ARPG,ACT等游戏中非常常用,首先看一下我们程序的结构。
在HelloWorldScene.h中添加如下:
场景方法很简单,就是创建场景,并加入我们例子的核心部分----层
在HelloWorldScene.cpp中添加如下:
/*场景的初始化就是首先通过tmx文件定义地图,然后定义主角,并把主角放在地图上,然后为主角定义动作,来回运动,然后加入scheduleUpate,使得每帧调用update。在update中我们将修改我们的视角。*/
在update中,我们获得主角的位置,并把它和主角初始坐标作比较,得到deltax和deltay,然后,视角随主角移动其实就是视角和主角相对静止,地图向相反方向移动即可,所以我们把地图的初始坐标减去刚才获得的坐标差就得到了地图的正确位置。
出现问题:TMX瓦片地图无法加载问题。Get date from file(iso-test.png) failed!
解决:将官方示例自带的tmx和对应瓦片的png扔进去测一下。
很简单,就是整个视角(屏幕)随主角的移动而移动,这在ARPG,ACT等游戏中非常常用,首先看一下我们程序的结构。
在HelloWorldScene.h中添加如下:
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "SimpleAudioEngine.h" using namespace cocos2d; class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); //ADD Begin CCSprite* m_tamara; void update(float dt); //ADD End // implement the "static node()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
场景方法很简单,就是创建场景,并加入我们例子的核心部分----层
在HelloWorldScene.cpp中添加如下:
//ADD Begin using namespace CocosDenshion; enum{ //!Default tag KtagTileMap, }; //ADD End
// on "init" you need to initialize your instance bool HelloWorld::init() { if (!CCLayer::init()) { return false; } CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20)); //create menu,it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem,NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu,1); CCParallaxNode* voidNode = CCParallaxNode::create(); //添加资源路径 CCTMXTiledMap *map = CCTMXTiledMap::create("iso-test-zorder.tmx"); this->addChild(map,0,KtagTileMap); //cocos2d-x中使用getContentSize获得的就是逻辑点的大小 CCSize s = map->getContentSize(); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); map->setPosition(ccp(-s.width/2+winSize.width/2,0)); m_tamara = CCSprite::create("grossinis_sister1.png"); map->addChild(m_tamara,map->getChildren()->count()); m_tamara->retain(); int mapWidth = map->getMapSize().width * map ->getTileSize().width; m_tamara->setPosition(ccp(mapWidth/2,0)); m_tamara->setAnchorPoint(ccp(0.5f,0)); CCActionInterval* move = CCMoveBy::create(10,ccpMult(ccp(300,250),1/CC_CONTENT_SCALE_FACTOR())); CCActionInterval* back = move->reverse(); CCFiniteTimeAction* seq = CCSequence::create(move,back,NULL); m_tamara->runAction(CCRepeatForever::create((CCActionInterval*)seq)); scheduleUpdate(); return true; }
/*场景的初始化就是首先通过tmx文件定义地图,然后定义主角,并把主角放在地图上,然后为主角定义动作,来回运动,然后加入scheduleUpate,使得每帧调用update。在update中我们将修改我们的视角。*/
void HelloWorld::update(float dt) { char mch[256]; CCPoint herop = m_tamara->getPosition(); sprintf(mch,"x->%.2f---y->%.2f",herop.x,herop.y); CCLog(mch); CCTMXTiledMap* map = (CCTMXTiledMap*)getChildByTag(KtagTileMap); int mapWidth = map->getMapSize().width * map->getTileSize().width; float deltax = herop.x - mapWidth/2; float deltay = herop.y; CCSize s = map->getContentSize(); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); map->setPosition(ccp(-s.width/2+winSize.width/2-deltax,-deltay)); }
在update中,我们获得主角的位置,并把它和主角初始坐标作比较,得到deltax和deltay,然后,视角随主角移动其实就是视角和主角相对静止,地图向相反方向移动即可,所以我们把地图的初始坐标减去刚才获得的坐标差就得到了地图的正确位置。
出现问题:TMX瓦片地图无法加载问题。Get date from file(iso-test.png) failed!
解决:将官方示例自带的tmx和对应瓦片的png扔进去测一下。
相关文章推荐
- cocos2d-x游戏实例(1)-视角跟随主角
- cocos2d-x游戏实例(1)-视角跟随主角
- cocos2d-x学习日志(1) -Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境
- cocos2d-x学习日志(13) --A星寻路算法demo
- Cocos2d-x 3.1.1 学习日志4--cocos2d-x解决中文乱码问题的几种办法
- Unity学习笔记--鹰击长空类固定视角游戏摄像机跟随
- cocos2d-x 学习日志(3)之QQ平台接入
- cocos2d-x学习日志(14) --A星寻路算法之45度地图
- Cocos2d-x 3.1.1 学习日志7--7分钟让你了解cocos2d-x3.1.1 Sprite精灵类
- cocos2d-x学习日志(4) -- CCCallFunc系列函数的应用
- cocos2d-x学习日志(7) --CCScrollView和CCTableView的使用
- cocos2d-x 学习日志(2)之使用UIWebView加载网页
- Cocos2d-x 3.1.1 学习日志12--一Cocos2dx3.1.1移植到Android平台的方法(最有用最有效的!!)
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第五步--button控制主角Jump&Crouch
- cocos2d-x-LuaProxy学习日志(1) -- Cocos2d-x HelloLua 介绍
- Cocos2d-x 3.1.1 学习日志14--CocosStudio学习必看
- Cocos2d-x 3.1.1 学习日志14--CocosStudio学习必看
- 【转】Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性
- cocos2d-x 学习日志(5)之cocos2d-x播放视频
- cocos2d-html 学习日志(一) 环境搭建