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

      首先来建一个类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. SGU 106.Index of super-prime

    时间限制:0.25s 空间限制:4M 题目大意:                 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...

  2. mysql更新密码为空

    1.进入命令行 mysql -u root -p 'oldpassword'; 2 修改root用户的密码:mysql> update mysql.user set password=PASSW ...

  3. 执行*.sh脚本时提示Permission denied

    使用chmod修改.sh的权限 chmod u+x *.sh 再次执行

  4. js常用效果

    //创建元素 var txt1="<p style='color:red'>我是由HTML创建的</p>"; // 以 HTML 创建新元素 var txt ...

  5. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

  6. Delphi中停靠技术的实现

    随着软件技术的不断进步,软件界面也越来越美观,操作也越来越方便.综观市面上比较专业的各种软件,我们会发现大部分都提供窗体停靠的功能,特别象工具软件,基本上都或多或少有停靠功能.自然,Delphi也支持 ...

  7. vs 2015 菜单重复的问题解决方法

    打开 “运行” 输入 D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe /resetuserdata ...

  8. 3ds max 2016 新功能

    3ds max 2016 新功能_MCG节点编辑器 这是介绍视频下载: http://pan.baidu.com/s/1gds4wqJ

  9. [151225] Python3 实现最大堆、堆排序,解决TopK问题

    参考资料: 1.算法导论,第6章,堆排序 堆排序学习笔记及堆排序算法的python实现 - 51CTO博客 堆排序 Heap Sort - cnblogs 小根堆实现优先队列:Python实现 -cn ...

  10. Computational Methods in Bayesian Analysis

    Computational Methods in Bayesian Analysis Computational Methods in Bayesian Analysis  [Markov chain ...