Tiled之跑酷游戏地图设计(金币 + 地图滚动 )

<1>数据成员。

public:
	void mapScroll(CCPoint touchPoint); //地图滚动
	void update(float dt); //更新
	void itTiled(); //初始化地图
	bool isExistTiled(CCPoint position);  //得到点击点所对应的砖块位置是否有砖块
	void testCrashHeroWithCoin(); //英雄和金币碰撞检测
public:
	CC_SYNTHESIZE(CCTMXTiledMap*, _map, Map); //地图
	CC_SYNTHESIZE(CCTMXLayer*, _crashLayer, CrashLayer); //碰撞检测层
	CC_SYNTHESIZE(CCPoint, _viewPoint, ViewPoint);  //地图视图坐标点
	CC_SYNTHESIZE(CCSprite*, _hero, Hero); //英雄主角
	CC_SYNTHESIZE(CCArray*, _coinArray, CoinArray); //金币数据存储

 
 <2>实现。 
 

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    CCSize size = CCDirector::sharedDirector()->getWinSize();

	_map = CCTMXTiledMap::create("MyTiaoTest.tmx");
	addChild(_map);
   
	_crashLayer = getMap()->layerNamed("crashLayer");

    _viewPoint = ccp(size.width/2, size.height/2);

	_hero = CCSprite::create("noodledie.png");
	_hero->setAnchorPoint(ccp(0.5f, 0));
	_hero->setPosition(ccp(20, 360));
	addChild(_hero);
	_hero->setScale(0.5f);

	_coinArray = CCArray::create();
	_coinArray->retain();
	itTiled();

	scheduleUpdate();

    return true;
}

void HelloWorld::mapScroll(CCPoint touchPoint) //地图的滚动。
{ 
	CCSize winSize=CCDirector::sharedDirector()->getWinSize();
	int x=MAX(touchPoint.x, winSize.width/2);
	int y=MAX(touchPoint.y, winSize.height/2);
	x=MIN(x,(this->getMap()->getMapSize().width*this->getMap()->getTileSize().width)-winSize.width/2);
	y=MIN(y,(this->getMap()->getMapSize().height*this->getMap()->getTileSize().height)-winSize.height/2);

	CCPoint actualPosition=ccp(x,y);
	CCPoint centerOfView=ccp(winSize.width/2,winSize.height/2);
	CCPoint viewPoint=ccpSub(centerOfView,actualPosition);  //v1 - v2 也就是第一个减去第二个
	this->setPosition(viewPoint);
}

void HelloWorld::update(float dt)
{
	_viewPoint = ccpAdd(_viewPoint, ccp(5, 0));
	getHero()->setPosition(ccpAdd(getHero()->getPosition(), ccp(5, 0)));
	mapScroll(_viewPoint);

	if(getHero()->getPositionY() > 0 && !isExistTiled(getHero()->getPosition()))
	{
		getHero()->setPosition(ccpSub(getHero()->getPosition(), ccp(0, 5)));
	}
	else if(getHero()->getPositionY() == 0)
	{
		getHero()->setPosition(ccpAdd(getHero()->getPosition(), ccp(0, 300)));
	}

	if(getHero()->getPositionX() >= getMap()->getContentSize().width - getHero()->getContentSize().width/2)
	{
		unscheduleUpdate();
	}

	testCrashHeroWithCoin();
}

void HelloWorld::itTiled() //
{
	CCTMXObjectGroup* group = getMap()->objectGroupNamed("coinLayer");  //对象层,比如金币什么的。
	CCArray* array = group->getObjects();
	CCObject* obj = NULL;
	CCARRAY_FOREACH(array, obj)
	{
		CCDictionary* dic = dynamic_cast<CCDictionary*>(obj);
		float x = dic->valueForKey("x")->floatValue();
		float y = dic->valueForKey("y")->floatValue();
		CCSprite* sp = CCSprite::create("coin.png");
		sp->setPosition(ccpAdd(ccp(x, y), ccp(getMap()->getTileSize().width/2, getMap()->getTileSize().height/2)));
		addChild(sp);
		getCoinArray()->addObject(sp);
	}
}

bool HelloWorld::isExistTiled(CCPoint position)  //一个普通的图层,里面可以用资源画上一些砖块
{   
	int x = position.x / getMap()->getTileSize().width;  
	int y = (getMap()->getContentSize().height - position.y) / getMap()->getTileSize().height;  
	int flag =  getCrashLayer()->tileGIDAt(CCPointMake(x, y));
	return flag != 0 ? true : false; 
}  

void HelloWorld::testCrashHeroWithCoin()
{
	/*第一种方法/
	CCObject* obj = NULL;
	CCARRAY_FOREACH(getCoinArray(), obj)
	{
		CCSprite* coin = dynamic_cast<CCSprite*>(obj);
		if(coin->boundingBox().intersectsRect(getHero()->boundingBox()))
			coin->removeFromParent();
	}
	//*/
	
	//*/
	CCArray* coinDeleteArray = CCArray::create();
	CCObject* obj = NULL;
	CCARRAY_FOREACH(getCoinArray(), obj)
	{
		CCSprite* coin = dynamic_cast<CCSprite*>(obj);
		if(coin->boundingBox().intersectsRect(getHero()->boundingBox()))
			coinDeleteArray->addObject(coin);
	}

	//*第二种方法/
	CCARRAY_FOREACH(coinDeleteArray, obj)
	{
		CCSprite* coin = dynamic_cast<CCSprite*>(obj);
		getCoinArray()->removeObject(coin);
		coin->removeFromParent();
	}
	//*/
	
}
总结:金币在对象层coinLayer,而碰撞检测是在一个普通的图层crashLayer。

你可能感兴趣的:(Tiled之跑酷游戏地图设计(金币 + 地图滚动 ))