大家早上好,趁着阳光美好的时候,我打算写下博客;今天要说的是僵尸的产生了,这块和太阳因子的产生比较相似,大体上的区别在于僵尸的基类这块;我在考虑是详细的写还是大体的写,本着对自己作业的态度和对编程的负责,我想还是一点一点的写出来吧;虽然比较长!(我们让帽子僵尸在地图的右边定时产生,以一定速度运动屋子的前面然后销毁它);

      首先来建一个类JsLayer继承CCLayer;JsLayer.h中,进行如下声明:

#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"
#include "cocos2d.h"
#include "RedCatJsSprite.h"
class JsLayer :public cocos2d::CCLayer
{
public:
JsLayer(void);
~JsLayer(void);
virtual bool init();
CREATE_FUNC(JsLayer);
cocos2d::CCSpriteFrameCache* _redCatCache;
cocos2d::CCSpriteBatchNode* _redCatBatchNode; RedCatJsSprite* _redCatSprite;
void initRedCatSprite(float dt);//初始化红帽子僵尸
void redCatMoveWay();//给定红帽子僵尸的运动路线
void removeRedCat(CCNode* pSend);//删除红帽子僵尸
};

在JsLayer.cpp中;
这里我们沿用太阳因子时的思路,先不去处理这些空函数体!

#include "JsLayer.h"
USING_NS_CC; JsLayer::JsLayer(void)
{
this->_redCatCache = CCSpriteFrameCache::sharedSpriteFrameCache();
this->_redCatCache->addSpriteFramesWithFile("redcat.plist");
this->_redCatCache->retain(); this->_redCatBatchNode = CCSpriteBatchNode::create("redcat.pvr.ccz");
this->_redCatCache->retain();
this->addChild(this->_redCatBatchNode);
this->_redCatSprite=NULL;
}
JsLayer::~JsLayer(void)
{
this->_redCatBatchNode->release();
this->_redCatCache->release();
} bool JsLayer::init()
{
if(!CCLayer::init())
{
return false;
} return true;
}
void JsLayer::initRedCatSprite(float dt)
{ }
//定义红帽子僵尸的运动路线
void JsLayer::redCatMoveWay()
{ }
void JsLayer::removeRedCat(CCNode* pSend)
{ }

下面我们来写下所有僵尸的基类JsAstributeSprite

在JsAstributeSprite.h中:(暂时我还没想好该往僵尸基类里面写些什么,可能会在后续的开发中想到什么,到时候在整合进去,这                                                      个基类肯定是不能少的)

#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\sprite_nodes\ccsprite.h"
#include "cocos2d.h"
class JsAstributeSprite :public cocos2d::CCSprite
{
public:
JsAstributeSprite(void);
~JsAstributeSprite(void); int _hp ;//代表僵尸的生命值;
};

JsAstribute.cpp中:先不做任何处理

#include "JsAstributeSprite.h"
USING_NS_CC; JsAstributeSprite::JsAstributeSprite(void)
{
} JsAstributeSprite::~JsAstributeSprite(void)
{
}

下面我们要写具体的是红帽子僵尸了,虽然它的帽子是橘黄色的,呵呵就这么叫了;这个精灵类继承我们的JsAstributeSprite类;

RedCatSprite.h中:

#pragma once
#include "jsastributesprite.h"
#include "cocos2d.h"
class RedCatJsSprite :public JsAstributeSprite
{
public:
RedCatJsSprite(void);
~RedCatJsSprite(void);
virtual bool init();
CREATE_FUNC(RedCatJsSprite); };


RedCatSprite.cpp中:(给它一个基础动画)

#include "RedCatJsSprite.h"

USING_NS_CC;
RedCatJsSprite::RedCatJsSprite(void)
{
this->_hp = 60;//帽子僵尸的生命值为60;
} RedCatJsSprite::~RedCatJsSprite(void)
{
} bool RedCatJsSprite::init()
{
if(!JsAstributeSprite::initWithSpriteFrameName("ConeheadZombie_1.png"))
{
return false;
}
int i;
CCArray* redCatArray = CCArray::create();//创建一个数组用于存放帽子僵尸的帧
redCatArray->retain();
//下面是帽子僵尸的动画实现过程
for(i=1;i<21;i++)
{
CCSpriteFrame* redCatFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("ConeheadZombie_%d.png",i)->getCString());
redCatArray->addObject(redCatFrames);
}
CCAnimation* redCatAnimation=CCAnimation::createWithSpriteFrames(redCatArray,0.2f);
this->runAction(CCRepeatForever::create(CCAnimate::create(redCatAnimation)));
return true; return true;
}

好这三个类的基本框架是搭建好了,下面呢,我们就要来实现具体的功能了;我们先把RedCatSprite类加到我们的JsLayer类当中去。这些要声明的内容在上面的JsLayer.h中已经做好了,我们现在主要做的是在JsLayer.cpp中实现功能;

在JsLayer.cpp中;我们想每隔一段时间产生一个僵尸;所以在void JsLayer::initRedCatSprite(float dt)中做以下处理:

//初始化橘黄帽子僵尸
void JsLayer::initRedCatSprite(float dt)
{
CCSize winSize =CCDirector::sharedDirector()->getWinSize();
this->_redCatSprite =RedCatJsSprite::create();
this->_redCatBatchNode->addChild(this->_redCatSprite);
this->_redCatSprite->setPosition(ccp(winSize.width* 8/9,winSize.height/2));
this->redCatMoveWay();
}

下面我们来规定帽子僵尸的运动路线:所以在void JsLayer::redCatMoveWay()中:

//定义红帽子僵尸的运动路线
void JsLayer::redCatMoveWay()
{
//这个函数代表帽子僵尸的具体运动过程
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCFiniteTimeAction* redCatMove = CCMoveTo::create(75.0f,ccp(winSize.width/9,this->_redCatSprite->getPosition().y)); this->_redCatSprite->runAction(CCSequence::create(redCatMove,CCCallFuncN::create(this,callfuncN_selector(JsLayer::removeRedCat)),NULL));
}

在回调函数void JsLayer::removeRedCat(CCNode* pSend)中删除帽子僵尸:

void JsLayer::removeRedCat(CCNode* pSend)
{
CCSprite* sprite = (CCSprite*) pSend;
this->_redCatBatchNode->removeChild(sprite,true);//从精灵批处理节点中回收红帽子僵尸
}

在bool JsLayer::init()中调用:

bool JsLayer::init()
{
if(!CCLayer::init())
{
return false;
} schedule(schedule_selector(JsLayer::initRedCatSprite),50.0f);
return true;
}

最后要做的工作,就是把我们的JsLayer层加到我们的主游戏层GameLayer中去:
在GameLayer.h中做一下声明:

#include "JsLayer.h"
JsLayer* _jsLayer;
void initJsLayer();

在GameLayer.cpp的初始化函数中加入一句:

this->_jsLayer=NULL;

在void GameLayer::initJsLayer()中,进行初始化:

//初始化红帽子僵尸层
void GameLayer::initJsLayer()
{
this->_jsLayer =JsLayer::create();
this->addChild(this->_jsLayer);
}

最后在bool GameLayer::init()方法中调用:

this->initJsLayer();

看下效果图吧:感觉还不错:

[置顶] cocos2d-x 植物大战僵尸(4) 帽子僵尸的产生的更多相关文章

  1. [置顶] cocos2d-x 植物大战僵尸(13)类似酷跑的【同一角色不同动画间的切换的实现】

          有几天没和大家分享博客了,原因很简单,就是我在运行第12章所写的代码时:(开始一切正常,不过没多久就出现了内存泄露!.可能求成心切吧,当时没多加考虑就把代码发上去了.我在此对看过第12章得 ...

  2. 在UWP中页面滑动导航栏置顶

    最近在研究掌上英雄联盟,主要是用来给自己看新闻,顺便copy个界面改一下段位装装逼,可是在我copy的时候发现这个东西 当你滑动到一定距离的时候导航栏会置顶不动,这个特性在微博和淘宝都有,我看了@ms ...

  3. WinFrom窗体始终置顶

    调用WindowsAPI使窗体始终保持置顶效果,不被其他窗体遮盖: [DllImport("user32.dll", CharSet = CharSet.Auto)] privat ...

  4. winform窗体置顶

    winform窗体置顶 金刚 winform 置顶 今天做了一个winform小工具.需要设置置顶功能. 网上找了下,发现百度真的很垃圾... 还是必应靠谱些. 找到一个可以链接. https://s ...

  5. 自定义置顶TOP按钮

    简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...

  6. ahk之路:利用ahk在window7下实现窗口置顶

    操作系统:win7 64位 ahk版本:autohotkey_L1.1.24.03 今天安装了AutoHotkey_1.1.24.03.SciTE.PuloversMacroCreator,重新开始我 ...

  7. Qt中让Qwidget置顶的方法

    一般来是说窗体置顶和取消只要        setWindowFlags(Qt::WindowStaysOnTopHint);        setWindowFlags(Qt::Widget); 要 ...

  8. js之滚动置顶效果

    0.js获取高度 ? 1 2 3 4 5 6 document.all   // 只有ie认识   document.body.clientHeight              // 文档的高,屏幕 ...

  9. Javascript笔记----实现Page页面右下角置顶按钮.

    从用博客开始,发现博客园中很多博友的博客中在Page右下角都有个图标,不论屏幕怎么拉伸,都始终停留在右下角.点击后页面置顶.后面想想写一个Demo来实现这种效果吧. 一. 图标右下角固定. 1.SS ...

随机推荐

  1. cas sso单点登录系列8_抛弃Https让Cas以Http协议提供单点登录服务

    转:http://blog.csdn.net/ycyk_168/article/details/18668951 本文环境: 1.apache-tomcat-7.0.50-windows-x86 2. ...

  2. STL库list::sort()实现深度解析

    原创,转载请注明出处:STL库list::sort()实现深度解析 list模板的定义以及一些基本成员函数的实现这里我就不赘述了,还不清楚的同学可以到网上查找相关资料或者直接查看侯捷翻译的<ST ...

  3. qsort 函数用法

    用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 各参数: 1 待排 ...

  4. Codeforces 527E Data Center Drama(欧拉回路)

    题意: 给定一个无向图连通图,把这个的无向边变成有向边,并添加最少的有向边使这个图每个结点的出度为偶数. Solution: 题目很长,并且很多条件说的不太直接,确实不太好懂. 首先先看得到的无向图, ...

  5. 一个IP支持多个网站实例(Apache2、Ubuntu相关)

    http://www.blogjava.net/Andyluo/archive/2009/08/24/21821.html http://blog.csdn.net/zltianhen/article ...

  6. 在easyui dialog的子页面内如何关闭弹窗

    因项目需要在dialog中添加滚动条,所以就在div中加了iframe: <div id="applyRefundDialog" style="display:no ...

  7. 9张思维导图学习Javascript

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  8. [jQuery编程挑战]006 生成一个倒计时效果

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  9. Win8.1 64bit安装Genymotion模拟器

    其实安装并不复杂,只要环境正常,此事并不难.但估计最坏的情况都被我撞上了,才折腾了差不多一天的 那我有哪些环境不正常呢? 破解了系统主题 Device Install Service服务未启动 下面来 ...

  10. eval函数:分号的应用

    eval("echo'hello world';"); ("参数;") eval函数把参数当做php代码来执行,参数后要有分号,最后还要另加一个分号 相当于: ...