Share a game named cocos2dx, cocos2dx, and cocos2dx.

Source: Internet
Author: User

Share a game named cocos2dx, cocos2dx, and cocos2dx.

First:

  

   

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) {} void Hole: onEnter () {// listens to the touch event CCDirector * pDirector = CCDirector: shareddire(); pDirector-> getTouchDispatcher () -> addTargetedDelegate (this,-128, false); CCNode: onEnter ();} void Hole: onExit () {CCDirector * pDirector = CCDirector: shareddire (); 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 () {// initialize the image 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) {// The hamster randomly holes out, 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 ("show4.JPG"); this-> animation-> addSpriteFrameWithFileName ("show5.JPG "); this-> animation-> addSpriteFrameWithFileName ("show6.JPG"); this-> animation ("show5.JPG"); this-> animation-> addSpriteFrameWithFileName ("show4.JPG "); this-> animation-> addSpriteFrameWithFileName ("show3.JPG"); this-> animation ("show2.JPG"); this-> animation-> addSpriteFrameWithFileName ("show1.JPG "); this-> animation-> setDelayPerUnit (0.1f); this-> animation-> setRestoreOriginalFrame (true); // CCAction * action = CCRepeat: create (CCAnimate :: create (animation), 1); // you can specify the animation of the hamster and the callback function CCFiniteTimeAction * action = CCSequence: create (CCAnimate: create (animation) after the animation is completed ), 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 ++)
{// Initialize the Gangu genie
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 );
}
}
// Set the callback function
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 ++)
{// Trigger the trigger function of each genie at regular intervals
(* Iter)-> update (tick );
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.