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

      首先来建一个类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. IIS部署.NET项目的有关事项_2015.07.02

    今天在做项目中的关于发送邮件的一些功能.在部署服务的时候遇到了一些奇葩的问题,基本上是和IIS有关的问题. 首先,项目是基于.NET Framework4.0 版本的,由于本人用的是一台新配置好的机器 ...

  2. java设计模式——单例(Singleton)模式

    在某些场景,你需要找到一个承担职责的对象,并且这个对象是他所属类的唯一实例.此时可以使用单例模式. 单例模式的意图是为了确保一个类有且仅有一个实例,并为他提供一个全局的访问点.创建一个担当独一无二角色 ...

  3. window下配置SSH连接GitHub、GitHub配置ssh key(转)

    转自:http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html 此经验分两部分: 第一部分介绍:在windows下通过msysGit ...

  4. MySQL数据库服务器 主从配置

    A B 为两台 MySQL 服务器,均开启二进制日志,数据库版本 MySQL 5.5 一.服务器参数   [A 服务器 192.168.1.100] server-id = 1 binlog-do-d ...

  5. log4j 1.2配置(转载)

    转载自:http://www.blogjava.net/kit-soft/archive/2009/08/28/292977.html 第一步:加入log4j-1.2.8.jar到lib下. 第二步: ...

  6. PHP 开启 ssh2

    首先,为PHP安装SSH2扩展需要两个软件包, libssh2和ssh2(php pecl拓展). 两者的最新版本分别为libssh2-1.5.0.tar.gz和ssh2-0.12.tgz,下载地址分 ...

  7. DEDECMS重要文件

    DEDECMS 重要文件dedecms/include/common.inc.php全局变量文件dedecms/include/extend.func.php自定义函数文件

  8. STM32学习笔记——SPI串行通讯(向原子哥学习)

    一.SPI  简介 SPI是 Serial Peripheral interface 的缩写,就是串行外围设备接口.SPI 接口主要应用在  EEPROM, FLASH,实时时钟,AD 转换器,还有数 ...

  9. iOS开源项目

    在结束了GitHub平台上“最受欢迎的Android开源项目”系列盘点之后,我们正式迎来了“GitHub上最受欢迎的iOS开源项目”系列盘点.今天,我们将介绍20个在GitHub上非常受开发者欢迎的i ...

  10. 持续集成之戏说Check-in Dance

    尽管Thoughtworks的首席科学家Martion folwer 为“持续集成 ” 下了定义,但由于自身背景与经历的不同,每个人对其都有不同的理解.从狭义上讲,持续集成可以认为是一种基于某种或者某 ...