﻿ 使用TileMap制作游戏，在cocos2d - 鸿网互联

# 使用TileMap制作游戏，在cocos2d

##### 本篇博客出自阿修罗道http://blog.csdn.net/fansongy/article/details/8881664

[cpp] view plaincopyprint?
2. {
3.     CCSprite* enemy = CCSprite::create("enemy1.png");
4.     enemy->setPosition(ccp(x,y));
6.     //moveEnemy(enemy);
8. }

[cpp] view plaincopyprint?
1. void HelloWorld::moveEnemy(CCSprite* enemy)
2. {
3.     CCPoint diff = ccpSub(m_player->getPosition(),enemy->getPosition());
4.     float angleRadians = atanf((float)diff.y / (float)diff.x);
6.     float cocosAngle =-1* angleDegrees;
7.     if (diff.x <0) {
8.         cocosAngle +=180;
9.     }
10.     enemy->setRotation(cocosAngle);
11.
12.     int x = m_player->getPosition().x;
13.     int y = m_player->getPosition().y;
14.     CCActionInterval* move = CCMoveBy::create(3.0f, ccpSub(m_player->getPosition(),enemy->getPosition()));
15.     enemy->runAction(CCSequence::create(move,
16.         CCCallFuncN::create(this,callfuncN_selector(HelloWorld::enemyMoveFinished)),NULL));
17. }

[cpp] view plaincopyprint?
1. //此处如此设计因为玩家位置会发生变化，需要重复获取
2. void HelloWorld::enemyMoveFinished(CCNode* node)
3. {
4.     CCSprite* enemy = (CCSprite*)node;
5.     moveEnemy(enemy);
6. }

[cpp] view plaincopyprint?
1. void HelloWorld::checkMoveEnemy(CCPoint center)
2. {
3.     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
4.     center = convertToNodeSpace(center);
5.     CCRect screen = CCRectMake(center.x-winSize.width/2,center.y-winSize.height/2,winSize.width,winSize.height);
6.     CCObject* e = NULL;
7.     CCARRAY_FOREACH(m_enemies,e)
8.     {
9.         CCSprite* enemy =  dynamic_cast<CCSprite*>(e);
10.         CCPoint temp = enemy->getPosition();
11.         CCPoint pos = convertToNodeSpace(temp);
12.
13.         if(screen.containsPoint(pos))
14.         {
15.             //此处会有重复调用的问题，即对已经移动的怪调用runAction
16.             //if(enemy->isRunning()) //这个函数不能用？
17.             moveEnemy(enemy);
18.         }
19.     }
20. }

[cpp] view plaincopyprint?
1. void HelloWorld::sendProjectile(CCSprite* projectile,CCPoint pos)
2. {
3.
4.     int realX;
5.
6.     CCPoint diff = ccpSub(pos, m_player->getPosition());
7.     if (diff.x >0)
8.     {
9.         realX = (m_tileMap->getContentSize().width) +
10.             (projectile->getContentSize().width/2);
11.     }
12.     else {
13.         realX =-(m_tileMap->getContentSize().width) -
14.             (projectile->getContentSize().width/2);
15.     }
16.     float ratio = (float) diff.y / (float) diff.x;
17.     int realY = ((realX - projectile->getPositionX()) * ratio) + projectile->getPositionY();
18.     CCPoint realDest = ccp(realX, realY);
19.
20.     // Determine the length of how far we're shooting
21.     int offRealX = realX - projectile->getPositionX();
22.     int offRealY = realY - projectile->getPositionY();
23.     float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
24.     float velocity =480/1; // 480pixels/1sec
25.     float realMoveDuration = length/velocity;
26.
27.     CCActionInterval* throwProjectile = CCMoveTo::create(realMoveDuration,realDest);
28.     projectile->runAction(CCSequence::create(throwProjectile,
29.         CCCallFuncN::create(this,callfuncN_selector(HelloWorld::projectileMoveFinished)),NULL));
30.
31. }

[cpp] view plaincopyprint?
1. void HelloWorld::projectileMoveFinished(CCNode* object)
2. {
3.     CCSprite* p = (CCSprite* )object;
4.     if(p)
5.     {
6.         removeChild(p,true);
7.         m_projectiles->removeObject(p);
8.     }
9. }

[cpp] view plaincopyprint?
1. void HelloWorld::checkHit(float dt)
2. {
3.     CCArray* projectilesToDelete = new CCArray;
4.     CCArray* targetToDelete = new CCArray;
5.     CCObject* it = NULL;
6.     CCObject* jt = NULL;
7.     CCRect playerRect = CCRectMake(
8.                 m_player->getPositionX() - m_player->getContentSize().width/2,
9.                 m_player->getPositionY() - m_player->getContentSize().height/2,
10.                 m_player->getContentSize().width,
11.                 m_player->getContentSize().height);
12.
13.     CCARRAY_FOREACH(m_projectiles,it)
14.     {
15.         CCSprite * projectile = dynamic_cast<CCSprite*>(it);
16.         CCRect projectileRect = CCRectMake(
17.             projectile->getPositionX() - projectile->getContentSize().width/2,
18.             projectile->getPositionY() - projectile->getContentSize().height/2,
19.             projectile->getContentSize().width,
20.             projectile->getContentSize().height);
21.         CCARRAY_FOREACH(m_enemies,jt)
22.         {
23.             CCSprite* target = dynamic_cast<CCSprite*>(jt);
24.             CCRect targetRect = CCRectMake(
25.                 target->getPositionX() - target->getContentSize().width/2,
26.                 target->getPositionY() - target->getContentSize().height/2,
27.                 target->getContentSize().width,
28.                 target->getContentSize().height);
29.             if(projectileRect.intersectsRect(targetRect))
30.             {
32.             }
33.
34.         }
35.         CCARRAY_FOREACH(targetToDelete,jt)
36.         {
37.             CCSprite* delTarget = dynamic_cast<CCSprite*>(jt);
38.             m_enemies->removeObject(delTarget,true);
39.             this->removeChild(delTarget,true);
40.         }
41.         if(targetToDelete->count() >0)
42.         {
44.         }
45.         targetToDelete->removeAllObjects();
46.     }
47.     CCARRAY_FOREACH(projectilesToDelete,it)
48.     {
49.         CCSprite* delProject = dynamic_cast<CCSprite*>(it);
50.         m_projectiles->removeObject(delProject);
51.         this->removeChild(delProject,true);
52.     }
53.     CCARRAY_FOREACH(m_enemies,jt)
54.     {
55.         CCSprite* target = dynamic_cast<CCSprite*>(jt);
56.         CCRect targetRect = CCRectMake(
57.                 target->getPositionX() - target->getContentSize().width/2,
58.                 target->getPositionY() - target->getContentSize().height/2,
59.                 target->getContentSize().width,
60.                 target->getContentSize().height);
61.
62.         if(playerRect.intersectsRect(targetRect))
63.         {
64.             GameOverSecne *gameOverScene = GameOverSecne::create();
65.             gameOverScene->getLayer()->getLabel()->setString("You Lose!");
66.             CCDirector::sharedDirector()->replaceScene(gameOverScene);
67.         }
68.     }
69.     targetToDelete->release();
70.     projectilesToDelete->release();
71. }

<