Wednesday, December 12, 2012

Textfield on cocos2d Mac

Surprisingly adding a textfield in cocos2d Mac isn't as simple as in cocos2d iOS. There are various suggestions by other coders on how to do this.

This sample code tells you one of the several ways to deal with the textfield in cocos2d Mac.
HERE is the download link.

In this sample code, I have created an external window which opens up when the user hits the textfield.
Here is the UserInterface of nib file. (TextFieldView.xib).

We have following connections on this window.

When I run the app, my window looks like following with textfield which is actually a touch responsive CCMenuItemImage and on top of it sits a CCLabelTTF which says Enter UserName. This string"Enter UserName" will be changed to whatever user enters in the Textfield popup.

When I click on the Enter Username with my mouse, it opens the popup with the textfield. Following code is executed to make the window pop up:

 TextfieldProjectAppDelegate* appDelegate = (TextfieldProjectAppDelegate*)[[NSApplication sharedApplication] delegate];

 tView = [[TextFieldView alloc] initWithWindowNibName:@"TextFieldView"];
 assert ([appDelegate window]);
 assert ([tView window]);
 [NSApp beginSheet:[tView window]
       modalForWindow:[appDelegate window]

We also need to define a selector which is where the control comes back when the popup is gone, hence we implement the following:

- (void)usernameSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
    [sheet orderOut:nil];

    NSLog(@"name == %@", tView.username);

    //Need to update text on textfield
    CCLabelTTF *userName=(CCLabelTTF*)[self getChildByTag:106];

When you are done entering the value in textfield popup, just hit the OK button. When you do that following method(implemented in TextFieldView.m ) is executed. This method makes the popup go away. This is the selector method attached with OK button. You can view the connections in the Xib Interface Window in Xcode (already described above).

- (IBAction)usernameEntered:(id)sender {

     username=[nameField stringValue];
    [NSApp endSheet:self.window];


  1. Good ideas with professionals tips thank you so much!! looking for more such good ideas!!
    visit here

  2. Wonderful blog! I truly love how it’s easy on my eyes as well as the info are well written. I am wondering how I may be notified whenever a new post has been made.
    Global education centre

  3. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.

    Android App Development Company
    iOS App Development Company

  4. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

  5. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India


  6. Nice blog..! I really loved reading through this article. Thanks for sharing such a
    amazing post with us and keep blogging... iot training in chennai | iot training in chennai quora | iot training and placement in chennai | iot training center in chennai | best iot training centre in chennai

  7. Nice blog..! I really loved reading through this article. Thanks for sharing such a
    amazing post with us and keep blogging... best angularjs training institute in chennai | angularjs training in omr | angularjs best training center in chennai | angularjs training in chennai