您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x简单游戏<植物大战僵尸>代码实现|第八部分:子弹类<后续会提供源码下载链接>

2014-03-17 17:45 1071 查看
这个植物大战僵尸的小游戏Demo 虽然下,但是基本包括了:

  1.植物的选取、僵尸的攻击、发射子弹;

  2.太阳的生成、碰撞检测等功能;

 

 

 第一部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第一部分:开始场景

 第二部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第二部分:菜单场景

 第三部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第三部分:通关场景

 第四部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第四部分:通关失败场景

 第五部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第五部分:公用类

 第六部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第六部分:植物类

 第七部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第七部分:僵尸类

 第八部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第八部分:子弹类

 第九部分<A>:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第九部分:游戏场景GameScene.h
 第九部分<B>:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第九部分:游戏场景GameScene.cpp

#ifndef __MyTest__Bullets__

#define __MyTest__Bullets__

#include <iostream>

#include "CommonHeader.h"

#include "Plants.h"

#define BulletSpeedRatio 200
//子弹速度(时间=距离/子弹速度)

class Bullet:publicCCSprite {

public:

    
   Plants *plants;
   CCPoint position;

    

public:

    

    //子弹所属植物和位置

    //构造函数
    Bullet(Plants *aplants,CCPoint point,CCLayer *layer);
    ~Bullet();
   void creatSunflowerBullet(CCPoint point,CCLayer *layer);
   void creatPeaBullet(CCPoint point,CCLayer *layer);
   void creatPeaBulletOfMid(CCNode *sender,CCLayer *layer);//双豌豆调用
   void creatPeaBulletOfThree(CCNode *sender,CCLayer *layer);

   void collectSunshine(CCNode *sender);
   void moveSunshine(CCNode *sender,CCMenu *menu);

};

#endif /* defined(__MyTest__Bullets__) */

#include "Bullets.h"
externCCArray *bulletArray;//扩展数组

//有参构造函数
Bullet::Bullet(Plants *aplants,CCPoint point,CCLayer
*layer){

    //根据植物名称创建植物对象

    plants =
new Plants(aplants->plantsName);
   switch (aplants->plantsName) {
       caseSunflower:{

         
           this->creatSunflowerBullet(point, layer);
           break;
        }
       caseSmallPea:
       caseMidllePea:

        caseThreeBulletPea:{
           this->creatPeaBullet(point, layer);
           break;
        }
           default:
           break;
    }

    

    
}

//创建向日葵
voidBullet::creatSunflowerBullet(CCPoint point,CCLayer
*layer){

    //从缓存中读取

    CCSpriteFrameCache *cache =CCSpriteFrameCache::sharedSpriteFrameCache();

    cache->addSpriteFramesWithFile("Sun_default.plist");

    this->initWithFile("Sun-1(被拖移).tiff");
   this->setScale(0.5);

    
   char temp[50];
   CCArray *plistArray =CCArray::createWithCapacity(10);
   for (int i =1; i<22; i++) {
       sprintf(temp,"Sun-%i(被拖移).tiff",i);
       CCSpriteFrame *frame = cache->spriteFrameByName(temp);
        plistArray->addObject(frame);
    }

    //创建动画
   CCAnimation *animation =CCAnimation::createWithSpriteFrames(plistArray);
   CCAnimate *animate =CCAnimate::create(animation);
   this->runAction(CCRepeatForever::create(animate));//创建永久动作

    

    //创建目录精灵

    CCMenuItemSprite *menuSprite =CCMenuItemSprite::create(this,this,this,menu_selector(Bullet::collectSunshine));
    menuSprite->setTag(4002);
   CCMenu *menu =CCMenu::create(menuSprite,NULL);
    menu->setPosition(ccp(point.x+20,point.y+30));
    menu->setEnabled(true);
    menu->setTag(4001);
    layer->addChild(menu);//在场景中添加

    //设置跳跃
   CCJumpTo *jumpTo =CCJumpTo::create(1.0f,ccp(menu->getPosition().x-40,menu->getPosition().y-25),30,1);
    menu->runAction(jumpTo);
    menu->setZOrder(1000);//设置z轴目标点

}

//搜集阳光
voidBullet::collectSunshine(CCNode *sender){

    sender->stopAllActions();
   CCSize size =GET_WINSIZE;

//    CCMoveTo *moveTo = CCMoveTo::create(1.0f,ccp(size.width/2+60,size.height/2+320));
   CCMoveTo *moveTo =CCMoveTo::create(1.0f,ccp(60,320));
   CCCallFuncND *funcND =CCCallFuncND::create(this,callfuncND_selector(Bullet::moveSunshine),sender->getParent());
   CCSequence *sequence =CCSequence::create(moveTo,funcND,NULL);
    sender->getParent()->runAction(sequence);

    
}

//移动阳光
voidBullet::moveSunshine(CCNode *sender,CCMenu
*menu){

   externint SunNumber;

    SunNumber =
SunNumber+50;

    menu->removeFromParent();

}

//
voidBullet::creatPeaBullet(CCPoint point,CCLayer
*layer){

    bulletArray->retain();
   position = point;

    

    CCSpriteFrameCache *cache =CCSpriteFrameCache::sharedSpriteFrameCache();

    cache->addSpriteFramesWithFile("PeashooterBullet_default.plist");

    this->initWithSpriteFrameName("PeashooterBullet-1(被拖移).tiff");
   this->setPosition(ccp(point.x+10,
point.y+10));
   this->setScale(0.5);

    bulletArray->addObject(this);//把子弹添加到数组里
    layer->addChild(this);//加入场景
   float distance =500-this->getPosition().x;
   float time = distance/BulletSpeedRatio;
   CCMoveTo *moveTo =CCMoveTo::create(time,ccp(500,this->getPosition().y));
   this->runAction(moveTo);

    if (plants->plantsName ==MidllePea)
{
       this->creatPeaBulletOfMid(this,layer);
    }

}
voidBullet::creatPeaBulletOfMid(CCNode *sender,CCLayer
*layer){

    CCSprite *sunSprite = (Bullet *)CCSprite::createWithSpriteFrameName("PeashooterBullet-1(被拖移).tif");
    sunSprite->setPosition(ccp(position.x+10,position.y+7));
    sunSprite->setScale(0.5f);
   bulletArray->addObject(sunSprite);
    layer->addChild(sunSprite);
   float distance =500-sunSprite->getPosition().x;
   float time = distance/BulletSpeedRatio;
   CCMoveTo *moveTo =CCMoveTo::create(time,ccp(500,sunSprite->getPosition().y));
   CCSequence *sequence =CCSequence::create(CCDelayTime::create(0.1f),moveTo,NULL);
    sunSprite->runAction(sequence);

}
voidBullet::creatPeaBulletOfThree(CCNode *sender,CCLayer
*layer){

    CCSprite* sunSprite = (Bullet*)CCSprite::createWithSpriteFrameName("PeashooterBullet-1(被拖移).tiff");
    sunSprite->setPosition(ccp(position.x+10,position.y+4));
    sunSprite->setScale(0.5);
   bulletArray->addObject(sunSprite);//把子弹添加到数组里边
    layer->addChild(sunSprite);
   float distance1 =500-sunSprite->getPosition().x;
   float time1 = distance1/BulletSpeedRatio;
   CCMoveTo *moveTo1 =CCMoveTo::create(time1,ccp(500,sunSprite->getPosition().y));
   CCSequence* seq =CCSequence::create(CCDelayTime::create(0.2),
moveTo1,NULL);
    sunSprite->runAction(seq);

    
}

Bullet::~Bullet(){

   //释放
   deleteplants;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐