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

      首先来建一个类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. 【HOJ1356】【Miller_rabin素性测试】Prime Judge

    Given a positive integer, your job is writing a program to determine whether it is a prime number or ...

  2. JavaScript 获取当月天数

    getDate() 方法可返回月份的某一天.取值范围是1~31 如果是0的话,就返回最后一天.这样就能取得当月的天数了 比如获取16年2月份的天数 var day = new Date(2016,2, ...

  3. EasyUI 1.3之前DataGrid中动态选中、获取Checkbox

    这几天做项目,由于项目中用到的EasyUI版本过低,不能使用自带操作DataGrid中CheckBox的方法. 所以自己写了一个临时方案: 根据ID集合选中所属行的CheckBox: data={1, ...

  4. ComboBoxEdit

    1.  如何使其不可编辑 TextEditStyle 设置为:DisableTextEditor 2.  如何设置鼠标为手形 Cursor 设置为:Hand

  5. Html 中select标签的边框与右侧倒三角的去除

    首先是边框的去除:可以设置属性border:none;或border:0px; 不过这还是有一个bug,不同浏览器会在选中select标签时,加上一个边框: 之后是右侧倒三角的去除:设置属性 appe ...

  6. javascript实现ajax

    什么是 ajax ajax 即“Asynchronous JavaScript and XML”(异步 JavaScript 和 XML),也就是无刷新数据读取. http 请求 首先需要了解 htt ...

  7. 设置session的生命周期(php)

    PHP中,Session变量保存在服务器端(默认以文件格式保存),而Session ID以cookie形式保存在客户端. 销毁session的方法有2种 第一种是通过程序 session_destor ...

  8. curl http_code状态码 含义

    curl爬取过程中,会返回一个http_code,下面是他们的意义信息 $http_code["]="Unable to access"; $http_code[&quo ...

  9. PHP面向对象(OOP):克隆对象__clone()方法

    有的时候我们需要在一个项目里面,使用两个或多个一样的对象,如果你使用“new”关键字重新创建对象的话,再赋值上相同的属性,这样做比较烦琐而且也容易出错,所以要根据一个对象完全克隆出一个一模一样的对象, ...

  10. Python中几种数据结构的整理,列表、字典、元组、集合

    列表:shoplist = ['apple', 'mango', 'carrot', 'banana']字典:di = {'a':123,'b':'something'}集合:jihe = {'app ...