我使用cocos2d菜单为我的游戏创建了配置场景,但我希望能够允许用户输入数值。
菜单非常适合多项选择,但如何为用户添加值框或其他方式输入数值。
答案 0 :(得分:2)
一种方法是在cocos2d场景中使用UIKit元素(是的,你可以混合它们)。特别是,您可以在游戏层中使用UITextField:
@interface MyLayer : CCLayer<UITextFieldDelegate> {
UITextField *myTextField;
}
然后在MyLayer.m的init函数中:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(..,..,..,..)];
myTextField.delegate = self; //set the delegate of the textfield to this layer
[myTextField becomeFirstResponder];
// Configure some of your textfield properties as appropriate...
[myTextField.keyboardType = UIKeyboardTypeDefault;
[myTextField.returnKeyType = UIReturnKeyDone;
[myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[myTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[[[CCDirector sharedDirector] openGLView] addSubview: myTextField];
然后,您可以通过执行以下操作来获取用户完成后的文本字段值:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSString* myValue = myTextField.text; // get the value and do something
return YES;
}