First on:
The main classes:
#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){}voidHole::onenter () {//Listen for touch events Ccdirector* Pdirector =Ccdirector::shareddirector (); Pdirector->gettouchdispatcher ()->addtargeteddelegate ( This, - -,false); Ccnode::onenter ();}voidHole::onexit () {Ccdirector* Pdirector =Ccdirector::shareddirector (); Pdirector->gettouchdispatcher ()->removedelegate ( This); Ccnode::onexit ();} Ccrect Hole::rect () {ccsize size= This->image->getcontentsize (); returnCcrectmake (-size.width/2,-size.height/2, Size.width, size.height);}BOOLHole::init () {//Initialize picture 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;}voidHole::update (floattick) { if(rand ()% -> -) {//hamster randomly out of hole, This- out(); }}voidHole:: 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);Sets the hole animation for the Gopher, and the callback function after the animation is completedccfinitetimeaction* action = Ccsequence::create (Ccanimate::create (animation), Cccallfuncnd::create ( This, Callfuncnd_selector (hole::call_back), NULL), NULL); This->image->runaction (action); This->state =1; }voidHole::call_back (ccnode* sender,void*ref){ This->state =0;}BOOLHole::isinsprite (cctouch*Touch) {Ccpoint TouchPoint= touch->getLocation (); Ccpoint Reallypoint= This-Converttonodespace (TouchPoint); Ccrect rect= This-BoundingBox (); if(Rect.containspoint (touchPoint)) {return true; } return false;}BOOLHole::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;}voidHole::cctouchmoved (cctouch* Touch, ccevent*Event){ }voidhole::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++)
{//Initialize nine Gongge sprites
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);
}
}
Setting the callback function
This->schedule (Schedule_selector (helloworld::update), 3.0);
return true;
}
void Helloworld::update (float tick)
{
for (Vector{//Timed trigger function for each sprite
(*iter)->update (tick);
}
}
Share a game called "Gopher", COCOS2DX version