2

This is what I got so far:

enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"Tile.png"]];
enterName.fontSize = 16.0f;
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
enterName.scale = 0.70f;
[self addChild:enterName z:5];

Basically, it creates a microscopic text field which you can barely click on. The only method that pops up for CCTextField is "textFieldWithSpriteImage."

Bonus help: How to store the string that was typed after enter. Cheers.

2
  • Try changing contentSize
    – CodeSmile
    Commented Mar 11, 2014 at 20:12
  • Thanks. I removed enterName.scale = 0.70f; and added enterName.contentSize = CGSizeMake(100.0f, 50.0f); and it is still microscopic
    – carljalal
    Commented Mar 12, 2014 at 3:24

2 Answers 2

5

In order to prevent microscopic CCTextFields in Cocos2d, make sure to set preferredSize.

Here is the code which prevents a microscopic textfield:

CCSprite *textSprite = [CCSprite spriteWithImageNamed:@"Tile.png"];
enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"Tile.png"]];
enterName.fontSize = 16.0f;
enterName.contentSize = CGSizeMake(100.0f, 50.0f);
enterName.preferredSize = textSprite.contentSize; // don't forget this !
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
[self addChild:enterName z:5];
1

If you are using Cocos2d there are other ways to incorporate text fields. The most convenient way is Spritebuilder, similar to Storyboard. You can edit the font size, and size of text box without using code. Then you just adjust the Textfield so that you can connect it to a variable. To do this you change code connections.

I usually use this as the final option in code connections:

Doc root var: _whatever

Then in MainScene.m (automatically created by Spritebuilder in Xcode) you write this code and add on:

CCTextField * _whatever
//your code

Publish and run and the Textfield should be clickable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.