您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x学习日志(1)--视角跟随主角

2014-01-14 10:53 387 查看
本篇讲解一个主视角跟随主角的效果,先上效果图。



很简单,就是整个视角(屏幕)随主角的移动而移动,这在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扔进去测一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: