Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a UITextField on SpriteKit

I want to implement a Textfield to input your name on my SpriteKit Game, so I have something like this:

SKLabelNode* gameTitle = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
gameTitle.text = @"Game Title";
gameTitle.name = @"gametitle";
gameTitle.fontSize = 44.0;
gameTitle.fontColor = [SKColor blackColor];
gameTitle.position = CGPointMake(self.size.width/2,self.size.height/2 + 150);
[self addChild:gameTitle];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = @"Enter your name here";
textField.backgroundColor = [SKColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;

[self.view addSubview:textField];

so, the problem is that it's not showed on my scene, maybe the problem is when adding it into the scene, I use addSubview.

Is that wrong?

like image 452
Vergmort Avatar asked Oct 29 '13 12:10

Vergmort


1 Answers

You can add objects that inherits UIView's inside the didMoveToView:(SKView *)view function

Just add that function to your SKScene and move your UITextField inside:

-(void)didMoveToView:(SKView *)view {
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
    textField.center = self.view.center;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textColor = [UIColor blackColor];
    textField.font = [UIFont systemFontOfSize:17.0];
    textField.placeholder = @"Enter your name here";
    textField.backgroundColor = [UIColor whiteColor];
    textField.autocorrectionType = UITextAutocorrectionTypeYes;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.delegate = self.delegate;
    [self.view addSubview:textField];
}
like image 146
John Riselvato Avatar answered Sep 30 '22 19:09

John Riselvato