分享一个《打地鼠》的小游戏,cocos2dx版本

先上效果图:

  

   分享一个《打地鼠》的小游戏,cocos2dx版本 

主要的类:

  

#include "Hole.h"



#include <stdlib.h>



#include "ccMacros.h"



Hole::Hole(void)

{

    this->image = NULL;

    this->animation = NULL;

    this->hit = NULL;

    this->state = 0;

}





Hole::~Hole(void)

{



}





void Hole::onEnter()

{  //监听触摸事件

    CCDirector* pDirector = CCDirector::sharedDirector();

    pDirector->getTouchDispatcher()->addTargetedDelegate(this, -128, false);

    CCNode::onEnter();

}



void Hole::onExit()

{

    CCDirector* pDirector = CCDirector::sharedDirector();

    pDirector->getTouchDispatcher()->removeDelegate(this);

    CCNode::onExit();

}



CCRect Hole::rect()

{

    CCSize size = this->image->getContentSize();

    return CCRectMake(-size.width / 2, -size.height / 2, size.width, size.height);

}



bool Hole::init()

{  //初始化图片

    CCSprite::init();

    this->image = CCSprite::create("emptyhole.JPG");

    CCSize size = this->image->getContentSize();

    

    this->setContentSize(size);

    this->image->setPosition(CCSize(size.width/2,size.height/2));

    this->addChild(this->image);

    

    return true;

}



void Hole::update(float tick)

{

    if( rand()%100 > 60)

    {  //地鼠随机出洞,

        this->out();    

    }

}



void Hole::out()

{

    this->animation = CCAnimation::create();



    this->animation->addSpriteFrameWithFileName("show1.JPG");

    this->animation->addSpriteFrameWithFileName("show2.JPG");

    this->animation->addSpriteFrameWithFileName("show3.JPG");

    this->animation->addSpriteFrameWithFileName("show4.JPG");

    this->animation->addSpriteFrameWithFileName("show5.JPG");

    this->animation->addSpriteFrameWithFileName("show6.JPG");

    this->animation->addSpriteFrameWithFileName("show5.JPG");

    this->animation->addSpriteFrameWithFileName("show4.JPG");

    this->animation->addSpriteFrameWithFileName("show3.JPG");

    this->animation->addSpriteFrameWithFileName("show2.JPG");

    this->animation->addSpriteFrameWithFileName("show1.JPG");



    this->animation->setDelayPerUnit(0.1f);

    this->animation->setRestoreOriginalFrame(true);



    //CCAction* action = CCRepeat::create(CCAnimate::create(animation),1);

  //设置 地鼠的出洞动画,和动画完成之后的回调函数

    CCFiniteTimeAction * action = CCSequence::create(CCAnimate::create(animation),CCCallFuncND::create(this,callfuncND_selector(Hole::call_back),NULL),NULL); 



    this->image->runAction(action);

    this->state = 1;

    

}





void Hole::call_back(CCNode* sender,void* ref)

{

    this->state = 0;

}





bool Hole::isInSprite(CCTouch* touch)

{

    CCPoint touchPoint = touch->getLocation();

    CCPoint reallyPoint = this->convertToNodeSpace(touchPoint);

    CCRect rect = this->boundingBox();

    

    if(rect.containsPoint(touchPoint))

    {

        return true;

    }



    return false;

}





bool Hole::ccTouchBegan(CCTouch* touch, CCEvent* event)

{

    if(this->isInSprite(touch) && this->state == 1)

    {

        //this->image->stopAllActions();

        CCSprite* sp = CCSprite::create("hit.JPG");

        CCTexture2D* hit = sp->getTexture();

        this->image->setTexture(hit);

        this->state = 0;

        return true;

    }

    return false;

}



void Hole::ccTouchMoved(CCTouch* touch, CCEvent* event)

{

    

}





void Hole::ccTouchEnded(CCTouch* touch, CCEvent* event)

{

    this->image->stopAllActions();

    CCSprite* sp = CCSprite::create("emptyhole.JPG");

    CCTexture2D* hit = sp->getTexture();

    this->image->setTexture(hit);

}

    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {  //初始化九宫格的精灵
            Hole* hole = new Hole();
            hole->init();
            hole->setPosition(CCSize(i*hole->getContentSize().width +  hole->getContentSize().width/2
                , j*hole->getContentSize().height + hole->getContentSize().height/2));
            this->vect.push_back(hole);
            this->addChild(hole);
        }
    }
  //设置回调函数
    this->schedule(schedule_selector(HelloWorld::update),3.0);

    return true;
}


void HelloWorld::update(float tick)
{
    for(vector<Hole*>::iterator iter = this->vect.begin();iter != this->vect.end();iter++)
    {  //定时触发每个精灵的触发函数
        (*iter)->update(tick);
    }
}

 

你可能感兴趣的:(cocos2dx)