Kalzem
Kalzem

Reputation: 7492

How to use CCTextField or add UITextField on cocos2d v2

I am currently trying to permanently show up a TextField on a cocos2d v2 layer.

I am using this CCTextField because it looks easy to use : https://github.com/iNinja/CCTextField

The plugin is kind of old for cocos2d v2 so I updated the deprecated methods, but I still cannot show up any text field ...

How am I suppose to show a Text Field with cocos2d v2 (any means will do as long as it is clear enough and easy to use).

By the way, I looked on Google and came accross 2-3 answers on stackoverflow and forums. However, they are for cocos2d v1 (or earlier) and do not work at all (or are not clear enough).

EDIT

This is what I have in the onEnter function of my class

 UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(200, 200, 150, 50)];
tf.delegate = self;
tf.textAlignment = UITextAlignmentLeft;

[[[CCDirector sharedDirector] view] addSubview:tf];

And this is the .h

@interface AdventureLayer : CCLayer <UITextFieldDelegate>

Nothing shows up anywhere ...

EDIT 2

Hahaha ! Actually the TextField is INVISIBLE ! A placeholder is therefore very usefull

 tf.placeholder = @"FOOBAR";

Upvotes: 0

Views: 3941

Answers (1)

Ben Trengrove
Ben Trengrove

Reputation: 8729

Cocos2d 2.0 makes it very easy to add UIKit elements as CCDirector is actually a UIViewController.

Create a UITextField in code and then just do the following

 UITextField *aTextField = ...
 [[CCDirector sharedDirector] view] addSubview:aTextField];

Remember that its coordinate system starts from top left not bottom left like cocos2d.

Upvotes: 7

Related Questions