Cocos2dx practice-learning experience in the demo "Lori run"

Source: Internet
Author: User

[Nagging]

For the source code tutorial, go:Http://blog.csdn.net/iamlazybone/article/details/19612941

Thanks to lazy bones for providing so many demo tutorials, it is really helpful for beginners.

Note:This section only records the experiences of bloggers who have learned "Lolita fast run.


[Game]

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/ AB /wKioL1QKfB-gpzDFAAGnf6DDQyM688.jpg "Title =" 20131202205746203.jpg" alt = "wKioL1QKfB-gpzDFAAGnf6DDQyM688.jpg"/>



[Learning experience]


1. layer-based game main scenarios

If you write all the elements in a game class, it will be confusing and difficult to modify.

Therefore, game elements should be layered, such as the background layer, character layer, monster layer, and item layer.

Then, all layers are put into the game scenario and combined into a complete game.

(1) character layer: gamehero. h

(2) item layer: gamestar. h

(3) score layer: gamemark. h

(4) map layer: gamemap. h


2. Change the ccmenuitemimage image.

// SP-> setnormalimage (ccsprite: Create ("sound-off-A.png"); // normal image SP-> setselectedimage (ccsprite: Create ("sound-off-B.png ")); // image when selected //


3. Change the genie background

// Jump = cctexturecache: sharedtexturecache ()-> addimage ("s_jump.png"); // jump luoli-> settexture (jump );//


4. Running Animation

Use animation/animate to animated the action.

// Ccanimation * anrun = ccanimation: Create (); anrun-> addspriteframewithfilename ("s_1.png"); anrun-> addspriteframewithfilename ("s_2.png "); anrun-> parameters ("s_3.png"); anrun-> addspriteframewithfilename ("s_4.png"); anrun-> parameters ("s_5.png"); anrun-> addspriteframewithfilename ("s_6.png "); anrun-> invoke ("s_7.png"); anrun-> addspriteframewithfilename ("s_8.png"); anrun-> setdelayperunit (0.1f); // anrun-> setrestoreoriginalframe (true ); // whether to return the initial frame ccrepeatforever * acrun = ccrepeatforever: Create (ccanimate: Create (anrun); luoli-> runaction (acrun );//


5. Coordinates of SP elements on the map layer in Game

Assume that both Bg1 and SP are anchorpoint (0.5, 0.5) with the center as the anchorpoint and SP as the child node of Bg1.

To know the coordinates of the SP element on the screen, you need to know the coordinates of Bg1 on the screen and the coordinates of SP relative to Bg1.

Because the setposition set by SP isCoordinates relative to the parent node Bg1, not the actual coordinates on the screen.

/// 1. obtain the coordinates of the map layer. P1 is the coordinate ccpoint p1 = gamemap-> getposition (); // 2. obtain the SP coordinate. PSP is the coordinate ccpoint PSP = (gamemap-> SP)-> getposition (); // 3. calculates the coordinate ccpoint Pt = CCP (p1.x-gamemap-> getcontentsize () in the game (). width/2) + PSP. x, (p2.y-gamemap-> getcontentsize (). height/2) + PSP. Y );//


6. Rolling background

Use two background images to scroll from right to left. When the screen is exceeded, you can set a new position to continue scrolling.

Screen Size:480x320.

/// Background 1bg1 = ccsprite: Create ("back_1.png"); Bg1-> setposition (CCP (240,160); this-> addchild (Bg1 ); // background 2bg2 = ccsprite: Create ("back_1.png"); bg2-> setposition (CCP (720,160); this-> addchild (bg2); void gamemap :: update (float DT) {// move two pixels to the left (Bg1-> setposition (Bg1-> getposition ()-CCP (2, 0 )); bg2-> setposition (bg2-> getposition ()-CCP (2, 0); // beyond the screen, set the coordinates, and re-scroll if (Bg1-> getpositionx () <=-240) {Bg1-> setposition (CCP (720,160); // reload the resetstar (star1) on the background;} If (bg2-> getpositionx () <=-240) {bg2-> setposition (CCP (720,160); // reload the resetstar (star2) on the background );}}//



This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1549635

Cocos2dx practice-learning experience in the demo "Lori run"

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.