cocos2d 打地鼠项目


开始界面代码就不发了,相信各位同仁都会,就把打地鼠游戏场景的代码发一下。不喜勿喷,谢谢大家。


1.一个容器类

#include <iostream>

using namespace std;

#include "cocos2d.h"

#include "Diglett.hpp"



USING_NS_CC;

class VecManager

{

public:

    static VecManager*getInstance()

    {

        if(vecManager==nullptr)

        {

            vecManager=new VecManager();

        }

        return vecManager;

    }

    

    void addDiglett(Diglett*diglett);

    void deleteDiglett(Diglett*diglett);

    Vector<Diglett*>&getDiglettVec();

    int getDiglettVecSize();

    


   

    Vector<Diglett*>DiglettVec;

    

    void vecClear();

public:

    

    static VecManager*vecManager;

};




#include "VecManager.hpp"

VecManager*VecManager::vecManager=new VecManager();


void VecManager::addDiglett(Diglett*diglett)

{

    this->DiglettVec.pushBack(diglett);


}

void VecManager::deleteDiglett(Diglett*diglett)

{

    this->DiglettVec.eraseObject (diglett);

    diglett->removeFromParentAndCleanup( true);


}

Vector<Diglett*>&VecManager::getDiglettVec()

{

      return  this->DiglettVec;

}

int VecManager::getDiglettVecSize()

{

     return this->DiglettVec.size();

}





void VecManager::vecClear()

{

    DiglettVec.clear();

}




2.一个地鼠类

#include <iostream>

using namespace std;

#include "cocos2d.h"

USING_NS_CC;

#include "ui/CocosGUI.h"

using namespace cocos2d::ui;

#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

class  Diglett:public Sprite

{

public:

    static Sprite* create(int type);

    virtual bool init(int type);

    void setType(int type);

    

    const int getLevel();


    void beCatch(int type);

    void beCatch1(Vec2  v);


private://没用

    int fishLevel;//地鼠的等级

   

};




//地鼠死亡图片

string str[5]={"hit_pretty_mouse.png","hit_angle_mouse.png","hit_craze_mouse.png","hit_rabbit.png","hit_king_mouse.png"};

//地鼠图片

string str2[5]={"pretty_mouse.png","angle_mouse.png","craze_mouse.png","rabbit.png","king_mouse.png"};

//位置

Vec2 a[3][3]={Vec2(355, 215),Vec2(233, 125),Vec2(118, 215),Vec2(95, 84),Vec2(235, 80),Vec2(370, 80),Vec2(100, 150),Vec2(233, 150),Vec2(360, 150)};

Sprite* Diglett::create(int type)

{

    Diglett* diglett = new Diglett();

    if ( diglett &&  diglett->init(type))

    {

         diglett->autorelease();

        return  diglett;

    }

    CC_SAFE_DELETE( diglett);

    return nullptr;

}

bool Diglett::init(int type)

{

    if (!Sprite::init()) {

        return false;

    }

    

    

    

  auto vecManager = VecManager::getInstance();//容器对象

    

    VecManager::getInstance()->addDiglett(this);

    cout<<"地鼠容器的Size:"<<VecManager::getInstance()->getDiglettVecSize()<<endl;


    

   

    this->setType(type);  //地鼠成功执行动画

    this->setScale(1);


   

    int i=rand()%3;

    int j=rand()%3;

    

    auto _callFunc=CallFunc::create([=](){

        this->setPosition(a[i][j]);

        

    });

    auto _delayTime=DelayTime::create(2);

    auto _callFunc1=CallFunc::create([=](){

        vecManager->deleteDiglett(this);

        

    });

    

    

    auto _seq=Sequence::create(_callFunc,_delayTime,_callFunc1,NULL);

    this->runAction(_seq);

    

    

    cout<<"地容器的Size:"<<VecManager::getInstance()->getDiglettVecSize()<<endl;

    

    

    

    

    return true;

}

void Diglett::setType(int type)

{


    this->setTexture(str2[type]);

    

    

}


const int Diglett::getLevel()

{


    return fishLevel;

}


void Diglett::beCatch(int type)

{

   

    

   this->setTexture(str[type]);

    auto action_1 = DelayTime::create(2);

    auto action_2 = CallFuncN::create([=](Ref* sender)

      {

       

          Diglett*diglett = (Diglett*)sender;

         

          VecManager::getInstance()->deleteDiglett(diglett);

         

      });

    auto action  = Sequence::create(action_1,action_2, NULL);

    this->stopAllActions();// 停止动作

     

    this->runAction(action);

}


void Diglett::beCatch1(Vec2  v)

{

    auto _spriteFrameCache=SpriteFrameCache::getInstance();

    

    _spriteFrameCache->addSpriteFramesWithFile("Click.plist", "Click.png");

    

    auto wenli1=Sprite::create();

    wenli1->setSpriteFrame("star0.png");

    wenli1->setPosition(Vec2(this->getContentSize().width/2, this->getContentSize().height/2));

    log("star");

    this->addChild(wenli1,200);

    Vector<AnimationFrame*>_animationFrameVector;//动画容器

    for (int i=0; i<=11; ++i)

    {

        string str=StringUtils::format("star%d.png",i);

        //找精灵帧

        auto _spriteFrame=_spriteFrameCache->getSpriteFrameByName(str);

        //精灵帧->动画帧

        auto aniFrame=AnimationFrame::create(_spriteFrame, 0.01, ValueMap());

        

       

        _animationFrameVector.pushBack(aniFrame);//把动画帧缓存动画容器

    }

    //动画容器中的动画帧->动画

    auto _animation=Animation::create(_animationFrameVector,0.2f,10);

    //动画->动作

    auto _animate=Animate::create(_animation);

    wenli1->runAction(_animate);

}

3.打地鼠场景


class Diglett2:public Layer

{

public:

    static Scene*createScene() ;

    bool init()override;

    


    void onEnter()override;

    void onExit()override;

    CREATE_FUNC(Diglett2);

    

    Label*label;

    int num;

    int z;

    Sprite*wenli1;

    Sprite*wenli2;

   

    bool  onTouchBegan(Touch*touch,Event *unused_event);


    

    void  onTouchMoved(Touch*touch,Event *unused_event);


    void  onTouchEnded(Touch*touch,Event *unused_event);

    

    void  onTouchCancelled(Touch*touch,Event *unused_event);

  

    

};



#include "Diglett2.hpp"

#include "Diglett.hpp"

Scene*Diglett2::createScene()

{

    auto scene=Scene::create();

    auto layer=Diglett2::create();

    scene->addChild(layer);

    return scene;

}

bool Diglett2::init()

{

    if (!Layer::init())

    {

        return false;

    }


    num=0;

    //背景图片

    auto sprite=Sprite::create("background.png");

    sprite->setPosition(Vec2(240, 160));

    sprite->setContentSize(Size(480, 320));

    this->addChild(sprite);

    

    

    //纹理

    {auto _spriteFrameCache=SpriteFrameCache::getInstance();

    

    _spriteFrameCache->addSpriteFramesWithFile("Atack.plist", "Atack.png");


    wenli1=Sprite::create();

    wenli1->setPosition(Vec2(-100,-100));

        this->addChild(wenli1,100);

    Vector<AnimationFrame*>_animationFrameVector;//动画容器

    for (int i=0; i<=1; ++i)

    {

        string str=StringUtils::format("hammer%d.png",i);

        //找精灵帧

        auto _spriteFrame=_spriteFrameCache->getSpriteFrameByName(str);

        //精灵帧->动画帧

        auto aniFrame=AnimationFrame::create(_spriteFrame, 0.2,ValueMap());

        _animationFrameVector.pushBack(aniFrame);//把动画帧缓存动画容器

    }

    //动画容器中的动画帧->动画

    auto _animation=Animation::create(_animationFrameVector,1.0f,5);

    //动画->动作

    auto _animate=Animate::create(_animation);

        wenli1->runAction(_animate);}

    

    

    

    

    

   

    


    //分数

    auto sprite2=Sprite::create("all_score.png");

    

    sprite2->setPosition(Vec2(400, 280));


    this->addChild(sprite2);

    

    label=Label::createWithSystemFont("0", "", 30);

    label->setPosition(Vec2(450, 280));

    

    this->addChild(label);

    

    


  

    //创造地鼠

    schedule([=](float s)

    {


         z=rand()%5;

       auto sprite1=Diglett::create(z);

        this->addChild(sprite1,20);


    }, 5, "WWW");

    

 

    return true;

}



void Diglett2::onEnter()

{

    Layer::onEnter();

        auto touchListener=EventListenerTouchOneByOne::create();//触摸监听

         touchListener->onTouchBegan=CC_CALLBACK_2(Diglett2::onTouchBegan, this);

         touchListener->onTouchMoved=CC_CALLBACK_2(Diglett2::onTouchMoved, this);

         touchListener->onTouchEnded=CC_CALLBACK_2(Diglett2::onTouchEnded, this);

         touchListener->onTouchCancelled=CC_CALLBACK_2(Diglett2::onTouchCancelled, this);

     

       Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority (touchListener,this);//注册该监听

    

}

void Diglett2::onExit()

{

    Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);

    Layer::onExit();

}


bool  Diglett2::onTouchBegan(Touch*touch,Event *unused_event)

{

    

    //锤子

    wenli1->setPosition(touch->getLocation());


    auto _callFunc=CallFunc::create([=]()

    {

        wenli1->setPosition(Vec2(-100,-100));

        

    });

    auto _delayTime=DelayTime::create(0.5);

    


    auto _seq=Sequence::create(_delayTime,_callFunc,NULL);


    wenli1->runAction(_seq);


    

    

    


    

    

    

    auto vecManager = VecManager::getInstance();//容器对象

    for (auto diglett : vecManager->getDiglettVec())//地鼠容器,遍历

    {

        auto _diglett = (Diglett*)diglett;

    

            auto rect = _diglett->getBoundingBox();//地鼠的面积

            if (rect.containsPoint(touch->getLocation()))//地鼠的面积包含锤子

            {

                num=num+10;

                

                string str=StringUtils::format("%d",num);

                label->setString(str.c_str());

                 //死亡图片

                _diglett->beCatch1(touch->getLocation());

                //星星

                _diglett->beCatch(z);

                

            }

        }

   


    

    

    return true;

}


 void  Diglett2::onTouchMoved(Touch*touch,Event *unused_event)

{

   



}



 void  Diglett2::onTouchEnded(Touch*touch,Event *unused_event)

{

    Vec2 touchPoint =touch->getStartLocation();

    log("Touch Ended\r\n");


}


 void  Diglett2::onTouchCancelled(Touch*touch,Event *unused_event)

{

    long("Touch Cancelled\r\n");

}





猜你喜欢

转载自blog.csdn.net/qq_41939248/article/details/79937493
今日推荐