大家早上好,趁着阳光美好的时候,我打算写下博客;今天要说的是僵尸的产生了,这块和太阳因子的产生比较相似,大体上的区别在于僵尸的基类这块;我在考虑是详细的写还是大体的写,本着对自己作业的态度和对编程的负责,我想还是一点一点的写出来吧;虽然比较长!(我们让帽子僵尸在地图的右边定时产生,以一定速度运动屋子的前面然后销毁它);
首先来建一个类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();
看下效果图吧:感觉还不错:
分享到:
相关推荐
斗地主,五子棋,小海豹,割绳子,数字游戏,俄罗斯方块,中国象棋,植物大战僵尸,按钮特效,心形进度条,赛车游戏,预测球的轨迹,愤怒的小鸟,ClippingNode,刀光效果,SLG,魂斗罗,天天过马路,橡皮擦效果,流体test,2d 光线,愤怒...
五子棋,小海豹,割绳子,数字游戏,俄罗斯方块,中国象棋,植物大战僵尸,按钮特效,心形进度条,赛车游戏,预测球的轨迹,愤怒的小鸟,ClippingNode,刀光效果,SLG,魂斗罗,天天过马路,橡皮擦效果,流体test,2d 光线,愤怒的小鸟2,...
1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合...
Android植物大战僵尸源码 cocos-2d [注:本内容来自网络,在此分享仅为帮助有需要的网友,如果侵犯了您的权利,麻烦联系我,我会第一时间删除,谢谢您。]
使用cocos2d游戏引擎开发的android版植物大战僵尸
cocos2d开发,植物大战僵尸(iphone版demo源码),仅做学习交流!!
我是用cocos2d-x3.17版本开发的植物大战僵尸,实现了部分功能,暂时只能种植2种植物,能出2种僵尸,他们之间的血量变化,以及僵尸动作之间的变化都实现好了。文件里我的开发过程,代码有详细注释。
自己写了一个简单的小程序,其中有不足之处,望谅解,本程序只供对cocos2d-x游戏开发者学习使用;开发环境xcode4.5 coco2d-x
游戏引擎基于版本 Cocos2d-X v3.16游戏全部使用c++开发如果你刚刚接触Cocos2d-X 这绝对是个不错的入门项目如果是你C++初学者或者其他语言转游戏开发,这个项目没有复杂的语法绝对是一个不错的学习项目。如果你是游戏...
植物大战僵尸游戏,会不断的更新。 植物大战僵尸游戏 Cocos2d_android介绍 Cocos2d是一个大家庭,包括Cocos2d-iphone,Cocos2d-x,Cocos2d-javascript等。而在国内,Cocos2d-x则相对领先。在中国的2D手机游戏开发中,...
多款HTML5 游戏源代码特辑,包括了捕鱼达人、植物僵尸、Solarmax、俄罗斯方块、扫雷、切水果、超级玛丽、三国杀、驴子跳、太空战舰、坦克大战等
使用c++基于Cocos2dx V3.16 开发的植物大战僵尸游戏。游戏引擎基于版本 Cocos2d-X v3.16。游戏全部使用c++开发
基于Cocos2dx V3.16开发的植物大战僵尸游戏
重要 这个游戏模仿了原版植物大战僵尸。不要用于商业用途,否则后果自负 官网: 项目说明 此游戏全部由本人自己制作完成。游戏具有可玩性(目前有104个关卡),今后会不定时更新(不断完善)。 该游戏相较于原版的...
愤怒的小鸟 贪吃蛇 超级玛丽 切水果等...3D俄罗斯方块 Cocos2d-html5-v2.0 ...lufylegend-1.5.0 切水果 坦克 塞车游戏 太阳系 德州21点扑克 ...植物大战僵尸 碰撞球 脚本课程10 贪吃蛇 超级玛丽 飞机游戏 驴子跳游戏源码 魔塔
毛球iOS 休闲无塔防御游戏,像植物大战僵尸一样以猫和狗为特色。 请注意,这个游戏是 3 年前开发的,此后一直没有更新,所以虽然它可以编译和运行,但屏幕看起来很糟糕,因为我们使用的 Cocos2D 的代码和版本已经...
我的其他资源都是免费的,是对于c语言初学者和想初学游戏者的帮助比较大的,其中有单片机,ARM,数据结构,window编程,MFC编程,cocos2dx,unity3d自己编写的小游戏。...目前连载cocos植物大战僵尸教程中。