zoukankan      html  css  js  c++  java
  • [置顶] cocos2d-x 植物大战僵尸(4) 帽子僵尸的产生

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

          首先来建一个类JsLayer继承CCLayer;JsLayer.h中,进行如下声明:

    #pragma once
    #include "e:cocos2d-xcocos2d-x-2.2cocos2d-x-2.2cocos2dxlayers_scenes_transitions_nodescclayer.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-xcocos2d-x-2.2cocos2d-x-2.2cocos2dxsprite_nodesccsprite.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();


    看下效果图吧:感觉还不错:









  • 相关阅读:
    使用Haskell写web
    src/lxml/etree.so: undefined symbol: xmlSchematronSetValidStructuredErrors 解决方案
    CentOS允许某一端口接受外部链接
    windows下的NTP服务
    Huffman树,Huffman编码的实现(C#)
    OpenGL的函数(GLU, GLUT)
    OpenGL的函数(GL)
    GLUT函数说明
    FreeImage使用基础,图像旋转,图像滤波
    Hello PureMVC!!!
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3457436.html
Copyright © 2011-2022 走看看