Cocos2d-x 3.3塔防游戏《保卫萝卜》教程06:重构让角色鲜活起来

2015年03月18日 09:29 0 点赞 0 评论 更新于 2025-11-21 17:33

\n\t       温故而知新,新篇开始前简单回顾一下学习过的章节,环境搭建、项目创建、项目解析、游戏原型、屏幕适配,今天要开始的是重构角色…… \n

\n

\n\t一、本篇前提: \n

\n

\n\t完成前一篇的内容。\n

\n

\n\t具体参考:Cocos2d-x 3.3塔防游戏《保卫萝卜》教程05:对游戏原型进行屏幕适配完善 \n

\n

\n\t
\n

\n

\n\t二、本篇目标: \n

\n

\n\t说说游戏中各种角色的动作、属性以及重构思路;\n

\n

\n\t进行代码重构让色狼大叔和女主角生动鲜活起来;\n

\n

\n\t三、内容:\n

\n

\n\t说说游戏中各种角色的动作、属性以及重构思路\n

\n

\n\t通过前两篇我们建立的一个简陋的游戏原型,但是游戏中的人物比如色狼大叔、女主角都看去来很呆板不够鲜活,比如色狼会沿着道路移动,那这个只能说是移动根本不像是在走路,没有走的动作感觉就是沿着道路在漂移,女主角也是一动不动的站那里。这样的游戏很没有乐趣,所以需要给这些游戏角色加入动作和表情,让人看去来他们是鲜活的,对这些角色进行一下简单的动画以及属性分析如下:\n

\n

\n\t色狼大叔的动画:\n

\n

\n\t1、静止动画:游戏刚开始处于静止状态\n

\n

\n\t2、走路动画:沿着道路走\n

\n

\n\t3、死亡动画:当子弹击中血量消耗完时死亡消失\n

\n

\n\t色狼大叔的属性:\n

\n

\n\t1、是否运动:色狼是否处于激活沿着道路行走\n

\n

\n\t2、是否死亡:是否被炮塔打死\n

\n

\n\t3、行走速度:不同色狼的行走速度不同\n

\n

\n\t4、色狼血量:不同色狼血量不同\n

\n

\n\t5、行走:沿着道路行走\n

\n

\n\t女主角的动画:\n

\n

\n\t1、静止动画:游戏刚开始处于静止状态\n

\n

\n\t2、卖萌动画:不能像木头似的,就加点表情动作\n

\n

\n\t3、死亡动画:当纯洁值被色狼玷污光了就死亡了\n

\n

\n\t女主角的属性:\n

\n

\n\t1、女主角贞洁值:相当于生命值\n

\n

\n\t根据上面的分析我们把每个角色拆分成动画显示和业务属性逻辑两个部分,对色狼和女主角进行代码重构。\n

\n

\n\t
\n

\nluobo1.jpg \n

\n\t重构后大概结构如上图:\n

\n

\n\tActionSprite:属于CCSprite类,负责游戏角色精灵的动画显示,Luoli(萝莉)、DaShu(大叔)、JiaoShou(叫兽)等角色精灵均继承自ActionSprite,都属于动画显示类。\n

\n

\n\tLuoli(萝莉):属ActionSprite的子类,负责女主角的动画效果展示。\n

\n

\n\tDaShu(大叔):属ActionSprite的子类,负责大叔色狼的动画效果展示。\n

\n

\n\tJiaoShou (叫兽):属ActionSprite的子类,负责叫兽色狼的动画效果展示。\n

\n

\n\tSelang(色狼):属于CCNode类,负责色狼的行走、血量、速度、攻击等具体的业务,每个Selang都包含一个DaShu(大叔)或JiaoShou(叫兽)类的游戏精灵。并且具备最重要的行为实现沿着道路行走。\n

\n

\n\tGirl(女孩):属于CCNode类,负责女主角的血量等具体的业务,每个Girl都包含一个Luoli类的游戏精灵。\n

\n

\n\t进行代码重构让色狼大叔和女主角生动鲜活起来\n

\n

\n\t打开项目工程按照上面的思路重点对色狼和女主角的代码实现进行重构。\n

\n

\n\t色狼大叔代码重构\n

\n

\n\t第一步:新建ActionSprite.h、ActionSprite.cpp类(角色动画类),这个类继承自CCSprite负责游戏角色的动画效果显示,色狼和女孩都会是这个类的子类。\n

\n

\n\tActionSprite.h代码:\n

\n

\n\t//声明一个动作状态的枚举类型\n

\n

\n\ttypedef enum _ActionState{\n

\n

\n\tkActionStateNone = 0, //无状态\n

\n

\n\tkActionStateIdle, //静止状态\n

\n

\n\tkActionStateWalk, //行走状态\n

\n

\n\tkActionStateDeath //死亡状态\n

\n

\n\t}ActionState;\n

\n

\n\tclass ActionSprite: public cocos2d::CCSprite\n

\n

\n\t{\n

\n

\n\tpublic:\n

\n

\n\tActionSprite(void);\n

\n

\n\t~ActionSprite(void);\n

\n

\n\t//静止\n

\n

\n\tvoid idle();\n

\n

\n\t//死亡\n

\n

\n\tvoid death();\n

\n

\n\t//行走\n

\n

\n\tvoid walk();\n

\n

\n\t//价格\n

\n

\n\tCC_SYNTHESIZE(int,_price,Price);\n

\n

\n\t//生命值\n

\n

\n\tCC_SYNTHESIZE(float,_hp,HP);\n

\n

\n\t//静止状态动作\n

\n

\n\tCC_SYNTHESIZE_RETAIN(cocos2d::Action*,_idleAction,IdleAction);\n

\n

\n\t//死亡状态动作\n

\n

\n\tCC_SYNTHESIZE_RETAIN(cocos2d::Action*,_deathAction,DeathAction);\n

\n

\n\t//行走状态动作\n

\n

\n\tCC_SYNTHESIZE_RETAIN(cocos2d::Action*,_walkAction,WalkAction);\n

\n

\n\t//当前动作状态\n

\n

\n\tCC_SYNTHESIZE(ActionState,_actionState,ActionState);\n

\n

\n\t//行走速度\n

\n

\n\tCC_SYNTHESIZE(float,_walkSpeed,WalkSpeed);\n

\n

\n\t//伤害值\n

\n

\n\tCC_SYNTHESIZE(float,_damage,Damage);\n

\n

\n\t//金钱\n

\n

\n\tCC_SYNTHESIZE(float,_money,Money);\n

\n

\n\t//是否有光环\n

\n

\n\tCC_SYNTHESIZE(bool,_halo,Halo);\n

\n

\n\t};\n

\n

\n\tActionSprite.cpp代码:\n

\n

\n\tActionSprite::ActionSprite(void)\n

\n

\n\t{\n

\n

\n\t_price=0;\n

\n

\n\t_idleAction=NULL;\n

\n

\n\t_walkAction=NULL;\n

\n

\n\t_deathAction=NULL;\n

\n

\n\t}\n

\n

\n\tActionSprite::~ActionSprite(void)\n

\n

\n\t{\n

\n

\n\t//释放内存\n

\n

\n\tCC_SAFE_RELEASE_NULL(_idleAction);\n

\n

\n\tCC_SAFE_RELEASE_NULL(_deathAction);\n

\n

\n\tCC_SAFE_RELEASE_NULL(_walkAction);\n

\n

\n\t}\n

\n

\n\t//设置精灵为静止状态\n

\n

\n\tvoid ActionSprite::idle()\n

\n

\n\t{\n

\n

\n\tif (_actionState != kActionStateIdle)\n

\n

\n\t{\n

\n

\n\t//先停止所有动作\n

\n

\n\tthis->stopAllActions();\n

\n

\n\t//运行静止动作\n

\n

\n\tthis->runAction(_idleAction);\n

\n

\n\t_actionState=kActionStateIdle;\n

\n

\n\t}\n

\n

\n\t}\n

\n

\n\t//设置精灵为行走状态\n

\n

\n\tvoid ActionSprite::walk()\n

\n

\n\t{\n

\n

\n\tif (_actionState != kActionStateWalk)\n

\n

\n\t{\n

\n

\n\t//先停止所有动作\n

\n

\n\tthis->stopAllActions();\n

\n

\n\t//运行行走动作\n

\n

\n\tthis->runAction(_walkAction);\n

\n

\n\t_actionState=kActionStateWalk;\n

\n

\n\t}\n

\n

\n\t}\n

\n

\n\t//设置精灵为死亡状态\n

\n

\n\tvoid ActionSprite::death()\n

\n

\n\t{\n

\n

\n\t//先停止所有动作\n

\n

\n\tthis->stopAllActions();\n

\n

\n\tthis->runAction(_deathAction);\n

\n

\n\t_actionState=kActionStateDeath;\n

\n

\n\t}\n

\n

\n\t第二步:\n

\n

\n\t
\n

\nluobo22.jpg \n

\n\t素材准备,设计2张大叔不同动作的图片,交替显示模拟色狼行走动画,完成后把图片拷贝到Resources的iphonehd文件夹中,为了适应小分辨率的手机把这个2张图片按比例缩小一半并且拷贝到Resources的iphone文件夹中。\n

\n

\n\t第三步:\n

\n

\n\t新建DaShu.h、DaShu.cpp类(色狼大叔动画类),这个类继承自上面的ActionSprite负责游戏色狼大叔的动画效果显示。\n

\n

\n\tDaShu.h:\n

\n

\n\tclass DaShu : public ActionSprite\n

\n

\n\t{\n

\n

\n\tpublic:\n

\n

\n\tDaShu(void);\n

\n

\n\t~DaShu(void);\n

\n

\n\tCREATE_FUNC(DaShu);\n

\n

\n\t//初始化方法\n

\n

\n\tbool init();\n

\n

\n\t//设置光环,拥有光环的色狼生命值加倍\n

\n

\n\tvoid setHalo(bool halo);\n

\n

\n\t};\n

\n

\n\tDaShu.cpp:\n

\n

\n\tbool DaShu::init()\n

\n

\n\t{\n

\n

\n\tbool bRet=false;\n

\n

\n\tdo\n

\n

\n\t{\n

\n

\n\tCC_BREAK_IF(!ActionSprite::initWithFile("dashu_2.png"));\n

\n

\n\t//设置静止状态动作\n

\n

\n\tVector idleFrames(1);\n

\n

\n\tSpriteFrame *frame1=SpriteFrame::create("dashu_2.png", Rect(0, 0, 60, \n83));\n

\n

\n\tidleFrames.pushBack(frame1);\n

\n

\n\tAnimation \n*idleAnimation=Animation::createWithSpriteFrames(idleFrames,float(6.0 / \n12.0));\n

\n

\n\tthis->setIdleAction(CCRepeatForever::create(CCAnimate::create(idleAnimation)));\n

\n

\n\tint i=0;\n

\n

\n\t//设置行走状态动作\n

\n

\n\tVector walkFrames(2);\n

\n

\n\tfor (i=0;i<2;i++)\n

\n

\n\t{\n

\n

\n\tSpriteFrame \n*frame2=SpriteFrame::create(CCString::createWithFormat("dashu_%d.png", \ni+1)->getCString(), Rect(0, 0, 60, 83));\n

\n

\n\twalkFrames.pushBack(frame2);\n

\n

\n\t}\n

\n

\n\tAnimation \n*walkAnimation=Animation::createWithSpriteFrames(walkFrames,float(6.0 / \n12.0));\n

\n

\n\tthis->setWalkAction(CCRepeatForever::create(CCAnimate::create(walkAnimation)));\n

\n

\n\t//设置死亡状态动作\n

\n

\n\tVector deathFrames(1);\n

\n

\n\tSpriteFrame *frame3=SpriteFrame::create("dashu_2.png", Rect(0, 0, 60, \n83));\n

\n

\n\tdeathFrames.pushBack(frame3);\n

\n

\n\tAnimation \n*deathAnimation=Animation::createWithSpriteFrames(deathFrames,float(6.0 / \n12.0));\n

\n

\n\tthis->setDeathAction(Animate::create(deathAnimation));\n

\n

\n\t//设置攻击值\n

\n

\n\tthis->setDamage(1);\n

\n

\n\t//设置行走速度\n

\n

\n\tthis->setWalkSpeed(0.4f);\n

\n

\n\t//设置生命值\n

\n

\n\tthis->setHP(18);\n

\n

\n\t//设置金钱数\n

\n

\n\tthis->setMoney(1.0f/10.0f);\n

\n

\n\tbRet=true;\n

\n

\n\t} while (0);\n

\n

\n\treturn bRet;\n

\n

\n\t}\n

\n

\n\t//设置光环\n

\n

\n\tvoid DaShu::setHalo(bool halo)\n

\n

\n\t{\n

\n

\n\tif (halo)\n

\n

\n\t{\n

\n

\n\t//拥有光环后生命值加4倍\n

\n

\n\tfloat h=this->getHP()*4.0f;\n

\n

\n\tthis->setHP(h);\n

\n

\n\t}\n

\n

\n\t}\n

\n

\n\t第四步:新建Selang.h、Selang.cpp类(色狼类),这个类继承自CCNode游戏场景中的每一个色狼都有这个类产生,它肯定包含一个ActionSprite的色狼动画类,并且之前在MainScene.cpp的update方法中实现的色狼沿路行走代码也将转移到这个类的update方法中。\n

\n

\n\tSelang.h:\n

\n

\n\t#include "cocos2d.h"\n

\n

\n\t#include "Waypoint.h"\n

\n

\n\t#include "GameMediator.h"\n

\n

\n\t#include "ActionSprite.h"\n

\n

\n\tclass Selang : public cocos2d::CCNode\n

\n

\n\t{\n

\n

\n\tpublic:\n

\n

\n\tSelang(void);\n

\n

\n\t~Selang(void);\n

\n

\n\t//根据提供的spriteIndex实例化成不同的色狼\n

\n

\n\tstatic Selang* nodeWithType(int spriteIndex);\n

\n

\n\t//初始化方法\n

\n

\n\tbool initWithType(int spriteIndex,bool halo);\n

\n

\n\t//激活色狼\n

\n

\n\tvoid doActivate(float dt);\n

\n

\n\t//获取精灵Rect\n

\n

\n\tvirtual cocos2d::Rect getRect();\n

\n

\n\t//设置精灵是否激活\n

\n

\n\tvoid setActive(bool active);\n

\n

\n\t//是否死亡\n

\n

\n\tbool isDie;\n

\n

\n\tvoid update(float delta);\n

\n

\n\t//色狼精灵\n

\n

\n\tCC_SYNTHESIZE_RETAIN(ActionSprite*,_mySprite,MySprite);\n

\n

\n\tprivate:\n

\n

\n\t//精灵序号,为每种精灵编一个序号\n

\n

\n\tint _spriteIndex;\n

\n

\n\tGameMediator* m;\n

\n

\n\t//当前精灵的位置\n

\n

\n\tcocos2d::Point myPosition;\n

\n

\n\t//走路速度\n

\n

\n\tfloat walkingSpeed;\n

\n

\n\t//开始路点\n

\n

\n\tWaypoint *beginningWaypoint;\n

\n

\n\t//结束路点\n

\n

\n\tWaypoint *destinationWaypoint;\n

\n

\n\t//是否激活\n

\n

\n\tbool active;\n

\n

\n\t//色狼高度\n

\n

\n\tfloat myHeight;\n

\n

\n\t//两个点的碰撞检测\n

\n

\n\tbool collisionWithCircle(cocos2d::Point circlePoint,float \nradius,cocos2d::Point circlePointTwo, float radiusTwo);\n

\n

\n\t};\n

\n

\n\tSelang.cpp:\n

\n

\n\t//根据提供的spriteIndex实例化成不同的色狼\n

\n

\n\tSelang* Selang::nodeWithType(int spriteIndex)\n

\n

\n\t{\n

\n

\n\tSelang* pRet=new Selang();\n

\n

\n\tbool b=false;\n

\n

\n\tif (pRet && pRet->initWithType(spriteIndex,b))\n

\n

\n\t{\n

\n

\n\tpRet->autorelease();\n

\n

\n\treturn pRet;\n

\n

\n\t}\n

\n

\n\telse\n

\n

\n\t{\n

\n

\n\tdelete pRet;\n

\n

\n\tpRet=NULL;\n

\n

\n\treturn NULL;\n

\n

\n\t}\n

\n

\n\t}\n

\n

\n\t//初始化方法\n

\n

\n\tbool Selang::initWithType(int spriteIndex,bool halo)\n

\n

\n\t{\n

\n

\n\tbool bRet=false;\n

\n

\n\tdo\n

\n

\n\t{\n

\n

\n\t//色狼类型index\n

\n

\n\t_spriteIndex=spriteIndex;\n

\n

\n\t//\n

\n

\n\tm = GameMediator::sharedMediator();\n

\n

\n\t//不激活\n

\n

\n\tactive=false;\n

\n

\n\t//行走速度\n

\n

\n\twalkingSpeed=0.2f;\n

\n

\n\tActionSprite* sprite=NULL;\n

\n

\n\tif (spriteIndex==1)//如果类型是1初始化成大叔色狼\n

\n

\n\t{\n

\n

\n\tsprite=DaShu::create();\n

\n

\n\tsprite->setHalo(halo);\n

\n

\n\t//设置速度\n

\n

\n\twalkingSpeed=sprite->getWalkSpeed();\n

\n

\n\t}\n

\n

\n\tthis->setMySprite(sprite);\n

\n

\n\t//添加精灵到当前Selang中\n

\n

\n\tthis->addChild(_mySprite);\n

\n

\n\t//计算当前色狼精灵1/2高\n

\n

\n\tmyHeight=sprite->getTextureRect().size.height/2.0f;\n

\n

\n\t//获得路点集合中的最后一个点\n

\n

\n\tWaypoint *waypoint=(Waypoint*)m->getWayPoints().back();\n

\n

\n\t//设置为色狼出发点\n

\n

\n\tbeginningWaypoint=waypoint;\n

\n

\n\t//获取出发点的下个点为色狼目标点\n

\n

\n\tdestinationWaypoint=waypoint->getNextWaypoint();\n

\n

\n\t//获得出发点坐标\n

\n

\n\tPoint pos=waypoint->getMyPosition();\n

\n

\n\t//对坐标进行校正提供半个身位高度\n

\n

\n\tpos.add(Vec2(0,myHeight));\n

\n

\n\t//记录位置坐标\n

\n

\n\tmyPosition=pos;\n

\n

\n\t//设置精灵的初始坐标\n

\n

\n\t_mySprite->setPosition(pos);\n

\n

\n\t//设置初始不可见\n

\n

\n\tthis->setVisible(false);\n

\n

\n\t//把当前色狼添加到游戏的MainScene场景中显示\n

\n

\n\tm->getNowScene()->addChild(this);\n

\n

\n\t//启动定时器\n

\n

\n\tthis->scheduleUpdate();\n

\n

\n\tbRet=true;\n

\n

\n\t} while (0);\n

\n

\n\treturn bRet;\n

\n

\n\t}\n

\n

\n\tvoid Selang::doActivate(float dt)\n

\n

\n\t{\n

\n

\n\t//激活色狼\n

\n

\n\tactive=true;\n

\n

\n\t//设置色狼可见\n

\n

\n\tthis->setVisible(true);\n

\n

\n\t}\n

\n

\n\t//获取精灵Rect\n

\n

\n\tRect Selang::getRect()\n

\n

\n\t{\n

\n

\n\tRect rect =Rect(_mySprite->getPosition().x - \n_mySprite->getContentSize().width * 0.5f,\n

\n

\n\t_mySprite->getPosition().y - _mySprite->getContentSize().height* \n0.5f,\n

\n

\n\t_mySprite->getContentSize().width,\n

\n

\n\t_mySprite->getContentSize().height);\n

\n

\n\treturn rect;\n

\n

\n\t}\n

\n

\n\t//设置精灵是否激活\n

\n

\n\tvoid Selang::setActive(bool aactive)\n

\n

\n\t{\n

\n

\n\tactive=aactive;\n

\n

\n\tthis->setVisible(true);\n

\n

\n\t}\n

\n

\n\tvoid Selang::update(float delta)\n

\n

\n\t{\n

\n

\n\tif (!active)\n

\n

\n\t{\n

\n

\n\treturn;\n

\n

\n\t}\n

\n

\n\tPoint destinationPos=destinationWaypoint->getMyPosition();\n

\n

\n\t//提升色狼半个身位\n

\n

\n\tdestinationPos.add(Vec2(0,myHeight));\n

\n

\n\t//是否拐弯\n

\n

\n\tif (this->collisionWithCircle(myPosition,1,destinationPos,1))\n

\n

\n\t{\n

\n

\n\tif (destinationWaypoint->getNextWaypoint())\n

\n

\n\t{\n

\n

\n\t//设置新的出发点和目标点\n

\n

\n\tbeginningWaypoint=destinationWaypoint;\n

\n

\n\tdestinationWaypoint=destinationWaypoint->getNextWaypoint();\n

\n

\n\t}\n

\n

\n\t}\n

\n

\n\tPoint targetPoint=destinationWaypoint->getMyPosition();\n

\n

\n\t//提升色狼半个身位\n

\n

\n\ttargetPoint.add(Vec2(0,myHeight));\n

\n

\n\tfloat movementSpeed=walkingSpeed;\n

\n

\n\t//计算目标点的向量\n

\n

\n\tPoint \nnormalized=Point(targetPoint.x-myPosition.x,targetPoint.y-myPosition.y).getNormalized();\n

\n

\n\t//根据速度和向量分别计算x,y方式上的偏移值\n

\n

\n\tfloat ox=normalized.x * walkingSpeed;\n

\n

\n\tfloat oy=normalized.y *walkingSpeed;\n

\n

\n\t//更新色狼移动后的位置\n

\n

\n\tmyPosition = Point(myPosition.x + ox, myPosition.y +oy);\n

\n

\n\t_mySprite->setPosition(myPosition);\n

\n

\n\t}\n

\n

\n\t//两个点的碰撞检测\n

\n

\n\tbool Selang::collisionWithCircle(cocos2d::Point circlePoint,float \nradius,cocos2d::Point circlePointTwo, float radiusTwo)\n

\n

\n\t{\n

\n

\n\tfloat xdif = circlePoint.x - circlePointTwo.x;\n

\n

\n\tfloat ydif = circlePoint.y - circlePointTwo.y;\n

\n

\n\t//计算两点间的距离\n

\n

\n\tfloat distance = sqrt(xdif * xdif + ydif * ydif);\n

\n

\n\tif(distance <= radius + radiusTwo)\n

\n

\n\t{\n

\n

\n\treturn true;\n

\n

\n\t}\n

\n

\n\treturn false;\n

\n

\n\t}\n

\n

\n\t第五步:\n

\n

\n\t如果运行一下那么上面的代码中Waypoint \n*waypoint=(Waypoint*)m->getWayPoints().back();这行应该会报错,因为GameMediator中没有提供getWayPoints()这个方法,所以我们要对GameMediator类进行修改加上这个方法,代码如下:\n

\n

\n\tvoid GameMediator::setWayPoints(cocos2d::Vector wayPoints)\n

\n

\n\t{\n

\n

\n\t_wayPoints=wayPoints;\n

\n

\n\t}\n

\n

\n\tVector GameMediator::getWayPoints()\n

\n

\n\t{\n

\n

\n\treturn _wayPoints;\n

\n

\n\t}\n

\n

\n\t第六步:\n

\n

\n\t在MainScene的init方法中把路点集合通过setWayPoints方法赋值给GameMediator,这样在Selang.cpp中就可以取到路点集合了:\n

\n

\n\t……\n

\n

\n\tthis->wayPositions.pushBack(waypoint12);\n

\n

\n\tGameMediator::sharedMediator()->setWayPoints(wayPositions);\n

\n

\n\t……\n

\n

\n\t第七步:\n

\n

\n\t测试这个Selang类具体效果,先给MainScene添加一个void startGame(float \ndelta)的方法,用这个方法开始游戏。\n

\n

\n\t//开始游戏\n

\n

\n\tvoid MainScene::startGame(float delta)\n

\n

\n\t{\n

\n

\n\t//实例化一个大叔类型的色狼\n

\n

\n\tSelang* selang=Selang::nodeWithType(1);\n

\n

\n\t//激活这个色狼\n

\n

\n\tselang->setActive(true);\n

\n

\n\t//设置色狼动画为行走动画\n

\n

\n\tselang->getMySprite()->walk();\n

\n

\n\t//取消定时器方法,保证startGame只执行一次\n

\n

\n\tthis->unschedule(schedule_selector(MainScene::startGame));\n

\n

\n\t}\n

\n

\n\t第八步:\n

\n

\n\t我们在MainScene的init方法末尾处调用这个startGame的方法:\n

\n

\n\t1\n

\n

\n\t2//0.5秒后调用startGame方法\n

\n

\n\tthis->schedule(schedule_selector(MainScene::startGame),0.5f);\n

\n

\n\t到这里,把第一篇中临时添加色狼的代码删除,就可以运行测试游戏了,会看到色狼大叔一扭一扭的沿着道路靠近女主角。效果非常好,我们成功的对色狼的代码进行了重构。\n

\n

\n\t女主角代码重构\n

\n

\n\t第一步:\n

\n

\n\t
\n

\nluobo3.jpg \n

\n\t素材准备,设计4张萝莉不同动作的图片,交替显示模拟萝莉卖萌动画,完成后把图片拷贝到Resources的iphonehd文件夹中,为了适应小分辨率的手机把这个4张图片按比例缩小一半并且拷贝到Resources的iphone文件夹中。\n

\n

\n\t第二步:\n

\n

\n\t新建Luoli.h、Luoli.cpp类(女主角动画类),这个类继承自上面的ActionSprite负责游戏女主角的动画效果显示。\n

\n

\n\tLuoli.h:\n

\n

\n\tclass Luoli : public ActionSprite\n

\n

\n\t{\n

\n

\n\tpublic:\n

\n

\n\tLuoli(void);\n

\n

\n\t~Luoli(void);\n

\n

\n\tCREATE_FUNC(Luoli);\n

\n

\n\tbool init();\n

\n

\n\t};\n

\n

\n\tLuoli.cpp:\n

\n

\n\tbool Luoli::init()\n

\n

\n\t{\n

\n

\n\tbool bRet=false;\n

\n

\n\tdo\n

\n

\n\t{\n

\n

\n\tCC_BREAK_IF(!ActionSprite::initWithFile("girl1_1.png"));\n

\n

\n\t//设置静止状态动作\n

\n

\n\tVector idleFrames(1);\n

\n

\n\tSpriteFrame *frame1=SpriteFrame::create("girl1_1.png", Rect(0, 0, 100, \n126));\n

\n

\n\tidleFrames.pushBack(frame1);\n

\n

\n\tAnimation \n*idleAnimation=Animation::createWithSpriteFrames(idleFrames,float(6.0 / \n12.0));\n

\n

\n\tthis->setIdleAction(CCRepeatForever::create(CCAnimate::create(idleAnimation)));\n

\n

\n\t//设置行走状态动作\n

\n

\n\tint i;\n

\n

\n\tVector walkFrames(4);\n

\n

\n\tfor (i=0;i<4;i++)\n

\n

\n\t{\n

\n

\n\tSpriteFrame \n*frame1=SpriteFrame::create(CCString::createWithFormat("girl1_%d.png", \ni+1)->getCString(), Rect(0, 0, 100, 126));\n

\n

\n\twalkFrames.pushBack(frame1);\n

\n

\n\t}\n

\n

\n\tAnimation \n*walkAnimation=Animation::createWithSpriteFrames(walkFrames,float(6.0 / \n12.0));\n

\n

\n\tthis->setWalkAction(CCRepeatForever::create(CCAnimate::create(walkAnimation)));\n

\n

\n\tbRet=true;\n

\n

\n\t} while (0);\n

\n

\n\treturn bRet;\n

\n

\n\t}\n

\n

\n\t第三步:\n

\n

\n\t新建Girl.h、Girl.cpp类(女孩类),这个类继承自CCNode游戏场景中的女主角由这个类产生,它肯定包含一个ActionSprite的萝莉动画类。\n

\n

\n\tGirl.h:\n

\n

\n\tclass Girl : public cocos2d::CCNode\n

\n

\n\t{\n

\n

\n\tpublic:\n

\n

\n\tGirl(void);\n

\n

\n\t~Girl(void);\n

\n

\n\t//根据提供的type实例化成不同的女主角\n

\n

\n\tstatic Girl* nodeWithType(int type);\n

\n

\n\t//初始化方法\n

\n

\n\tbool initWithLocation(cocos2d::Point location);\n

\n

\n\t//获取精灵Rect\n

\n

\n\tcocos2d::Rect getRect();\n

\n

\n\tprivate:\n

\n

\n\t//萝莉精灵\n

\n

\n\tCC_SYNTHESIZE_RETAIN(ActionSprite*,_mySprite,MySprite);\n

\n

\n\t};\n

\n

\n\tGirl.cpp:\n

\n

\n\t//根据提供的type实例化成不同的女主角\n

\n

\n\tGirl* Girl::nodeWithType(int type)\n

\n

\n\t{\n

\n

\n\tGirl* pRet=new Girl();\n

\n

\n\tGameMediator* m = GameMediator::sharedMediator();\n

\n

\n\tWaypoint *waypoint=(Waypoint*)m->getWayPoints().front();\n

\n

\n\tPoint pos=waypoint->getMyPosition();\n

\n

\n\tif (pRet && pRet->initWithLocation(pos))\n

\n

\n\t{\n

\n

\n\tpRet->autorelease();\n

\n

\n\treturn pRet;\n

\n

\n\t}\n

\n

\n\telse\n

\n

\n\t{\n

\n

\n\tdelete pRet;\n

\n

\n\tpRet=NULL;\n

\n

\n\treturn false;\n

\n

\n\t}\n

\n

\n\t}\n

\n

\n\t//初始化方法\n

\n

\n\tbool Girl::initWithLocation(cocos2d::Point location)\n

\n

\n\t{\n

\n

\n\tbool bRet=false;\n

\n

\n\tdo\n

\n

\n\t{\n

\n

\n\t//实例化一个萝莉\n

\n

\n\tActionSprite *sprite= Luoli::create();\n

\n

\n\tthis->setMySprite(sprite);\n

\n

\n\t//添加精灵到当前Gril中\n

\n

\n\tthis->addChild(sprite);\n

\n

\n\t//设置为静止\n

\n

\n\tsprite->idle();\n

\n

\n\t//计算当前萝莉精灵1/2高\n

\n

\n\tint myHeight=sprite->getTextureRect().size.height/2.0f;\n

\n

\n\t//对坐标进行校正提供半个身位高度\n

\n

\n\tlocation.add(Vec2(0,myHeight));\n

\n

\n\tsprite->setPosition(location);\n

\n

\n\t//把当前女主角添加到游戏的MainScene场景中显示\n

\n

\n\tGameMediator* m = GameMediator::sharedMediator();\n

\n

\n\tm->getNowScene()->addChild(this,10000);\n

\n

\n\tbRet=true;\n

\n

\n\t}\n

\n

\n\twhile (0);\n

\n

\n\treturn bRet;\n

\n

\n\t}\n

\n

\n\tRect Girl::getRect()\n

\n

\n\t{\n

\n

\n\tRect rect = Rect(_mySprite->getPosition().x - \n_mySprite->getContentSize().width * 0.5f,\n

\n

\n\t_mySprite->getPosition().y - _mySprite->getContentSize().height* \n0.5f+20,\n

\n

\n\t_mySprite->getContentSize().width,\n

\n

\n\t_mySprite->getContentSize().height-40);\n

\n

\n\treturn rect;\n

\n

\n\t}\n

\n

\n\t第四步:\n

\n

\n\t在MainScene的 startGame(float delta)的方法中加上初始化女主角的代码。\n

\n

\n\t1\n

\n

\n\t2\n

\n

\n\t3\n

\n

\n\t4\n

\n

\n\t5\n

\n

\n\t6\n

\n

\n\t7……\n

\n

\n\t//初始一个女主角\n

\n

\n\tGirl* girl=Girl::nodeWithType(1);\n

\n

\n\t//设置女主角动画为卖萌动画\n

\n

\n\tgirl->getMySprite()->walk();\n

\n

\n\t//取消定时器方法,保证startGame只执行一次\n

\n

\n\tthis->unschedule(schedule_selector(MainScene::startGame));\n

\n

\n\t到这里,把第一篇中临时添加女主角的代码删除,就可以运行测试游戏了,本篇的任务到此为止,本篇完成后Android真机的运行效果如下:\n

\n

\n\t
\n

\n020903392498779.gif \n

\n\t结束语:\n

\n

\n\t这个塔防游戏系列已经写了3篇了,到现在为止还没有出现炮塔,说好的炮塔呢?请期待下一篇炮塔姑娘的保护神。\n

\n

\n\t
\n

\n
\n

作者信息

boke

boke

共发布了 3994 篇文章