1. 程式人生 > >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);
}