cocos2dx如何制作一个塔防游戏 Cocos2d-x 2.0.4
转载自:http://blog.csdn.net/akof1314/article/details/8674186 本文实践自 Pablo Ruiz 的文章《How To Make a Tower Defense Game》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.0.4进行学习和移植。在这篇文章,将会学习到如何制作一个塔防游戏。在这当中,学习如何在设定的时间内出现一波波的敌人,使这些敌人沿着指定的路点前进,如何在地图上指定的位置创建炮塔,如何使炮塔射击敌人,如何可视化调试路点和炮塔的攻击范围。 步骤如下:
通过放置的背景图片,可以直观的看出哪些地方允许玩家放置炮塔。编译运行,如下图所示: |
cocos2d::CCArray*towerBases;
|
{
CCArray*towerPositions=CCArray::createWithContentsOfFile( "TowersPosition.plist");
towerBases=CCArray::createWithCapacity( 10);
towerBases->retain();
CCObject*pObject= NULL;
CCARRAY_FOREACH(towerPositions,pObject)
{
CCDictionary*towerPos=(CCDictionary*)pObject;
CCSprite*towerBase=CCSprite::create( "open_spot.png");
this->addChild(towerBase);
towerBase->setPosition(ccp(((CCString*)towerPos->objectForKey( "x"))->intValue(),
((CCString*)towerPos->objectForKey( "y"))->intValue()));
towerBases->addObject(towerBase);
}
}
在init函数里面,添加背景图片代码之后,添加如下代码:
5.开始建立炮塔。打开 HelloWorldScene.h 文件,添加如下代码:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#define__TOWER_H__
#include "cocos2d.h"
#include "HelloWorldScene.h"
#definekTOWER_COST 300
classTower: publiccocos2d::CCNode
{
public:
Tower( void);
~Tower( void);
staticTower*nodeWithTheGame(HelloWorld*game,cocos2d::CCPointlocation);
boolinitWithTheGame(HelloWorld*game,cocos2d::CCPointlocation);
voidupdate( floatdt);
voiddraw( void);
CC_SYNTHESIZE(HelloWorld*,_theGame,TheGame);
CC_SYNTHESIZE(cocos2d::CCSprite*,_mySprite,MySprite);
private:
intattackRange;
intdamage;
floatfireRate;
};
#endif //__TOWER_H__
打开Tower.cpp文件,代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using namespacecocos2d;
Tower::Tower( void)
{
}
Tower::~Tower( void)
{
}
Tower*Tower::nodeWithTheGame(HelloWorld*game,CCPointlocation)
{
Tower*pRet= newTower();
if(pRet&&pRet->initWithTheGame(game,location))
{
returnpRet;
}
else
{
deletepRet;
pRet= NULL;
return NULL;
}
}
boolTower::initWithTheGame(HelloWorld*game,CCPointlocation)
{
boolbRet= false;
do
{
attackRange= 70;
damage= 10;
fireRate= 1;
_mySprite=CCSprite::create( "tower.png");
this->addChild(_mySprite);
_mySprite->setPosition(location);
_theGame=game;
_theGame->addChild( this);
this->scheduleUpdate();
bRet= true;
} while( 0);
returnbRet;
}
voidTower::update( floatdt)
{
}
voidTower::draw( void)
{
#ifdefCOCOS2D_DEBUG
ccDrawColor4F( 255,255,255);
ccDrawCircle(_mySprite->getPosition(),attackRange,360,30,false);
#endif
CCNode::draw();
}
这个Tower类包含几个属性:一个精灵对象,这是炮塔的可视化表现;一个父层的引用,方便访问父层;还有三个变量:
- attackRange: 炮塔可以攻击敌人的距离。
- damage: 炮塔对敌人造成的伤害值。
- fireRate: 炮塔再次攻击敌人的时间间隔。
6.让玩家添加炮塔。打开 HelloWorldScene.cpp 文件,加入以下头文件声明:
2
_towers->retain();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
return true;
}
voidHelloWorld::ccTouchesBegan(CCSet*pTouches,CCEvent*pEvent)
{
CCSetIteratoriter=pTouches->begin();
for(;iter!=pTouches->end();iter++)
{
CCTouch*pTouch=(CCTouch*)(*iter);
CCPointlocation=pTouch->getLocation();
CCObject*pObject= NULL;
CCARRAY_FOREACH(towerBases,pObject)
{
CCSprite*tb=(CCSprite*)pObject;
if( this->canBuyTower()&&tb->boundingBox().containsPoint(location)&&!tb->getUserData())
{
//Wewillspendourgoldlater.
Tower*tower=Tower::nodeWithTheGame( this,tb->getPosition());
_towers->addObject(tower);
tb->setUserData(tower);
}
}
}
}
方法ccTouchesBegan检测当用户触摸屏幕上任何点时,遍历towerBases数组,检查触摸点是否包含在任何一个塔基上。不过在创建炮塔前,还有两件事需要检查:
①玩家是否买得起炮塔?canBuyTower方法用来检查玩家是否有足够的金币来购买炮塔。在这里先假设玩家有很多金币,方法返回true。
②玩家是否违法了建筑规则?如果tb的UserData已经设置了,那么这个塔基已经有了炮塔,不能再添加一个新的了。
如果一切检查都通过,那么就创建一个新的炮塔,放置在塔基上,并将它添加到炮塔数组中。编译运行,触摸塔基,就可以看到炮塔放置上去了,并且它的周围还有白色的圆圈显示攻击范围,如下图所示:
7.添加路点。敌人将会沿着一系列的路点前进,这些简单相互连接的点构成了一条路径,敌人在这条路径上进行行走。敌人会出现在第一个路点,搜寻列表中的下一个路点,移动到那个位置,重复这个过程,直到他们到达列表中的最后一个路点——玩家基地。如果被敌人到达基地,那么玩家就会受到损害。添加Waypoint类,派生自CCNode类,Waypoint.h文件代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define__WAYPOINT_H__
#include "cocos2d.h"
#include "HelloWorldScene.h"
classWaypoint: publiccocos2d::CCNode
{
public:
Waypoint( void);
~Waypoint( void);
staticWaypoint*nodeWithTheGame(HelloWorld*game,cocos2d::CCPointlocation);
voiddraw( void);
CC_SYNTHESIZE(cocos2d::CCPoint,_myPosition,MyPosition);
CC_SYNTHESIZE(Waypoint*,_nextWaypoint,NextWaypoint);
private:
HelloWorld*theGame;
};
#endif //__WAYPOINT_H__
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using namespacecocos2d;
Waypoint::Waypoint( void)
{
_nextWaypoint= NULL;
}
Waypoint::~Waypoint( void)
{
}
Waypoint*Waypoint::nodeWithTheGame(HelloWorld*game,CCPointlocation)
{
Waypoint*pRet= newWaypoint();
if(pRet&&pRet->initWithTheGame(game,location))
{
returnpRet;
}
else
{
deletepRet;
pRet= NULL;
return NULL;
}
}
boolWaypoint::initWithTheGame(HelloWorld*game,CCPointlocation)
{
boolbRet= false;
do
{
theGame=game;
_myPosition=location;
this->setPosition(CCPointZero);
theGame->addChild( this);
bRet= true;
} while( 0);
returnbRet;
}
voidWaypoint::draw( void)
{
#ifdefCOCOS2D_DEBUG
ccDrawColor4F( 0,0,255);
ccDrawCircle(_myPosition,6,false);
ccDrawCircle(_myPosition,2,false);
if(_nextWaypoint)
{
ccDrawLine(_myPosition,_nextWaypoint->_myPosition);
}
#endif
CCNode::draw();
}
8.创建路点列表。打开HelloWorldScene.h文件,添加以下代码:
{
_waypoints=CCArray::create();
_waypoints->retain();
Waypoint*waypoint1=Waypoint::nodeWithTheGame( this,ccp( 420,35));
_waypoints->addObject(waypoint1);
Waypoint*waypoint2=Waypoint::nodeWithTheGame( this,ccp( 35,35));
_waypoints->addObject(waypoint2);
waypoint2->setNextWaypoint(waypoint1);
Waypoint*waypoint3=Waypoint::nodeWithTheGame( this,130));
_waypoints->addObject(waypoint3);
waypoint3->setNextWaypoint(waypoint2);
Waypoint*waypoint4=Waypoint::nodeWithTheGame( this,ccp( 445,130));
_waypoints->addObject(waypoint4);
waypoint4->setNextWaypoint(waypoint3);
Waypoint*waypoint5=Waypoint::nodeWithTheGame( this,220));
_waypoints->addObject(waypoint5);
waypoint5->setNextWaypoint(waypoint4);
Waypoint*waypoint6=Waypoint::nodeWithTheGame( this,ccp(- 40,220));
_waypoints->addObject(waypoint6);
waypoint6->setNextWaypoint(waypoint5);
}
在init函数,添加如下代码:
在地图上有6个路点,这是敌人的行走路线。在让敌人出现在游戏中前,还需要添加一个辅助方法。打开 HelloWorldScene.cpp 文件,添加方法如下:
2
3
4
5
6
7
8
9
10
11
12
13
{
floatxdif=circlePoint.x-circlePointTwo.x;
floatydif=circlePoint.y-circlePointTwo.y;
floatdistance=sqrt(xdif*xdif+ydif*ydif);
if(distance<=radius+radiusTwo)
{
return true;
}
return false;
}
方法collisionWithCircle用于判断两个圆是否碰撞或者相交。这将用于判断敌人是否到达一个路点,同时也可以检测敌人是否在炮塔的攻击范围之内。
9.添加敌人。打开HelloWorldScene.h文件,添加以下代码:
2
3
4
cocos2d::CCLabelBMFont*ui_wave_lbl;
打开HelloWorldScene.cpp文件,在析构函数里,添加如下代码:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define__ENEMY_H__
#include "cocos2d.h"
#include "HelloWorldScene.h"
#include "Waypoint.h"
classEnemy: publiccocos2d::CCNode
{
public:
Enemy( void);
~Enemy( void);
staticEnemy*nodeWithTheGame(HelloWorld*game);
boolinitWithTheGame(HelloWorld*game);
voiddoActivate( floatdt);
voidgetRemoved();
voidupdate( floatdt);
voiddraw( void);
CC_SYNTHESIZE(HelloWorld*,MySprite);
private:
cocos2d::CCPointmyPosition;
intmaxHp;
intcurrentHp;
floatwalkingSpeed;
Waypoint*destinationWaypoint;
boolactive;
};
#endif //__ENEMY_H__
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using namespacecocos2d;
#defineHEALTH_BAR_WIDTH 20
#defineHEALTH_BAR_ORIGIN- 10
Enemy::Enemy( void)
{
}
Enemy::~Enemy( void)
{
}
Enemy*Enemy::nodeWithTheGame(HelloWorld*game)
{
Enemy*pRet= newEnemy();
if(pRet&&pRet->initWithTheGame(game))
{
returnpRet;
}
else
{
deletepRet;
pRet= NULL;
return NULL;
}
}
boolEnemy::initWithTheGame(HelloWorld*game)
{
boolbRet= false;
do
{
maxHp= 40;
currentHp=maxHp;
active= false;
walkingSpeed= 0. 5;
_theGame=game;
_mySprite=CCSprite::create( "enemy.png");
this->addChild(_mySprite);
Waypoint*waypoint=(Waypoint*)_theGame->getWaypoints()->objectAtIndex(_theGame->getWaypoints()->count()- 1);
destinationWaypoint=waypoint->getNextWaypoint();
CCPointpos=waypoint->getMyPosition();
myPosition=pos;
_mySprite->setPosition(pos);
_theGame->addChild( this);
this->scheduleUpdate();
bRet= true;
} while( 0);
returnbRet;
}
voidEnemy::doActivate( floatdt)
{
active= true;
}
voidEnemy::getRemoved()
{
this->getParent()->removeChild( this,true);
_theGame->getEnemies()->removeObject( this);
//Notifythegamethatwekilledanenemysowecancheckifwecansendanotherwave
_theGame->enemyGotKilled();
}
voidEnemy::update( floatdt)
{
if(!active)
{
return;
}
if(_theGame->collisionWithCircle(myPosition,1,destinationWaypoint->getMyPosition(),1))
{
if(destinationWaypoint->getNextWaypoint())
{
destinationWaypoint=destinationWaypoint->getNextWaypoint();
}
else
{
//Reachedtheendoftheroad.Damagetheplayer
_theGame->getHpDamage();
this->getRemoved();
}
}
CCPointtargetPoint=destinationWaypoint->getMyPosition();
floatmovementSpeed=walkingSpeed;
CCPointnormalized=ccpNormalize(ccp(targetPoint.x-myPosition.x,targetPoint.y-myPosition.y));
_mySprite->setRotation(CC_RADIANS_TO_DEGREES(atan2(normalized.y,-normalized.x)));
myPosition=ccp(myPosition.x+normalized.x*movementSpeed,myPosition.y+normalized.y*movementSpeed);
_mySprite->setPosition(myPosition);
}
voidEnemy::draw( void)
{
CCPointhealthBarBack[]={
ccp(_mySprite->getPosition().x- 10,_mySprite->getPosition().y+ 16),
ccp(_mySprite->getPosition().x+ 10,_mySprite->getPosition().y+ 14),
ccp(_mySprite->getPosition().x- 10,_mySprite->getPosition().y+ 14)
};
ccDrawSolidPoly(healthBarBack,4,ccc4f( 255,255));
CCPointhealthBar[]={
ccp(_mySprite->getPosition().x+HEALTH_BAR_ORIGIN,
ccp(_mySprite->getPosition().x+HEALTH_BAR_ORIGIN+( float)(currentHp*HEALTH_BAR_WIDTH)/maxHp,
ccp(_mySprite->getPosition().x+HEALTH_BAR_ORIGIN,_mySprite->getPosition().y+ 14)
};
ccDrawSolidPoly(healthBar,ccc4f( 0,255));
CCNode::draw();
}
首先,通过传递一个HelloWorld对象引用进行初始化。在初始化函数里面,对一些重要的变量进行设置:
- maxHP: 敌人的生命值。
- walkingSpeed: 敌人的移动速度。
- mySprite: 存储敌人的可视化表现。
- destinationWaypoint: 存储下一个路点的引用。
①计算出从当前位置到目标位置的向量,然后将其长度设置为1(向量标准化)
②将移动速度乘以标准化向量,得到移动的距离,将它与当前坐标进行相加,得到新的坐标位置。
最后, draw 方法在精灵上面简单的实现了一条血量条。它首先绘制一个红色背景,然后根据敌人的当前生命值用绿色进行覆盖血量条。
10.显示敌人。打开 HelloWorldScene.cpp 文件,添加头文件声明:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
CCArray*waveData=CCArray::createWithContentsOfFile( "Waves.plist");
if(wave>=waveData->count())
{
return false;
}
CCArray*currentWaveData=(CCArray*)waveData->objectAtIndex(wave);
CCObject*pObject= NULL;
CCARRAY_FOREACH(currentWaveData,pObject)
{
CCDictionary*enemyData=(CCDictionary*)pObject;
Enemy*enemy=Enemy::nodeWithTheGame( this);
_enemies->addObject(enemy);
enemy->schedule(schedule_selector(Enemy::doActivate),((CCString*)enemyData->objectForKey( "spawnTime"))->floatValue());
}
wave++;
ui_wave_lbl->setString(CCString::createWithFormat( "WAVE:%d",wave)->getCString());
return true;
}
voidHelloWorld::enemyGotKilled()
{
//Iftherearenomoreenemies.
if(_enemies->count()<= 0)
{
if(! this->loadWave())
{
CCLog( "Youwin!");
CCDirector::sharedDirector()->replaceScene(CCTransitionSplitCols::create( 1,HelloWorld::scene()));
}
}
}
voidHelloWorld::getHpDamage()
{
}
在init函数里面,添加如下代码:
2
3
4
5
6
7
8
9
ui_wave_lbl=CCLabelBMFont::create(CCString::createWithFormat( "WAVE:%d",wave)->getCString(),"font_red_14.fnt");
this->addChild(ui_wave_lbl,10);
ui_wave_lbl->setPosition(ccp( 400,wins.height- 12));
ui_wave_lbl->setAnchorPoint(ccp( 0,0. 5));
_enemies=CCArray::create();
_enemies->retain();
this->loadWave();
现在对上面的代码进行一些解释。最重要的部分是loadWave方法,它从Waves.plist文件读取数据。查看这个文件,可以看到它包含了3个数组,每个数组代表着一波敌人。第一个数组包含6个字典,每个字典定义了一个敌人。在本篇文章中,这个字典仅存储敌人的出现时间,但是也可用于定义敌人类型或者其他特殊属性,以区分不同的敌人。loadWave方法检查下一波应出现的敌人,根据波信息创建相应的敌人,并安排它们在规定的时间出现在屏幕上。enemyGotKilled方法检查当前屏幕上的敌人数量,如果已经没有敌人的话,那么就让下一波敌人出现。之后,还使用这个方法来判断玩家是否赢得了游戏。编译运行,敌人正向玩家基地前进,如下图所示:
10.炮塔攻击。每座塔进行检查是否有敌人出现在攻击范围之内,如果有的话,对敌人进行开火,直到以下两种情况之一发生:敌人移动出范围;敌人被消灭。那么炮塔就会寻找下一个敌人。打开Tower.h文件,添加以下代码:
Enemy*chosenEnemy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
this->schedule(schedule_selector(Tower::shootWeapon),fireRate);
}
voidTower::chosenEnemyForAttack(Enemy*enemy)
{
chosenEnemy= NULL;
chosenEnemy=enemy;
this->attackEnemy();
enemy->getAttacked( this);
}
voidTower::shootWeapon( floatdt)
{
CCSprite*bullet=CCSprite::create( "bullet.png");
_theGame->addChild(bullet);
bullet->setPosition(_mySprite->getPosition());
bullet->runAction(CCSequence::create(
CCMoveTo::create( 0. 1,chosenEnemy->getMySprite()->getPosition()),
CCCallFunc::create( this,callfunc_selector(Tower::damageEnemy)),
CCCallFuncN::create( this,callfuncN_selector(Tower::removeBullet)),
NULL));
}
voidTower::removeBullet(CCSprite*bullet)
{
bullet->getParent()->removeChild(bullet,true);
}
voidTower::damageEnemy()
{
if(chosenEnemy)
{
chosenEnemy->getDamaged(damage);
}
}
voidTower::targetKilled()
{
if(chosenEnemy)
{
chosenEnemy= NULL;
}
this->unschedule(schedule_selector(Tower::shootWeapon));
}
voidTower::lostSightOfEnemy()
{
chosenEnemy->gotLostSight( this);
if(chosenEnemy)
{
chosenEnemy= NULL;
}
this->unschedule(schedule_selector(Tower::shootWeapon));
}
{
if(chosenEnemy)
{
//Wemakeitturntotargettheenemychosen
CCPointnormalized=ccpNormalize(ccp(chosenEnemy->getMySprite()->getPosition().x-_mySprite->getPosition().x,
chosenEnemy->getMySprite()->getPosition().y-_mySprite->getPosition().y));
_mySprite->setRotation(CC_RADIANS_TO_DEGREES(atan2(normalized.y,-normalized.x))+ 90);
if(!_theGame->collisionWithCircle(_mySprite->getPosition(),chosenEnemy->getMySprite()->getPosition(),1))
{
this->lostSightOfEnemy();
}
}
else
{
CCObject*pObject= NULL;
CCARRAY_FOREACH(_theGame->getEnemies(),pObject)
{
Enemy*enemy=(Enemy*)pObject;
if(_theGame->collisionWithCircle(_mySprite->getPosition(),enemy->getMySprite()->getPosition(),1))
{
this->chosenEnemyForAttack(enemy);
break;
}
}
}
}
打开Enemy.h文件,添加以下代码:
attackedBy->retain();
2
3
4
5
6
CCARRAY_FOREACH(attackedBy,pObject)
{
Tower*attacker=(Tower*)pObject;
attacker->targetKilled();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
attackedBy->addObject(attacker);
}
voidEnemy::gotLostSight(Tower*attacker)
{
attackedBy->removeObject(attacker);
}
voidEnemy::getDamaged( intdamage)
{
currentHp-=damage;
if(currentHp<= 0)
{
this->getRemoved();
}
}
代码中最重要的部分是在Tower类的update方法。炮塔不断检查敌人是否在攻击范围内,如果是的话,炮塔将旋转朝向敌人,开火攻击。一个敌人一旦被标记为被攻击,将会调用方法让炮塔以攻击间隔发射子弹。反过来,每个敌人都存储有向其攻击的炮塔列表,所以如果敌人被杀死了,那么炮塔就会被通知停止攻击。编译运行,放置几个炮塔在地图上,将会看到一旦敌人进入炮塔的攻击范围,炮塔就会向它们开火攻击,敌人的血量条就会减少,直到被消灭。如下图所示:
11.显示玩家血量。打开HelloWorldScene.h文件,添加以下代码:
2
3
cocos2d::CCLabelBMFont*ui_hp_lbl;
boolgameEnded;
2
3
4
5
playerHp= 5;
ui_hp_lbl=CCLabelBMFont::create(CCString::createWithFormat( "HP:%d",playerHp)->getCString(),"font_red_14.fnt");
this->addChild(ui_hp_lbl,10);
ui_hp_lbl->setPosition(ccp( 35,wins.height- 12));
{
playerHp--;
ui_hp_lbl->setString(CCString::createWithFormat( "HP:%d",playerHp)->getCString());
if(playerHp<= 0)
{
this->doGameOver();
}
}
voidHelloWorld::doGameOver()
{
if(!gameEnded)
{
gameEnded= true;
CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create( 1,HelloWorld::scene()));
}
}
添加的方法为减少玩家生命值,更新标签,并检查玩家生命是否耗尽,如果是的话,游戏就结束了。当敌人到达基地的时候,getHpDamage方法被调用。编译运行,让敌人到达基地,你将会看到玩家的生命在减少,直到游戏失败。如下图所示:
12.限制金币供应量。大多数游戏都实现了“零和”功能,建造每座炮塔需要一定的资源,并给玩家有限的资源进行分配。打开HelloWorldScene.h文件,添加如下代码:
cocos2d::CCLabelBMFont*ui_gold_lbl;
ui_gold_lbl=CCLabelBMFont::create(CCString::createWithFormat( "GOLD:%d",playerGold)->getCString(),"font_red_14.fnt");
this->addChild(ui_gold_lbl,10);
ui_gold_lbl->setPosition(ccp( 135,wins.height- 12));
ui_gold_lbl->setAnchorPoint(ccp( 0,0. 5));
{
playerGold+=gold;
ui_gold_lbl->setString(CCString::createWithFormat( "GOLD:%d",playerGold)->getCString());
}
2
3
4
5
6
7
8
{
if(playerGold-kTOWER_COST>= 0)
{
return true;
}
return false;
}
ui_gold_lbl->setString(CCString::createWithFormat( "GOLD:%d",playerGold)->getCString());
13.加入背景音乐和音效。打开 HelloWorldScene.cpp 文件,添加头文件声明:
参考资料:
1.How To Make a Tower Defense Gamehttp://www.raywenderlich.com/15730/how-to-make-a-tower-defense-game
2.钓龟岛保卫战-如何从零开始制作一款iOS塔防游戏(新)http://article.ityran.com/archives/1941
非常感谢以上资料,本例子源代码附加资源下载地址:http://download.csdn.net/detail/akof1314/5143209 如文章存在错误之处,欢迎指出,以便改正。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!