ITeye博客

Cocos2d-x中地图的使用方法(视角的设置和对象层的使用)

HeLinHang · 2013-09-03 23:46 · Cocos2d-x游戏开发 · 编程语言 · Markdown

事件处理:

	virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
	virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
	virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
在init中setTouchEnabled(true);

 

 

获取地图中对象层中对象的坐标点:

m_tileMap =CCTMXTiledMap::create("12.tmx");
	sprite=CCSprite::create("picture\\banana.png");
	this->addChild(sprite);
	this->addChild(m_tileMap);

	CCTMXObjectGroup* objects = m_tileMap->objectGroupNamed("object");//层的名称
//层中的对象块(名称为floor_one)
	CCDictionary* spawnPoint = objects->objectNamed("floor_one");  
	int x = spawnPoint->valueForKey("x")->intValue();  
	int y = spawnPoint->valueForKey("y")->intValue();
	CCLog("x:%d",x);
	CCLog("y:%d",y);

	sprite->setPosition(ccp(x,y));
	sprite->setZOrder(20);

 让精灵始终在地图的中间:

void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	CCPoint tmp = touch->getLocationInView();  
	tmp = CCDirector::sharedDirector()->convertToGL(tmp);  
	CCPoint pos = convertToNodeSpace(tmp); 

	CCLog("x:%f",pos.x);
	CCLog("y:%f",pos.y);
	sprite->setPosition(ccp(sprite->getPosition().x-2,sprite->getPosition().y));

	setViewPosition(ccp(sprite->getPosition().x,sprite->getPosition().y));
}

 

视角函数:

setViewPosition(CCPoint pos)
{
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
	int x = max(pos.x,winSize.width/2);  
	int y =max(pos.y,winSize.height/2);  

	  x = min(x,(m_tileMap->getMapSize().width*m_tileMap->getTileSize().width-winSize.width/2));  
	  y = min(y,(m_tileMap->getMapSize().height*m_tileMap->getTileSize().height-winSize.height/2));  

//	x = min(x,(m_tileMap->getContentSize().width-winSize.width/2));  
//	y = min(y,(m_tileMap->getContentSize().height-winSize.height/2));  

	this->setPosition(ccp(winSize.width/2-x,winSize.height/2-y));  
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。