Cocos2dx Crazy Parkour (Crazyrun) Game Project analysis

Source: Internet
Author: User
Tags addchild

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise it will be held liable http://blog.csdn.net/a1977722280/article/details/42966761 game: http://www.mumayi.com/android-475978.html

Go straight to the point and start analyzing the development techniques involved

As a game, it is composed of UI and scene, starting interface, level selection, prompting box and so on are UI, the core game code in the course of the game is implemented in the scene:

First, the game start interface by the background, menu, game logo, dynamic fluttering clouds, particles, background music elements of the composition

1, background, logo are cocos2dx in the Picture wizard:

Related code:

/* Game title Picture */
ccsprite* TitleSprite2 = ccsprite::create ("Pickure/mainui/886.png");
Titlesprite2->setposition (CCP (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2));
This->addchild (TitleSprite2, 3);

VISIBLESIZE.WIDTH/2 is a relative screen coordinate. , we can also fill in absolute coordinates, such as Titlesprite2->setposition (CCP (200, 400));

2, the menu is implemented with Ccmenuitemimage, I use a single creative mode, because this makes the menu position more flexible

Related code:

Ccmenuitemimage *tile = Ccmenuitemimage::create (
"Pickure/mainui/image 185_1.png",
"Pickure/mainui/image 187_1.png",
This
Menu_selector (Mainui::enter));
CC_BREAK_IF (! tile);
Tile->setposition (CCP (650,200));
ccmenu* ptile = ccmenu::create (tile, NULL);
Ptile->setposition (Ccpointzero);
CC_BREAK_IF (! ptile);
This->addchild (Ptile, 3);

There are many similarities between the menu and the button, but the button function can be realized by the sprite action, but the most used menu is more convenient.

3, COCOS2DX on the windows have a special particle production tools, C # Writing, I use the code and the interface is simple to optimize a bit

Related code:

Small leaf effects
Ccparticlesystem * Lizi = new Ccparticlesystemquad ();
Set plist animation file
Lizi->initwithfile ("Treedown.plist");
Set particle display Position
Lizi->setposition (CCP (400,500));
Adding particle objects to the scene
This->addchild (lizi,10);
4, background music This will not be much, play a local music file

Related code:

/* Play background music */
Cocosdenshion::simpleaudioengine::sharedengine ()->playbackgroundmusic ("Music/bgmusic.wav",-1);

Of course we can also define an ID assigned to it to control the stop play, which is used in the use of sound effects, because in the game may have a variety of sound effects played simultaneously, we might need to stop a playing sound

5, fluttering clouds, using the sprite action and callback events to achieve, set the target point, perform action callback

Related code:

Set Sprite Actions

ccmoveto* toright = ccmoveto::create (8.0f, CCP (2800,thing1->getpositiony ()));
ccsequence*s = Ccsequence::create (ToRight,
Cccallfuncn::create (This,
Callfuncn_selector (mapthings::getright)),
NULL);
Thing1->runaction (s);

callback function

void Mapthings::getright (ccobject* psender)
{
Thing1->setflipx (TRUE);
ccmoveto* ToLeft = ccmoveto::create (8.0f, CCP (200,thing1->getpositiony ()));
ccsequence*s = Ccsequence::create (ToLeft,
Cccallfuncn::create (This,
Callfuncn_selector (Mapthings::getleft)),
NULL);
Thing1->runaction (s);
}


void Mapthings::getleft (ccobject* psender)
{
Thing1->setflipx (FALSE);
ccmoveto* toright = ccmoveto::create (8.0f, CCP (2800,thing1->getpositiony ()));
ccsequence*s = Ccsequence::create (ToRight,
Cccallfuncn::create (This,
Callfuncn_selector (mapthings::getright)),
NULL);
Thing1->runaction (s);
}

Second, the game level selection interface involves the relevant technology and architecture

Composition elements: Game score, number of coins, level icon, (Task/warehouse, Recharge menu), Action Wizard, return menu composition

1, gold coins and game score using Cclabelatlas to achieve

Related code:

Cclabelatlas*sumscorelabs = Cclabelatlas::create (Ccstring::createwithformat ("%d", Sumscore)->getCString (), " Pickure/choiceui/labelatlasimg.png ", 24, 32,
' 0 ');
Sumscorelabs->setanchorpoint (CCP (0,0.5));
Sumscorelabs->setposition (CCP (260,400));
Sumscorelabs->setscale (0.7f);
This->addchild (sumscorelabs,3);
Sumscorelabs->setstring ("360");

2, how to jump each page? Switching scenes in a menu callback function

Related code:

Enter the Mall interface
void Choiceui::shop (ccobject* psender)
{


Ccdirector::shareddirector ()->replacescene (Cctransitionflipangular::create (1.0f,shop::scene ()));


}

3, here is how to record whether the player has access to the level? Here to complete the previous pass, you can go to the next level

Related code:

Add a lock icon to a level that doesn't have permission

if (maxlock!=4)
{
Lock4 =ccsprite::create ("Pickure/choiceui/suo.png");
Lock4->setposition (CCP visiblesize.width/7+ (VISIBLESIZE.WIDTH/6.5+VISIBLESIZE.WIDTH/12), VISIBLESIZE.HEIGHT/2));
This->addchild (lock4,3);
if (maxlock!=3)
{
Lock3 =ccsprite::create ("Pickure/choiceui/suo.png");
Lock3->setposition (CCP (visiblesize.width/7+ (VISIBLESIZE.WIDTH/6.5+VISIBLESIZE.WIDTH/12), VISIBLESIZE.HEIGHT/2));
This->addchild (lock3,3);
if (maxlock!=2)
{
Lock2 =ccsprite::create ("Pickure/choiceui/suo.png");
Lock2->setposition (CCP (VISIBLESIZE.WIDTH/7+VISIBLESIZE.WIDTH/6.5+VISIBLESIZE.WIDTH/12, VISIBLESIZE.HEIGHT/2) );
This->addchild (lock2,3);

if (maxlock!=1)
{
Lock1 =ccsprite::create ("Pickure/choiceui/suo.png");
Lock1->setposition (CCP (400,200));
This->addchild (lock1,1);
}
}
}

Players click on the level icon to determine if they have access to the game scene

Related code:

void Choiceui::unlock2 (ccobject* psender)
{

   cocosdenshion::simpleaudioengine::sharedengine ()->playeffect (
    music/ Button.mp3 ");
  if (golds<36)
 {
    ccblink*blink = ccblink::create (1, 2);
    ccsequence*s = Ccsequence::create (Blink,
    cccallfuncn::create (this,
    callfuncn_selector (Choiceui::yincang)),
    null);
    tishi2->setvisible (true);
    tishi2->runaction (s);
    return;
}
 if (maxlock<2)
{
    ccrotateby* move = Ccrotateby::create (2,360);
    lock2->runaction (move);
   //showad ();
}

ccscaleby* out = Ccscaleby::create (0.5f,2);
cccallfunc* Callfunc = Cccallfunc::create (This, Callfunc_selector (choiceui::in));
ccsequence* actions = Ccsequence::create (out, Callfunc, NULL);
Ts->runaction (actions);

if (maxlock>1)
{

locknamber=2;
Cclog ("Tiled x=%d", maxlock);
if (score2!=0)
{
Sumscore (375, 295,score2);
Messbox ();
}
Else
{
Ccuserdefault::shareduserdefault ()->setintegerforkey ("Golds", golds-20);
Ccuserdefault::shareduserdefault ()->flush ();
Golds=ccuserdefault::shareduserdefault ()->getintegerforkey ("golds");
Sumgoldlabs->setstring (Ccstring::createwithformat ("%d", golds)->getcstring ());

Ccdirector::shareddirector ()->replacescene (Cctransitionpageturn::create (1.0f,scenea::scene (), false));

}
}
}

III. related technologies and architectures involved in the game scene

Composition elements: Game scoring calculation process, player character animation playback, motion control, Monster AI, collision gem detection, game map making and loading, use of props, and influence on characters, etc.

Map scrolling Implementation code:

void Scenea::update (float delta)

{

int bgposX1 = M_bgsprite1->getpositionx (); X-coordinate of background map 1
int bgposX2 = M_bgsprite2->getpositionx (); X-coordinate of background map 2

int bgispeed = 7; Map scrolling Speed

/* Two maps scroll left (two maps are adjacent, so scroll together, otherwise there will be voids) */
BgposX1-= Bgispeed;
BgposX2-= Bgispeed;

/* Map Size */
Ccsize mapsize = M_bgsprite1->getcontentsize ();

/* When the 1th map leaves the screen completely, let the 2nd map appear completely on the screen, while keeping the 1th map behind the 2nd map */
if (BgposX1 <-MAPSIZE.WIDTH/2) {
bgposX2 = MAPSIZE.WIDTH/2;
bgposX1 = Mapsize.width + mapsize.width/2;
}
/* Similarly, when the 2nd map completely leaves the screen, let the 1th map appear completely on the screen, while keeping the 2nd map behind the 1th map * *
if (BgposX2 <-MAPSIZE.WIDTH/2) {
bgposX1 = MAPSIZE.WIDTH/2;
bgposX2 = Mapsize.width + mapsize.width/2;
}

M_bgsprite1->setpositionx (bgposX1);
M_bgsprite2->setpositionx (bgposX2);

}

Techniques involved in collision detection with gems

Related code:

void Scenea::monstersnock ()
{
for (int i = 0; i < M_monsterarr->count (); i++)
{
Ccsprite *target = (Ccsprite *) M_monsterarr->objectatindex (i);
Ccrect Playerrect = Ccrectmake (
Player->getposition (). x-(Player->getcontentsize (). Width/2),

Player->getposition (). Y-(Player->getcontentsize (). HEIGHT/2),

Player->getcontentsize (). Width,

Player->getcontentsize (). height);
Ccrect Gunchildrect = Ccrectmake (
Gunchild->getposition (). x-(Gunchild->getcontentsize (). Width/2),

Gunchild->getposition (). Y-(Gunchild->getcontentsize (). HEIGHT/2),

Gunchild->getcontentsize (). Width,

Gunchild->getcontentsize (). height);
Ccrect Coard1rect = Ccrectmake (

Target->getposition (). x-(Target->getcontentsize (). Width/2),

Target->getposition (). Y-(Target->getcontentsize (). HEIGHT/2),

Target->getcontentsize (). Width,

Target->getcontentsize (). height);
Bullets hitting Gems
if (Gunchildrect.intersectsrect (Coard1rect))
{
Cocosdenshion::simpleaudioengine::sharedengine ()->playeffect (
"Music/eat.mp3");
ccmoveto* robot1movetoright = ccmoveto::create (2.0f, CCP (100,450));
Target->runaction (Robot1movetoright);
}

Protagonist colliding Gems
if (Playerrect.intersectsrect (Coard1rect))
{
Cocosdenshion::simpleaudioengine::sharedengine ()->playeffect (
"Music/eat.mp3");
M_monsterarr->removeobject (target);
Map->removechild (target);

}
}

Other... Subsequent updates ...

Cocos2dx Crazy Parkour (Crazyrun) Game Project analysis

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.