首页 > 代码库 > Cocos2d-x Tiled地图编辑器(二)精灵走动起来、碰撞检测

Cocos2d-x Tiled地图编辑器(二)精灵走动起来、碰撞检测

图:



程序代码:

头文件声明函数及变量

	void registerWithTouchDispatcher(void);
	bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
	void setViewpointCenter(CCPoint position)  ;
	void setSpritePoint(CCPoint position) ;
	CCPoint tileCoordForPosition(CCPoint position);

	 CCSprite *sprite1;
	 CCTMXTiledMap *map;


在init()方法创建tiled地图,获取对象坐标

	map = CCTMXTiledMap::create("map1.tmx");
	this->addChild(map);
	CCTMXObjectGroup *object = map->objectGroupNamed("object1");
	CCDictionary *sprite_object = object->objectNamed("sprite1");

并加载精灵

 

	float x = ((CCString*)sprite_object->objectForKey("x"))->floatValue();  
	float y = ((CCString*)sprite_object->objectForKey("y"))->floatValue(); 
	CCLOG("sprite postion x: %f, %f", x, y);
	sprite1 = CCSprite::create("yezhu.png");
	sprite1->setPosition(ccp(x, y));
	this->addChild(sprite1);
	
	setTouchEnabled(true);	//开启触摸事件

然后重载触摸事件

 

void HelloWorld::registerWithTouchDispatcher()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0, true);
}

bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	   return true;
}

处理触摸坐标与精灵坐标位置变量, 让精灵跟随触摸点移动

void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint touchlocation = convertTouchToNodeSpace(pTouch);
	CCPoint touchsprite =  sprite1->getPosition();
	CCPoint mpoint;

	CCLOG("touchlocationX:%f, Y:%f  touchspriteX:%f, Y:%f", touchlocation.x, touchlocation.y, touchsprite.x , touchsprite.y);
	//点击坐标x轴减去精灵x轴 绝对值大于 点击坐标在精灵x轴减去精灵x轴 
	if (fabs(touchlocation.x-touchsprite.x) > fabs(touchlocation.y-touchsprite.y))
	{
		//点击的坐标x轴大于精灵x轴 在精灵的右侧
		if (touchlocation.x > touchsprite.x) 
		{
			mpoint = ccp(touchsprite.x+32, touchsprite.y); //精灵x轴递增移动
		}
		else//点击的坐标x轴小于精灵x轴 在精灵的左侧
		{
			mpoint = ccp(touchsprite.x-32, touchsprite.y);	//精灵x轴递减移动
		}
		
	}
	//点击坐标x轴减去精灵x轴 绝对值小于 点击坐标在精灵x轴减去精灵x轴
	else
	{
		//点击的坐标y轴大于精灵y轴 在精灵的上侧
		if (touchlocation.y > touchsprite.y)
		{
			mpoint = ccp(touchsprite.x, touchsprite.y+32);	//精灵y轴递增移动
		}
		else //点击的坐标y轴小于精灵y轴 在精灵的下侧
		{
			mpoint = ccp(touchsprite.x, touchsprite.y-32);	//精灵y轴递减移动
		}
		
	}
	setSpritePoint(mpoint);//设置新坐标移动精灵,并检测碰撞
	CCPoint sp_point = sprite1->getPosition();
	setViewpointCenter(sp_point); //设置视图中心点


}

视角跟随精灵移动

void HelloWorld::setViewpointCenter(CCPoint position)  
{  
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
	float x = MAX(position.x, winSize.width / 2);  
	float y = MAX(position.y, winSize.height / 2);  
	x = MIN(x, (map->getMapSize().width*map->getTileSize().width) - winSize.width / 2);  
	y = MIN(y, (map->getMapSize().height*map->getTileSize().height)- winSize.height / 2);

	CCPoint acturalPosition = ccp(x, y);  

	CCPoint centerView = ccp(winSize.width / 2, winSize.height / 2);  
	CCPoint viewPoint = ccpSub(centerView, acturalPosition);
	this->setPosition(viewPoint);
	

}


检测与石头图素的碰撞,若石头图素属性为true不移动精灵,否则移动精灵

void HelloWorld::setSpritePoint(CCPoint position)
{
	 CCPoint tilecoord = this->tileCoordForPosition(position);//将坐标转换成tile坐标
	 CCTMXLayer *layer = map->layerNamed("layer1") ; //获取地图层
	 int tilegid = layer->tileGIDAt(tilecoord);//获取图素GID值
	 CCLOG("tilegid: %d", tilegid);
	 if (tilegid)
	 {
		 CCDictionary *properties  = map->propertiesForGID(tilegid)	;//根据GID值获取图素属性键值集合
		 if (properties)
		 {
			 const CCString *str = properties->valueForKey("ShiTou");//键值名称
			 if (str && str->compare("true") == 0) 	 //键值
				 return;
		 }
	 }
	 sprite1->setPosition(position);
}

将坐标转换成tile坐标

CCPoint HelloWorld::tileCoordForPosition(CCPoint position)
{
	int x = position.x / map->getTileSize().width;
	int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height;
	return ccp(x, y);
}