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