Cocos3.0 android return key function implementation, cocos3.0android

Source: Internet
Author: User

Cocos3.0 android return key function implementation, cocos3.0android

Example: Game. h Game. cpp

The header file Game. h is defined as follows:

Void onKeyReleased (EventKeyboard: KeyCode keyCode, Event * pEvent );

Implementation in the Game. cpp file:

Auto listener = EventListenerKeyboard: create (); // bind to the callback function

Listener-> onKeyReleased = CC_CALLBACK_2 (Game: OnKeyReleased, this); // Add it to the event distributor.

Director: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this );

// The Return key exits.

Void Game: onKeyReleased (EventKeyboard: KeyCode keyCode, Event * pEvent)
{
Director: getInstance ()-> end ();
}


// Note that this method only supports compiling on Android and windows. We recommend that you use this method to cancel compiling on windows.

// You can exit by clicking the return key only once. If you need to exit twice, you can use the following method:


Void Game: onKeyReleased (EventKeyboard: KeyCode keyCode, Event * pEvent)

{// Convert the keycode enumeration value to 6 for the Return key value of the int mobile phone
Auto x = (int) _ keyCode;
// If you click the return key once
If (x = 6)
{
// The variable that records the number of clicks. It is declared in the header file and assigned a value of 0.
Keydown ++;
// Exit the game if you click twice
If (keydown = 2)
{
Director: getInstance ()-> end ();
}
// Otherwise, the text is displayed, and "exit once again"
Else
{
// An added genie image is the displayed "quit the game again"
Auto label1 = Sprite: create ("again.png ");
AddChild (label1 );
// Add a disappear animation and a callback function to the text.
// After the animation disappears, the callback function deletes the Node and assigns the keydown value 0.
// If the animation does not disappear and the Return key is clicked, the game is directly exited.
// The interval between two click-back keys is 0.1 + 1.5 = 1.6. You can set this time by yourself.
Auto fadeout = FadeOut: create (0.1f );
Label1-> runAction (Sequence: create (DelayTime: create (1.5f), fadeout, CallFunc: create (CC_CALLBACK_0 (againExit: oncallback, this, label1 )), NULL ));
}
}

}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.