分享一個《打地鼠》的小遊戲,cocos2dx版本,《打地鼠》cocos2dx

來源:互聯網
上載者:User

分享一個《打地鼠》的小遊戲,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);
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.