Preview: http://www.cocoachina.com/bbs/read.php?tid=202870
5.3 Wave
In the Wave.h:
在Wave.h里面:
#pragma once
#include "cocos2d.h"
#include "Creep.h"
USING_NS_CC;
class Wave: public Node
{
public:
Point position;
int totalCreeps;
double spawnRate;
Creep* creepType;
virtual bool init();
Wave* initWithCreep(Creep* creep, double SpawnRate,int TotalCreeps);
CREATE_FUNC(Wave);
};
```
In the Wave.cpp:
在Wave.cpp里面:
#include "Wave.h"
USING_NS_CC;
bool Wave::init()
{
if (!Node::init())
{
return false;
}
return true;
}
Wave* Wave::initWithCreep(Creep* creep, double SpawnRate, int TotalCreeps)
{
this->creepType = creep;
this->spawnRate = SpawnRate;
this->totalCreeps = TotalCreeps;
return this;
}
```
Now we already know the ‘WayPoint’, ‘Wave’ and ‘DataModel’. Let’s go and see the bad guys:
现在我们已经知道了“WayPoint”“Wave”“DataModel”的作用了。现在我们来看看这些坏蛋吧~
5.4 Creep
Enemy1
In the Creep.h:
在Creep.h里
#pragma once
#include "cocos2d.h"
#include "WayPoint.h"
USING_NS_CC;
class Creep: public Sprite
{
public:
int curHp;
int moveDuration;
int curWaypoint;
int tag;
Sprite* sprite;
virtual bool init();
Creep* initWithCreep(Creep* copyFrom);
WayPoint* getNextWaypoint();
WayPoint* getCurrentWaypoint();
CREATE_FUNC(Creep);
};
class FastRedCreep: public Creep
{
public:
static Creep* creep();
};
class StrongGreenCreep: public Creep
{
public:
static Creep* creep();
};
```
Here we are going to define the values of the enemies. Including the current health point, the time they finish their movement, their current and next location and so on…
这里我们要开始定义敌人的值。包括现有体力、移动所需时间、现在以及接下来的位置等等。
Here is how we complete the implementation:
这里就是我们怎么完成这个方法的:
Enemy2
In the Creep.cpp:
在Creep.cpp里面:
#include "Creep.h"
#include "WayPoint.h"
#include "DataModel.h"
USING_NS_CC;
bool Creep::init()
{
if (!Sprite::init())
{
return false;
}
return true;
}
Creep* Creep::initWithCreep(Creep* copyFrom)
{
return NULL;
}
Creep* FastRedCreep::creep()
{
auto creep = Creep::create();
creep->sprite = Sprite::create("Enemy1.png");
creep->setScale(0.4);
creep->addChild(creep->sprite, 0);
creep->curHp = 10;
creep->moveDuration = 3;
creep->curWaypoint = 0;
return creep;
}
Creep* StrongGreenCreep::creep()
{
auto creep = Creep::create();
creep->sprite = Sprite::create("Enemy2.png");
creep->setScale(0.4);
creep->addChild(creep->sprite, 0);
creep->curHp = 30;
creep->moveDuration = 8;
creep->curWaypoint = 0;
return creep;
}
```
/*
This is how we make it work---we define a class. We can use “FastRedCreep creep” to call it. Then it will return a ‘creep’ object, so that we can add it into the scene. Since ‘Creep’ is derived from the CCSprite, it get all the features from the CCSprite.
这就是我们如何让他工作的—我们定义了一个类。我们引用“FastRedCreep”来引用它。然后返回一个’Creep’物体,然后我们就可以把它添加进场景里面了。毕竟‘Creep’是从Sprite里继承过来的。所以它有着Sprite的所有特征。*/
Next, in the class ‘Creep’, we are going to use the class ‘DataModel’ and the ‘WayPoint’.
接下来,在类‘Creep’中,我们将要使用到类‘DataModel’和 ’WayPoint’。
WayPoint* Creep::getCurrentWaypoint()
{
DataModel* m = DataModel::getModel();
WayPoint* waypoint = (WayPoint *)m->waypoints.at(this->curWaypoint);
return waypoint;
}
WayPoint* Creep::getNextWaypoint()
{
DataModel* m = DataModel::getModel();
if (this->curWaypoint != 7)
{
this->curWaypoint++;
} else
{
this->curWaypoint = 0;
}
CCLOG("%d",this->curWaypoint); // For testing.
WayPoint *waypoint = (WayPoint *)m->waypoints.at(curWaypoint);
return waypoint;
}
```
Here, we define the method about how to get the current waypoint and how to get to the next one. When the enemy go to the seventh waypoint, it will reset the value into the ‘0’ and the enemy will start from the beginning again.
这里,我们定义了关于怎样获取现有路标和下一个路标的方法。当我们的敌人到达了第七个路标后,会重设路标的值为‘0’然后敌人会重头开始行动。
未完待续~(一日一更)
Next:http://www.cocoachina.com/bbs/read.php?tid=203362