Basic elements of cocos2dx games

Source: Internet
Author: User


3.1 ccdirector: General Manager

Bool appdelegate: applicationdidfinishlaunching ()? {

  • ? & Nbsp; // initialize the director class

  • ? & Nbsp; ccdirector * pdirector = ccdirector: shareddirector ();

  • ? & Nbsp; pdirector-]> setopenglview (& cceglview: Export dopenglview ());

  • ? & Nbsp; // resource management for high-resolution screens (such as retina screens)

  • ? & Nbsp; // pdirector-]> enableretinadisplay (true );

  • ? & Nbsp; // enable FPS display

  • ? & Nbsp; pdirector-]> setdisplaystats (true );

  • ? & Nbsp; // sets the FPS ceiling. If this parameter is not set, the default FPS limit is 60.

  • ? & Nbsp; pdirector-]> setanimationinterval (1.0/60 );

  • ? & Nbsp; // create a scenario. The scenario is an autorelease object.

  • ? & Nbsp; ccscene * pscene = helloworld: Scene ();

  • ? & Nbsp; // running scenario

  • ? & Nbsp; pdirector-]> runwithscene (pscene );

  •  

? & Nbsp; return true;

?} The work of ccdirector is indeed very similar to that of the Director, mainly responsible for the following work. Game rendering settings, including setting the game rendering window, FPS display, default Frame Rate upper limit, texture color width, and so on. Switch the current game scenario to pause or resume the running of the game scenario.
  • All in all, the game has completed rendering settings and process control under the management of ccdirector.

  • ? & Nbsp; ccdirector plays the role of the Global Manager, and naturally adopts the singleton design mode. Anywhere in the program, you can access it through the following simple code:

  • ? & Nbsp; ccdirector * pdirector = ccdirector: shareddirector ();

  • ? & Nbsp; In ccdirector, we define the methods for the following management scenarios.

  • ? & Nbsp; runwithscene (ccscene * scene): starts the game and runs the scene scenario. This method is called when the main program starts for the first time.

  • ? & Nbsp; replacescene (ccscene * scene): directly replaces the current scene with the input scene to switch the screen. The current scene will be released. This is the most common method for switching scenarios.

  • ? & Nbsp; pushscene (ccscene * scene): Pause the currently running scenario and press it into the generation execution scenario stack. Then, set the passed-in scene to the current running scenario.

  • ? & Nbsp; popscene: releases the current scenario, then pops up the stack top scenario from the generation execution scenario stack, and sets it to the current running scenario. If the stack is empty, the application is directly ended. It can be used in pairs with pushscene, as shown in the following figure: Enter the settings page on the main interface, and then return to the main interface.

  • Pause: Pause all timers and actions in the current running scenario, and the scenario will still be displayed on the screen.

  • ? & Nbsp; resume: resume the timer and action paused in the current running scenario. It works with pause.

  • ? & Nbsp; end: end the scenario and exit the application at the same time.

? & Nbsp; note that the above three methods (replacescene, pushscene, and popscene) are used to completely load the scenario to be switched, to release the current running scenario. Therefore, when the new scenario is fully loaded, there are two scenarios in the system at the same time, which will be a test of internal storage. If you do not pay attention to it, switching scenarios may cause insufficient memory. 3.2 ccscene: Scenario 3.3 cclayer: Layer
  • Void addchild (ccnode * child );

  • ? & Nbsp; void addchild (ccnode * child, int zorder );

  • ? & Nbsp; void addchild (ccnode * child, int zorder, int tag );
  • The child parameter is the node to be added. For scenarios, we usually add nodes as layers. The added layer is placed under the added layer. If you want to specify the order for them, you can use different zorder values. zorder represents the order of the elements under the node. A larger value indicates a higher display order. The default value of zorder is 0. A tag is the identification number of an element. If you set a tag value for a child node, you can use the tag value to locate it in its parent node. Here we can select the method we need to add layers to the scenario. Another important feature of cclayer is to accept user input events, including touch, accelerometer, and keyboard input. The members related to user input events in cclayer are shown in Table 3-1.
Table 3-1 Member tables related to input events in cclayer

? Member type

? Name

? Description

? Attribute

? Touchenabled

? Obtain or set whether to accept touch events

?

? Accelerometerenabled

? Obtain or set whether to accept accelerometer events

?

? Keypadenabled

? Obtain or set whether to enable keyboard input.

? Callback Function

? Cctouchbegan

? Callback functions for touch events with targets

?

? Cctouchmoved

?

?

? Cctouchended

?

?

? Cctouchcancelled

?

?

? Cctouchesbegan

? Callback functions of standard touch events

?

? Cctouchesmoved

?

?

? Cctouchesended

?

?

? Cctouchescancelled

?

?

? Registerwithtouchdispatcher

? Registers the callback function of a touch event,
? Set the touch type to be registered in this function

?

? Didaccelerate

? Callback Function for an accelerometer change event

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.