`
369540808
  • 浏览: 196587 次
文章分类
社区版块
存档分类
最新评论

cocos2d-x 植物大战僵尸(3)随机太阳因子及利用C++多态实现金币递增

 
阅读更多

大家早上好,我就接着,昨天的写了;

玩过植物大战僵尸的朋友都应该知道太阳光照射产生金币的过程,我把它叫做太阳因子,我就这样命名了,怎么滴!

好吧,太阳因子会在地图上方随机位置产生;我设计它并要求它完成两个直线运动实现系统的自动拾取; 说到自动拾取我就想到我那次玩植物大战僵尸的时候,突然冒出一句:是否要系统自动帮您拾取太阳光照,当时感觉这真他妈搞科技,好人性化;呵呵;其实不过是简单的流程控制罢了!

好,下面我们来建一个层:(首先来说下这个层得作用:产生并实现太阳因子的各个功能,包括太阳因子的线性运动和回收);

SunCellLayer.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 "SunCellSprite.h"

class SunCellLayer :public cocos2d::CCLayer
{
public:
	SunCellLayer(void);
	~SunCellLayer(void);

	CREATE_FUNC(SunCellLayer);
	bool virtual init();
	SunCellSprite* _sunCellSprite; //太阳因子精灵
	void initSunCell(float dt);//定时产生太阳因子
    cocos2d::CCSpriteBatchNode* _sunBatchNode;//太阳因子的精灵批处理节点
	cocos2d::CCSpriteFrameCache* _sunCache;//太阳因子的精灵框帧缓存
	void SunCellMoveWay();//声明太阳因子的运动路线;
	void removeSunCell(CCNode* pSend);//太阳因子的回收
};
在.cpp中我们先不去写上面方法的具体实现过程;

#include "SunCellLayer.h"
#include "GameLayer.h"
USING_NS_CC;

SunCellLayer::SunCellLayer(void)
{
	this->_sunCache =CCSpriteFrameCache::sharedSpriteFrameCache();//创建精良框帧缓存
	this->_sunCache->addSpriteFramesWithFile("suncell.plist");//包含精灵碎图的文件
	this->_sunCache->retain(); //保存缓存资源

	this->_sunBatchNode=CCSpriteBatchNode::create("suncell.pvr.ccz");//创建太阳因子的批处理节点;
	this->_sunBatchNode->retain();
	this->addChild(this->_sunBatchNode);//把精灵批处理节点加到太阳因子层

	this->_sunCellSprite =NULL;

}

SunCellLayer::~SunCellLayer(void)
{
	this->_sunCache->release();
	this->_sunBatchNode->release();
}

bool SunCellLayer::init()
{
	if(!CCLayer::init())
	{
		return false;
	}

	return true;
}

void SunCellLayer::initSunCell(float dt)
{
     
}

void SunCellLayer::SunCellMoveWay()
{
	
}

void SunCellLayer::removeSunCell(CCNode* pSend)
{
	
}

这是SunCellLayer类的基本结构;

下面我们要创建一个太阳因子的精灵类;然后把它加到太阳因子层里面去

SunCellSprite.h

#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\sprite_nodes\ccsprite.h"
#include "cocos2d.h"
class SunCellSprite :public  cocos2d::CCSprite
{
public:
	SunCellSprite(void);
	~SunCellSprite(void);
    CREATE_FUNC(SunCellSprite);
    virtual bool init();
};

SunCellSprite.cpp

#include "SunCellSprite.h"
USING_NS_CC;

SunCellSprite::SunCellSprite(void)
{

}
SunCellSprite::~SunCellSprite(void)
{
}
bool SunCellSprite::init()
{
	if(!CCSprite::initWithSpriteFrameName("Sun_1.png"))
	{
		return false;
	}
   int i;
   CCArray* sunArray = CCArray::create();//创建一个数组用于存放太阳因子的帧
   sunArray->retain();
   //下面是太阳因子的动画实现过程
   for(i=1;i<23;i++)
   {
	   CCSpriteFrame* sunFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("Sun_%d.png",i)->getCString());
	   sunArray->addObject(sunFrames);
   }
   CCAnimation* sunAnimation=CCAnimation::createWithSpriteFrames(sunArray,0.2f);
   this->runAction(CCRepeatForever::create(CCAnimate::create(sunAnimation)));
   return true;
}

	 
现在我们要把太阳因子精灵加到太阳因子层里面去;

在上面的SunCellLayer.h中我们已经声明过了,大家回头看下;

下面我们就来填充SunCellLayer.cpp哪些空的函数体,我们要求太阳因子每隔一小段时间产生一次。所以在上面的

void SunCellLayer::initSunCell(float dt)函数中加入以下代码:


void SunCellLayer::initSunCell(float dt)
{
       this->_sunCellSprite = SunCellSprite::create();//产生代糖因子
	   this->_sunBatchNode->addChild(this->_sunCellSprite);//把太阳因子加入到精灵批处理节点中去
	   CCSize winsize = CCDirector::sharedDirector()->getWinSize();//获取窗口的大小
	   //设定太阳因子产生的随机位子
	   this->_sunCellSprite->setPosition(ccp(3*winsize.width/4 * rand()/RAND_MAX + 1*winsize.width/5,winsize.height+this->_sunCellSprite->getContentSize().height));
	   this->SunCellMoveWay();//执行上面提到的两个线性运动,并在金币产生的位置回收太阳因子
}

我们要求太阳因子做两个线性运动后回收;所以在上面的void SunCellLayer::SunCellMoveWay()中加入以下代码:

void SunCellLayer::SunCellMoveWay()
{
	//这个函数代表太阳因子的具体运动过程
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	CCFiniteTimeAction* sunCellMove1 = CCMoveTo::create(4.0f,ccp(this->_sunCellSprite->getPosition().x,1* winSize.height/4 *rand()/RAND_MAX + 2*winSize.height/5));
	CCFiniteTimeAction* sunCellMove2 = CCMoveTo::create(0.5f,ccp(2*winSize.width/7, 8*winSize.height/9));
	this->_sunCellSprite->runAction(CCSequence::create(sunCellMove1,sunCellMove2,CCCallFuncN::create(this,callfuncN_selector(SunCellLayer::removeSunCell)),NULL));
}

在上面的void SunCellLayer::removeSunCell(CCNode* pSend)加入以下代码:

void SunCellLayer::removeSunCell(CCNode* pSend)
{
	CCSprite* sprite = (CCSprite*) pSend;
	this->_sunBatchNode->removeChild(sprite,true);//从精灵批处理节点中回收太阳因子
	((GameLayer*)this->getParent())->_dollarDisplayLayer->_dollar = ((GameLayer*)this->getParent())->_dollarDisplayLayer->_dollar +25;//这句很重要,我的得意之处
	//利用C++多态实现金币的自动递增;
}
在SunCellLayer.cpp的init()中调用定时器:

schedule(schedule_selector(SunCellLayer::initSunCell),9.0f);//太阳因子的定时器


最后把我们的太阳因子层加到主游戏层里面去:

在GameLayer.h中加入以下代码:

#include "SunCellLayer.h"
SunCellLayer* _sunCellLayer;
	void initSunCellLayer();
	

在GameLayer.cpp的构造函数中:(这点很重要,一不小心就有可能造成内存泄露)

this->_sunCellLayer=NULL;

在void GameLayer::initSunCellLayer()中定义初始化太阳因子层:

//初始化太阳因子层
void GameLayer::initSunCellLayer()
{
	this->_sunCellLayer = SunCellLayer::create();
	this->addChild(this->_sunCellLayer);
}

在GameLayer.cpp中的init()方法中加入以下代码:
	this->initSunCellLayer();

这样太阳因子部分就完成了。。貌似有点多啊,先生们!










分享到:
评论

相关推荐

    Cocos2d-x实战:JS卷——Cocos2d-JS开发

    资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...

    cocos2d-x-2.1.5

    cocos2d-x-2.1.5

    cocos2d-x事件类

    在使用cocos2d-x开发游戏的过程中,为了实现逻辑和显示相分离。 在下通宵了一个晚上,写出了该事件类。 谨记,该事件只能用于cocos2d-x中。 事件发送者需要继承EventDispatcher类 事件接收者需要继承EventHandle类...

    Cocos2d-x游戏编程——C++篇 .iso

    Cocos2d-x游戏编程——C++篇(电子工业出版社,徐飞 著)书本配套的光盘代码,

    Cocos2d-x高级开发教程

    Cocos2d-x是移动跨平台开发最流行的游戏引擎,而本书是一本很全面的、比较‘接地气’的游戏开发教程。书中汇聚了热门手机游戏《捕鱼达人》开发的实战经验,作者从最基础的内容开始,逐步深入地介绍了Cocos2d-x的相关...

    大富翁手机游戏开发实战基于Cocos2d-x3.2引擎

    资源名称:大富翁手机游戏开发实战基于Cocos2d-x3.2引擎内容简介:李德国编著的《大富翁手机游戏开发实战(基于 Cocos2d-x3.2引擎)》使用Cocos2d-x游戏引擎技术,带领读者一步一步从零开始进行大富翁移动游戏的开发...

    cocos2d-x实战项目

    cocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML文件读取与骨骼动画.rarcocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML...

    Cocos2d-x 3.x游戏开发实战pdf含目录

    Cocos2d-x 3.x游戏开发实战pdf含目录,内容详细,强烈推荐给大家。

    cocos2d-x-3.2旧版引擎下载

    cocos2d-x-3.2下载,不多说。或者可以下载另一个资源 cocos引擎老版本集合(cocos2d-x-2.2.1 - 3.5) http://download.csdn.net/download/crazymagicdc/9982656

    Cocos2D-X游戏开发技术精解

    资源名称:Cocos2D-X游戏开发技术精解内容简介:Cocos2D-X是一款支持多平台的 2D手机游戏引擎,支持iOS、Android、BlackBerry等众多平台。当前,很多移动平台流行的游戏,都是基于Cocos2D-X开发的。 《Cocos2D-X...

    Cocos2D-X游戏开发技术精解.pdf

    《Cocos2D-X游戏开发技术精解》详细介绍如何使用Cocos2D-X引擎开发自己的移动平台游戏。全书共15章,主要内容包括:Cocos2D-X引擎简介;如何建立跨平台的开发环境;引擎的核心模块——渲染框架;如何实现动态画面和...

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    精通COCOS2D-X游戏开发 基础卷_2016.4-P399-13961841.pdf

    精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发

    cocos2d-x-3.0 类图

    这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...

    cocos2d-x实战 c++卷教程及完整源码

    cocos2d-x实战 c++卷教程及完整源码下载,使用最新cocos2d-x-3.14版本,在xcode7.3上已编译通过。 解决相关问题 1、解决源程序在高版本上无法编译问题 2、解决源程序中文注释部分,xcode上显示乱码问题 3、根据书籍...

    Cocos2d-x-3.x游戏开发之旅

    Cocos2d-x-3.x游戏开发之旅-钟迪龙著 全新pdf版和附书代码(代码为工程文件,可复制) 附带目录标签

    cocos2d-x windows vs2010配置

    Cocos2d-x windows vs2010 配置图文详解

    Cocos2d-x实战 JS卷 Cocos2d-JS开发

    Cocos2d-x实战 JS卷 Cocos2d-JS开发 PDF 电子书完整版本

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    cocos2d-x 3.0

    cocos2d-x 3.0 人物行走 . 包里有代码和 图片资源.

Global site tag (gtag.js) - Google Analytics