COCOS2D-JS implementation Double-click the Android Back button to exit the game

Source: Internet
Author: User

I measured the back button of Android, which was not acquired in Cocos2d-js.

(It may be that Cocos2d-js has intercepted Android's fallback, so native Java code is useless),

No way can only use Cocos2d-js's own realization:

One, prepare a back image

Second, add the event to monitor the back button:

//Back Button Press two times to exit the gameCc.eventManager.addListener ({event:cc. Eventlistener.keyboard, onkeyreleased:function(KeyCode, event) {if(KeyCode = = cc.) Key.back) {//Beta version The back of the KeyCode error, you can also change to 6Cc.log ("return button clicked. KeyCode: "+keycode);                That.exitby2click (); }                 Else if(KeyCode = = cc.) Key.menu) {//The beta version of the menu here is keycode wrong and can be changed toCc.log ("menu button clicked. KeyCode: "+keycode); }             }},  This);

Third, add function exitby2click()

isexit:false,//Global Variables
exitlogo:null,//Global Variables
Exitby2click:function(){ if( This. Isexit = =false) { This. Isexit =true;//Ready to Exit //If the return key is not pressed within 2 seconds, the start timer cancels out the task just performed This. Exitlogo =Newcc. Sprite (res.exitlogo_png); This. Exitlogo.setposition (Cc.padd (Cc.visibleRect.bottom, CC.P (0, 120))); This. AddChild ( This. Exitlogo, 10); varArrowaction = Cc.sequence (Cc.fadeout (2), Cc.callfunc (function () { This. Isexit =false; }, This)); This. Exitlogo.runaction (arrowaction); } Else{cc.director.end (); } }

COCOS2D-JS implementation Double-click the Android Back button to exit the game

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.