【问题标题】:Textfield in cocos2d/kobold2d not showing colorcocos2d/kobold2d 中的文本字段不显示颜色
【发布时间】:2013-06-27 06:25:44
【问题描述】:

我需要创建一个文本字段,我的游戏中的玩家可以在其中输入他们的名字以获取高分。我关注了这个问题:UITextField Example in Cocos2d,并成功地用键盘创建了一个文本字段。但是,文本字段的文本是白色的,背景颜色也是白色的,所以我看不到文本字段,只有键盘。

代码如下:

appdelegate.h

#import "KKAppDelegate.h"

@interface AppDelegate : KKAppDelegate
{
  UITextField *nameTextField;

}

-(void)specifyPlayerName;

@end

appdelegate.m

@implementation AppDelegate

-(void) initializationComplete
{

#ifdef KK_ARC_ENABLED
CCLOG(@"ARC is enabled");
#else
CCLOG(@"ARC is either not available or not enabled");
#endif
}

 - (void)specifyPlayerName
{
NSLog(@"called");

nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)];
[nameTextField setDelegate:self];
[nameTextField setText:@""];
[nameTextField setTextColor: [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]];
[[[[CCDirector sharedDirector] openGLView] window] addSubview:nameTextField];
[nameTextField becomeFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
//Terminate editing
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField*)textField {
if (textField==nameTextField) {
    [nameTextField endEditing:YES];

    // here is where you should do something with the data they entered

    GameData* data = [GameData sharedData];
    data.playerName = nameTextField.text;
    NSLog(@"entered: %@", data.playerName);
}
}

正在从另一个类/按钮调用该方法

    [[[UIApplication sharedApplication] delegate]performSelector:@selector(specifyPlayerName)];

我尝试设置文本字段的颜色,但它仍然显示为全白。任何帮助,将不胜感激。

【问题讨论】:

    标签: colors cocos2d-iphone textfield kobold2d


    【解决方案1】:

    我发现文本字段隐藏在另一个视图后面,所以

      [[[CCDirector sharedDirector] openGLView] addSubview:nameTextField];
    

    而不是

    [[[[CCDirector sharedDirector] openGLView] window] addSubview:nameTextField];
    

    文本字段的颜色默认为白底黑字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2020-12-15
      • 2017-09-05
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多