zoukankan      html  css  js  c++  java
  • [置顶] cocos2d-x 植物大战僵尸(13)类似酷跑的【同一角色不同动画间的切换的实现】

          有几天没和大家分享博客了,原因很简单,就是我在运行第12章所写的代码时:(开始一切正常,不过没多久就出现了内存泄露!。可能求成心切吧,当时没多加考虑就把代码发上去了。我在此对看过第12章得朋友表示歉意,真的很抱歉。(好比,大家原先的思维是对的,可能经我这么一搅和,却错了!)所以,因为上次的过失,我决定了,以后我一定对写过的代码多做测试,直到确定没错误了,才会发到CSDN上面去!再次向大家表示歉意!这几天来我一直在(为实现同一角色的不同动画的切换)而做了大量的尝试,基本上都解决了!(所以我也想和大家说句心里话:我们在面临陌生的事物时,最好是自己多去实践,而不是等着看别人的新发现!这样不利于自己灵感的爆发,你也许不相信灵感,不过我感觉它是在你多次的实验中产生的);

         (我们重新写一下12章得内容,有需要最新源码的朋友可以在评论里留言!这一章写完以后植物大战僵尸的第一关也将要接近尾声了。我在考虑第二关年前有没有时间写了。不说这烦心事了);

          好了开始吧:第12章主要是要实现僵尸被子弹多次击中后,所要做的系列动画。这些动画在redcat.plist文件中:我截个图吧:(就是这个)

                             

          我们打开我们工程里的RedCatJsSprite.h文件;在里面加入几句代码:(这是红帽子僵尸要做的系列动画)

    cocos2d::CCFiniteTimeAction* _walkAction;
    	cocos2d::CCFiniteTimeAction* _headAction;//头动画
    	cocos2d::CCFiniteTimeAction* _noHeadAction;//无头动画;
    
    	//无头动画函数
    	cocos2d::CCFiniteTimeAction*  headAninmation();
    	//僵尸头动画
    	cocos2d::CCFiniteTimeAction*  noHeadAninmation();
    
    	//僵尸的倒地动画:
    	cocos2d::CCFiniteTimeAction*  downTheGround();
    	cocos2d::CCFiniteTimeAction* _downAction;

    在RedCatJsSprite.cpp中定义这几个动画;(然后在碰撞检测中执行,这要到后面说。)

    下面是红帽子僵尸的头动画,无头动画,和死亡动画的定义:(大家注意下,这都是连续性动作)

    //僵尸的头动画;
    CCFiniteTimeAction* RedCatJsSprite::headAninmation()
      {
    	    int i;
           CCArray* headArray =CCArray::create();//创建一个数组用于存放僵尸的头帧;
           headArray->retain();
           for(i=1;i<13;i++)
            {
    	       CCSpriteFrame* headFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("jshead_%d.png",i)->getCString());
    	       headArray->addObject(headFrames);
            }
               CCAnimation* headAnimation=CCAnimation::createWithSpriteFrames(headArray,0.07f);
    		  
    		   this->_headAction =CCAnimate::create(headAnimation);
               this->_headAction->retain();
    		 
    		   return this->_headAction;
    		  
      } 
    
    CCFiniteTimeAction* RedCatJsSprite::noHeadAninmation()
    {
    	
    	//僵尸的无头动画
    	int i;
    	CCArray* noHeadArray =CCArray::create();//创建一个数组用于存放僵尸的无头帧;
    	noHeadArray->retain();
    	for(i=1;i<19;i++)
    	{
    		CCSpriteFrame* noHeadFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("losthead_%d.png",i)->getCString());
    		noHeadArray->addObject(noHeadFrames);
    	}
    	CCAnimation* noHeadAnimation=CCAnimation::createWithSpriteFrames(noHeadArray,0.2f);
    	this->_noHeadAction =CCRepeat::create(CCAnimate::create(noHeadAnimation),50);
    	this->_noHeadAction->retain();
    	
    	return this->_noHeadAction;
    }
    
    cocos2d::CCFiniteTimeAction* RedCatJsSprite::downTheGround()
    {
    	int i;
    	CCArray* downArray =CCArray::create();//创建一个数组用于存放僵尸的倒地帧;
    	downArray->retain();
    	for(i=1;i<11;i++)
    	{
    	  CCSpriteFrame* downFrames = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("redcatjsdie_%d.png",i)->getCString());
    	  downArray->addObject(downFrames);
    	}
    	  CCAnimation* downAnimation=CCAnimation::createWithSpriteFrames(downArray,0.1f);
    	  this->_downAction =CCAnimate::create(downAnimation);
    	  this->_downAction->retain();
    
    	return this->_downAction;
    }

    然后我们在红帽子僵尸的碰撞检测层实现他们,碰撞检测层在12章我们已经做过声明,这里就不再从新声明了;(我重新写了12章得void  bulletBoundingJs();函数);它的内部是这样子的:(我来解释下:僵尸的生命值我们前面已经说了是60,每次子弹攻击减少3,当生命值等于9时我们让僵尸做无头动画:看下效果:)


    就是这里:

    if(sprite2->_hp==9){sprite2->stopAllActions();//终止之前的所有动作;
    sprite2->runAction(CCSequence::create(((GameLayer*)this->getParent())->_jsLayer->_redCatSprite->headAninmation(),CCSpawn::create(((GameLayer*)this->getParent())->_jsLayer->_redCatSprite->noHeadAninmation(),((GameLayer*)this->getParent())->_jsLayer->noHeadJsMoveWay(),NULL),NULL));
    }


    这个过程子弹与无头僵尸继续碰撞,当僵尸的生命值为零时:我们让僵尸倒地,随后消失:

    if(sprite2->_hp==0) 
    { sprite2->stopAllActions(); sprite2->runAction(CCSequence::create(((GameLayer*)this->getParent())->_jsLayer->_redCatSprite->downTheGround(),CCCallFuncN::create(((GameLayer*)this->getParent())->_jsLayer,callfuncN_selector(JsLayer::preRemoveRedCat)),NULL));
     }


          

    void BoundingLayer::bulletBoundingJs()
     {
    	 CCObject* object1;//创建遍历数组的两个对象
    	 CCObject* object2;
    	
    	CCArray* removeBulletArray= CCArray::create();//创建待删除的子弹的临时数组
    	 removeBulletArray->retain();//保存临时数组
    
    	 CCArray* removeJsArray =CCArray::create();//创建待删除僵尸的临时数组
    	 removeJsArray->retain();
    	 CCARRAY_FOREACH(((GameLayer*)this->getParent())->_touchLayer->_bulletArray,object1)//遍历所有子弹
    	 {
    		 BulletSprite* sprite1= (BulletSprite*)object1;
    		 CCARRAY_FOREACH(((GameLayer*)this->getParent())->_jsLayer->_jsArray,object2)//遍历所有僵尸
    		 {
    			 RedCatJsSprite* sprite2 =(RedCatJsSprite*)object2;
    			 if(sprite1->boundingBox().intersectsRect(sprite2->ReRedCatBounding()) )//碰撞检测机理
    			 {   
    				  
    				 removeBulletArray->addObject(sprite1);
    				 removeJsArray->addObject(sprite2);
    			 }
    		 }
    	 }
    	 CCARRAY_FOREACH(removeBulletArray,object1)//遍历待删除子弹数组
    	 {
    
    		 BulletSprite* sprite1 = (BulletSprite*)object1;
    		 sprite1->runAction(CCCallFuncN::create(((GameLayer*)this->getParent())->_bulletLayer,callfuncN_selector(BulletLayer::removeBullet)));
    		 CCARRAY_FOREACH(removeJsArray,object2)//遍历待删除僵尸数组
    		 {
    
    			 RedCatJsSprite* sprite2 =(RedCatJsSprite*)object2;
    			 sprite2->_hp =sprite2->_hp-3;
    			 sprite2->runAction(CCBlink::create(0.1,1));//僵尸闪烁
    
    			 if(sprite2->_hp==9)
    			 {
    				sprite2->stopAllActions();//终止之前的所有动作;
    				sprite2->runAction(CCSequence::create(((GameLayer*)this->getParent())->_jsLayer->_redCatSprite->headAninmation(),CCSpawn::create(((GameLayer*)this->getParent())->_jsLayer->_redCatSprite->noHeadAninmation(),((GameLayer*)this->getParent())->_jsLayer->noHeadJsMoveWay(),NULL),NULL));
    			 }  if(sprite2->_hp==0)
    			 {
    				 sprite2->stopAllActions();
    				 sprite2->runAction(CCSequence::create(((GameLayer*)this->getParent())->_jsLayer->_redCatSprite->downTheGround(),CCCallFuncN::create(((GameLayer*)this->getParent())->_jsLayer,callfuncN_selector(JsLayer::preRemoveRedCat)),NULL));
    				
    			 }
                        }
                     }
    
     removeBulletArray->release();//释放临时数组
     removeJsArray->release();//释放临时数组
    }
    
    释放临时数组;这里面包含了同一精灵,不同动画间的切换,这个切换要看起来自然和动画不重叠也是需要好好琢磨的。如果下次要是还讲的话,那就讲第二关了。(我还有点内容没讲完,索性今晚都讲了!下章....)
    
  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    22. Generate Parentheses (backTracking)
    21. Merge Two Sorted Lists
    20. Valid Parentheses (Stack)
    19. Remove Nth Node From End of List
    18. 4Sum (通用算法 nSum)
    17. Letter Combinations of a Phone Number (backtracking)
    LeetCode SQL: Combine Two Tables
    LeetCode SQL:Employees Earning More Than Their Managers
  • 原文地址:https://www.cnblogs.com/riasky/p/3481522.html
Copyright © 2011-2022 走看看