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.