iphone - 如何使用cocos2d获取用户输入

标签 iphone objective-c cocos2d-iphone

我希望在我正在构建的游戏开始时提示用户输入他/她的名字。

在 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/

相关文章:

iphone - 如何在 iPhone 的 View 顶部显示弹出窗口

objective-c - 如何在 tableView :viewForFooterInSection 中添加 UIButton 或 UISwitch

objective-c - Cocos2d 中内存管理的推荐限制?

uiviewcontroller - 如何在基于 UIKit 的 App 中的 UIViewController 中加载 Cocos2d 动画?

iphone - 使用 CCMenuAdvanced - Cocos2d

iphone - 可以在 PhoneGap 和 native 代码之间共享 SQLite 数据库吗?

ios - How to solve ""_OBJC_CLASS_$_FIRApp”,引用自“with Firebase in Xcode

objective-c - 无法从 Objective-C++ 代码调用 swift 函数

ios - 在 UITableView 中默认重新加载后如何记住选择的行并维护它们?

ios - tableView reloadData/reloadRowsAtIndexPaths 导致滚动闪烁