7.添加路点。敌人将会沿着一系列的路点前进,这些简单相互连接的点构成了一条路径,敌人在这条路径上进行行走。敌人会出现在第一个路点,搜寻列表中的下一个路点,移动到那个位置,重复这个过程,直到他们到达列表中的最后一个路点——玩家基地。如果被敌人到达基地,那么玩家就会受到损害。添加Waypoint类,派生自CCNode类,Waypoint.h文件代码如下:
#include"cocos2d.h" #include"HelloWorldScene.h" class Waypoint : public cocos2d::CCNode { public: Waypoint(void); ~Waypoint(void); //初始化 static Waypoint* nodeWithTheGame(HelloWorld* game, cocos2d::CCPoint location); bool initWithTheGame(HelloWorld* game, cocos2d::CCPoint location); //画路线 void draw(void); //目前路线坐标点 CC_SYNTHESIZE(cocos2d::CCPoint, _myPosition, MyPosition); //下一个坐标点 CC_SYNTHESIZE(Waypoint*, _nextWaypoint, NextWaypoint); private: //游戏层 HelloWorld* theGame; };打开 Waypoint.cpp 文件,代码如下:
1
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 |
#include
"Waypoint.h"
using namespace cocos2d; Waypoint::Waypoint( void) { _nextWaypoint = NULL; } Waypoint::~Waypoint( void) { } Waypoint* Waypoint::nodeWithTheGame(HelloWorld* game, CCPoint location) { Waypoint *pRet = new Waypoint(); if (pRet && pRet->initWithTheGame(game, location)) { return pRet; } else { delete pRet; pRet = NULL; return NULL; } } bool Waypoint::initWithTheGame(HelloWorld* game, CCPoint location) { bool bRet = false; do { theGame = game; _myPosition = location; this->setPosition(CCPointZero); theGame->addChild( this); bRet = true; } while ( 0); return bRet; } void Waypoint::draw( void) { #ifdef COCOS2D_DEBUG ccDrawColor4F( 0, 255, 0, 255); ccDrawCircle(_myPosition, 6, 360, 30, false); ccDrawCircle(_myPosition, 2, 360, 30, false); if (_nextWaypoint) { ccDrawLine(_myPosition, _nextWaypoint->_myPosition); } #endif CCNode::draw(); } |
首先,通过传入的HelloWorld对象引用和路点位置坐标,进行初始化一个waypoint对象。每个路点都包含下一个路点的引用,这将会创建一个路点链接列表。每个路点知道列表中的下一个路点。通过这种方式,可以引导敌人沿着链表上的路点到达他们的最终目的地。最后,draw方法绘制显示路点的位置,并且绘制一条直线将其与下一个路点进行连接,这仅仅用于调试目的。
8.创建路点列表。打开HelloWorldScene.h文件,添加以下代码:
1
|
CC_SYNTHESIZE_RETAIN(cocos2d::CCArray*, _waypoints, Waypoints);
|
打开HelloWorldScene.cpp文件,加入以下头文件声明:
1
|
#include
"Waypoint.h"
|
在析构函数中添加如下代码:
1
|
_waypoints->release();
|
添加以下方法:
1
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 |
void HelloWorld::addWaypoints()
{ _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, ccp( 35, 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, ccp( 445, 220)); _waypoints->addObject(waypoint5); waypoint5->setNextWaypoint(waypoint4); Waypoint *waypoint6 = Waypoint::nodeWithTheGame( this, ccp(- 40, 220)); _waypoints->addObject(waypoint6); waypoint6->setNextWaypoint(waypoint5); } |
在init函数,添加如下代码:
1
|
this->addWaypoints();
|
截止到目前的"塔防游戏.zip" http://vdisk.weibo.com/s/FJsHo
在地图上有6个路点,这是敌人的行走路线。在让敌人出现在游戏中前,还需要添加一个辅助方法。打开HelloWorldScene.cpp文件,添加方法如下:
//碰撞检测 第一个参数:第一个坐标的中心点 第二个:第一个对象的半径 第三个参数:第二个坐标的中心点 第四个:第二个对象的半径 bool HelloWorld::collisionWithCircle(CCPoint circlePoint, float radius, CCPoint circlePointTwo, float radiusTwo) { //两个坐标之差 float xdif = circlePoint.x - circlePointTwo.x; float ydif = circlePoint.y - circlePointTwo.y; //坐标点之间的距离 float distance = sqrt(xdif * xdif + ydif * ydif); //判断 if(distance <= radius + radiusTwo) { return true; } return false; }方法 collisionWithCircle 用于判断两个圆是否碰撞或者相交。这将用于判断敌人是否到达一个路点,同时也可以检测敌人是否在炮塔的攻击范围之内。
代码例子 http://vdisk.weibo.com/s/BDn59yfnBVf02