Normally if you wanted a text field you would use the interface editor, but in cocos2d that seems to be not an option. How would I include a text entry field (so that I can use the OS standard keyboard) in one of my scenes?
Using the built in keyboard/text fields
(11 posts) (7 voices)-
Posted 2 years ago #
-
There are already topics discussing this issue, I suggest searching for it...
Basically, the idea is to combine UIKit with Cocos2d, through UIKitLayer... adding the UITextField directly to the core OpenGLView...
~ Natanavra.
Posted 2 years ago # -
A fast way to do it, is to create a textarea and set it to the first responder.
What I'm doing for littlephysics, isn't neatest way - but I just catch all the calls to it (it fires an event when it changes), and set the text for whatever I want using that info.Here's what I do,
self.currentTextView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 0, -100)] autorelease]; currentTextView.delegate = self; currentTextView.text = currentCategory.name; currentTextView.keyboardType = UIKeyboardTypeAlphabet; [currentTextView becomeFirstResponder]; [self.view addSubview:self.currentTextView];and I catch it here
// Changes the name of the scene, checks for returnkey to close - (void)textViewDidChange:(UITextView *)textView { // Return was pressed - save and close if([textView.text rangeOfString:@"\n"].length > 0) { NSError *error; if (![managedObjectContext save:&error]) NSLog(@"[ERROR] @ saving name"); [self hideKeyboard]; [self adjustSegmentsToMatchState]; [self unselectAllSegmentedControls]; return; } [currentCell setText:[[textView text] copy]]; [currentCategory setName:[[textView text] copy]]; }It came out like this: - I'm really just wrapping the calls, and using the text for whatever I want. You can't see it - but there's a cocos2d gamescene under there.

That should get you startedPosted 2 years ago # -
Where do you put that code?
Posted 2 years ago # -
I did it using a text alert view with a text field:
-(void) EnterName: (id)sender{ [[PASoundMgr sharedSoundManager] play:@"click"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name" message:@"Enter up to 5 names per player" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alert addTextFieldWithValue:@"" label:nil]; [alert textField].keyboardType = UIKeyboardTypeDefault; [alert textField].autocorrectionType = UITextAutocorrectionTypeNo; [alert textField].clearButtonMode = UITextFieldViewModeAlways; //[alert textField].text = @"hi"; [alert textField].delegate = self; alert.tag = 1; [alert show]; [alert release]; } - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch(buttonIndex) { case 0: [GlobalVariableManager sharedVariableManager].userAddedName = nil; break; case 1: [GlobalVariableManager sharedVariableManager].userAddedName = [actionSheet textField].text; [list addObject:[actionSheet textField].text]; NSLog(@"user name counter: %d", userNameCounter); NSLog(@"string: %@", [list objectAtIndex:userNameCounter]); userNameCounter++; NSLog(@"user name counter: %d", userNameCounter); if(userNameCounter == ([GlobalVariableManager sharedVariableManager].numPlayers)*5){ disabledItem.isEnabled = NO; disabledItem2.isEnabled = NO; disabledItem3.isEnabled = YES; [label setString:@"maximum number of"]; [label2 setString:@"names entered"]; [label3 setString:@"for this session"]; [yesLabel setString:@" "]; [noLabel setString:@" "]; [goLabel setString:@"continue"]; } break; default: break; } }It'll look like this
Hope it helpsPosted 2 years ago # -
The important thing when using UIKit stuff with cocos2d is to add the subview like this:
[[[[Director sharedDirector] openGLView] window] addSubview:textView];
instead of something like this:
[self.view addSubview:textView];Other than that, you can pretty much treat it like you would in any normal cocoa touch app.
Posted 2 years ago # -
@Ripps suggestion:
Be careful!
I've heard of at least two apps that were rejected because of the use of the undocumented addTextFieldWithValue method. Apple does not like that.
Posted 2 years ago # -
Obiwahn, thanks for the info, did not know that. I used this method on four different applications for name entry and what not, and they've all been approved (within the last couple of months). I wonder if I got lucky or they've relaxed a little?
Posted 2 years ago # -
@Ripp - it's all over the place whether they will accept it or not, and they may decide not to approve it the next time you update. All depends on who looks at the app. The safe thing to do is to subclass uialertview and add a text field yourself, which isn't hard to do. In fact, someone has already done it and shares it freely:
http://discussions.apple.com/thread.jspa?messageID=8061479 (toward the bottom of the page)Posted 2 years ago # -
@DetectiveCakes, thanks for the link, I'll definitely try that next time. I been a victim of the random aprovals/rejections from the reviewers, so if I can minimize the chance of a rejection I will.
Posted 2 years ago # -
http://stackoverflow.com/questions/693483/textfield-example-in-cocos2d
This was more helpful to me than anything I could find on this topic in these forums (not often that I have to hunt elsewhere for answers relating to Cocos2d). Figured I should share.
Posted 4 months ago #
Reply
You must log in to post.