Cocos2d-x-3.0 alpha1 with C ++ 11 exercise 9: dart ninja, add launch dart qty text & score text

Source: Internet
Author: User

1. Create a UI using cocostudio, create a UI, and add a LabelAtlas and a LabelBMFont respectively. Export the project, save it as cocos2d3sampleui_playScene.ExportJson, and add it to the Resource Directory of xcode together with the related Resource files. You may need to pay attention to this. But when adding resources, if no directory is used in the code, select "Create folder reference for any added folders" when adding resources ", in this way, a blue group will be created in the project, rather than a real sub-directory. 2. coding is implemented in HelloWorldScene. h: add the relevant variable declaration, // number of emission int numShootProjectile; UILabelAtlas * shootNumLabelAtlas; UILabelBMFont * scoreLabelbMFont; the variable name must be consistent with the control name set in UIEditor for encoding. In HelloWorldScene. add the following code before returning in init in cpp: UILayer * layer = UILayer: create (); auto widget = dynamic_cast <UIWidget *> (cocostudio: GUIReader :: shareReader ()-> widgetFromJsonFile ("cocos2d3sampleui_playScene.ExportJson"); widget-> setSize (Size (480,320); layer-> addWidget (widget); this-> addChild (layer ); this-> shootNumLabelAtlas = dynamic_cast <UILabelAtlas *> (widget-> getChildByName ("shootNumLabelAtlas"); t His-> shootNumLabelAtlas-> setStringValue ("0"); this-> shootNumLabelAtlas-> setPosition (Point (50, winSize. height-50); this-> scoreLabelbMFont = dynamic_cast <comment *> (widget-> getChildByName ("scoreLabelbMFont"); this-> scoreLabelbMFont-> setPosition (Point (winSize. width-100, winSize. height-50); this-> scoreLabelbMFont-> setText ("Score: 0"); numShootProjectile = 0; after the two controls are loaded, the center is automatically placed in the vertical direction. Why? Take the name of the control and save the reference to the variable. It should be noted that the variables declared in c ++ must be initialized. For example, if numShootProjectile = 0 is not initialized, the variable may be any value. In onTouchesEnded, after launching a dart, add 1 to the number and set a number in the text box. The Code is as follows: numShootProjectile ++; char numShootStr [20]; sprintf (numShootStr, "% d", numShootProjectile); this-> shootNumLabelAtlas-> setStringValue (numShootStr ); in c ++, you can use std: atoi to convert string to int, but in turn, itoa is not supported and provided by all compilers. This function (itoa) is not defined in ANSI-C and is not part of C ++, but is supported by some compilers. in update, add the code for updating the Score: char scoreStr [20]; sprintf (scoreStr, "Score: % d", targetDestroyed ); this-> scoreLabelbMFont-> setText (scoreStr );

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.