Cocos2d-x frame loop is not rigorous, causing scene switching to lag

Source: Internet
Author: User
Tags addchild

Recently in the use of cocos2d-x boot interface, 2dx version is 2.2.3, scene switching with the effect, all resources have been used texturepacker package preload, but in the actual running debugging, scenario switching is rather choppy.

After a variety of confusions, the frame rate printing (setdisplaystats (true) is unintentionally released, and a serious problem is found that the number of objects has been increasing, resulting in a continuously decreasing frame rate, by the time the scenario is switched, it will be completely unable to run. After carefully checking the code, I found that in the scenario frame loop (Update (), I monitored the network status of the mobile phone, removed some genie according to the network status, and then created and displayed some genie, the problem lies here.

Void skyguidesecondpage: Update (float delta) {char wlan_state [16] = {0}; property_get ("coocaa. wlan_state ", wlan_state," 0 "); int wlanstate = atoi (this-> guideinfo. wlan_state); If (wlanstate = 1) {This-> removechildbytag (214); ccsprite * sp_connecting = ccsprite: createwithspriteframe (ccspriteframecache: equals () -> spriteframebyname ("guide_connecting_ttf"); sp_connecting-> setanchorpoin T (CCP (0, 1); sp_connecting-> setposition (CCP (1640,175 0); sp_connecting-> setscale (1.5f); this-> addchild (sp_connecting ); // If the wlanstate is equal to 1 at the beginning of a certain time point, a sp_connecting genie will be created for each frame starting from this frame, and the frame is cyclically 40 times per second, then, 40 identical genie are created every second, and all the consumption is increased. When switching the scenario, it is definitely stuck. Therefore, you need to add a status judgment here, ensure that sp_connecting is created only once .}

 

Modified code:

Void skyguidesecondpage: Update (float delta) {char wlan_state [16] = {0}; property_get ("coocaa. wlan_state ", wlan_state," 0 "); int wlanstate = atoi (this-> guideinfo. wlan_state); If (wlanstate = 1) {If (this-> connectflag = 0) // The initial status is 0, and the status judgment is {This-> connectflag = 1; // set the status to 1 so that the next frame cannot be entered. The genie sp_connecting will not be created again with this-> removechildbytag (214); ccsprite * sp_connecting = ccsprite :: createwithspriteframe (ccspriteframecache: sharedspriteframecache ()-> spriteframebyname ("ing"); sp_connecting-> setanchorpoint (CCP (0, 1 )); sp_connecting-> setposition (CCP (500,400); sp_connecting-> setscale (1.5f); this-> addchild (sp_connecting );}}

 

Cocos2d-x frame loop is not rigorous, causing scene switching to lag

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.