码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-11 16:07:12      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

先上效果图:

  技术分享

  技术分享 

主要的类:

  

#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版本

标签:

原文地址:http://www.cnblogs.com/archy_yu/p/4417723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!