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

事件处理:

	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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值