"Cocos2d-x Game development Practical Solution" learning notes in cocos2d display images

Source: Internet
Author: User

The images in Cocos2d-x are displayed through the Sprite class. Each character, monster, prop in the Cocos2d-x game can be understood as a genie, the game background as a special unit to understand it as a genie is nothing wrong. In the source file in this chapter directory of the project ChapterThree03 shows the use of Cocos2d-x to implement a simple game start interface method, mainly through the Sprite class to display the image, the key code as shown in example 3-5 .

"Example 3-5 display image in cocos2d-x"

Size size = director::getinstance ()->getvisiblesize ();       // Get screen Size//Background image auto* background = sprite::create ("background.jpg");         //Create a Sprite class with a background image of Background.jpgbackground->setposition (SIZE.WIDTH/2,SIZE.HEIGHT/2);         //to center the background image Background->setscale (0.7f);                                   //let the background picture shrink size addchild (background,0);                                       // Add a background picture to the scene//three buttons on the right side of the screen auto* button1 = sprite::create ("Button.png");          The image of the       //button is button.pngbutton1->setposition (550, size.height /  2);                  //position of the first button Button1->setscale (0.6f);                                      //Set Button size Addchild (button1,1);                                          // Add the first button to the scene auto* button2 = sprite::create ("Button.png");                //second button button2->setposition (550, size.height  / 2-60); Button2->setscale (0.6f); AddChild (button2,1);                                          //adds a second button to the scene auto* button3 = sprite::create (" Button.png ");                // The third button button3->setposition (550, size.height / 2 - 120); Button3->setscale (0.6f); AddChild (button3,1);                                          //Add a third button to the scene//left side of the antique character auto* renwu = sprite::create (" Renwu.png ");                    //Create Elf classes, peopleMaterials stored in Renwu.png renwu->setposition (0,0);                                       //Set People position Renwu->setanchorpoint (VEC2 (0,0));                             //Set Genie Anchor Point Renwu->setscale (0.5f); AddChild (renwu,2);                                              //adding characters to the scene

the effect after running 3-5 is shown.

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2016/0216/204909_mpHP_2633993.png "alt=" 204909 _mphp_2633993.png "/>

Figure 3-5 The game start interface implemented using the Sprite class

can go to the project's The Resources directory looks at the footage 3-6 used to view the example . A total of 3 images are used as background images of the background.jpg, button picture button.png and left character picture Renwu.png. It can be seen that the interface shown in Figure 3-5 is actually the result of these 3 pieces of material stacked together in a certain pattern. The Paradigm 3-5 is just one such feature.

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2016/0216/204909_sOZE_2633993.png "alt=" 204909 _soze_2633993.png "/>

Figure 3-6 The footage used in sample 3-5

in The example 3-5, first gets the size of the current screen stored in the variable size , and then creates an object background of the Sprite class , where The parameter of the Create method is the file name of the footage you want, as shown in the example in line. The next thing to do is to use the AddChild method to add the sprite object to the scene, as shown in the Example section .

    But in this example, in addition to showing the picture, but also to achieve changes in the position of the picture, as well as zooming and other functions, so also to see the 04 The content at the line. Section 04 line use setposition method sets the location of the background picture , by default, Cocos2d-x setposition y Two shifts in the direction.

    As for the 05 line in use setscale< Span style= "font-family: ' The song Body '; > is to use a float 640*360

    08~19 line is using a similar approach to 3 buttons are added to the scene, and a certain amount of displacement and scaling is set. Section 21~25 setanchorpoint< Span style= "font-family: ' The song Body '; > method. The function of this method is to set the anchor point of the current object, as shown in section 23 setposition

recommend a school Cocos2d-x the latest version ofThe new book, "Cocos2d-x game development Real Solution", Tsinghua Publishing, will be a boutique!

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2016/0216/205627_Jv6H_2633993.png "alt=" 205627 _jv6h_2633993.png "/>


"Cocos2d-x Game development Practical Solution" learning notes in cocos2d display images

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.