-1

cocos2dのメニューを使ってゲームの設定シーンを作ったのですが、ユーザーが数値を入力できるようにしたいです。

メニューは複数の選択肢がある場合に最適ですが、ユーザーが数値を入力するための値ボックスやその他の方法を追加するにはどうすればよいですか。

4

1 に答える 1

2

1 つの方法は、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;
}
于 2012-10-07T03:47:18.033 に答える