我希望在我正在构建的游戏开始时提示用户输入他/她的名字。
在 cocos2d 中获取用户输入的最佳方式是什么?
谢谢, 乔伊
最佳答案
Cocos2d 没有任何文本输入控件,但您可以轻松地将 UIKit 控件添加到 Cocos2d 2.0 中的场景
[[CCDirector sharedDirector] view] addSubview:myTextField];
您可以使用嵌入了文本字段的 UIAlertView。
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
要接收从 UIAlertView 返回的事件,您需要实现 UIAlertViewDelegate。在您的头文件中将委托(delegate)协议(protocol)添加到您的接口(interface)
@interface BTMyScene : CCLayer <UIAlertViewDelegate>
然后在您的实现文件中添加您想要接收通知的委托(delegate)协议(protocol)中的任何方法。你可能想要这个
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *name = textField.text;
}
我建议阅读 UIAlertView 的文档和 UIAlertViewDelegate .您将看到所有可用的方法。
关于iphone - 如何使用cocos2d获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297750/