How to get user input using cocos2d

I want to prompt the user to enter his name at the beginning of the game that I am creating.

What is the best way to get user input in cocos2d?

Thanks Joey

+4
source share
1 answer

Cocos2d doesn't have text controls, but you can easily add UIKit controls to the scene in Cocos2d 2.0

[[CCDirector sharedDirector] view] addSubview:myTextField]; 

You can use UIAlertView with an embedded text box.

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; //[alert release]; If not using ARC 

To receive events from a UIAlertView, you implement a UIAlertViewDelegate . In your header file, add the delegate protocol to your interface

 @interface BTMyScene : CCLayer <UIAlertViewDelegate> 

Then, in your implementation file, add any of the methods from the delegate protocol for which you want to receive notifications. You probably need this

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { UITextField *textField = [alertView textFieldAtIndex:0]; NSString *name = textField.text; } 

I recommend reading the documentation for UIAlertView and UIAlertViewDelegate . You will see all the available methods that you can use.

+8
source

Source: https://habr.com/ru/post/1444940/


All Articles