2013-10-29 6 views
6

Ich möchte ein Textfeld zur Eingabe Ihres Namens auf meinem SpriteKit Spiel implementieren, so habe ich so etwas wie dieses:Wie ein UITextField auf SpriteKit erstellen

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, das Problem ist, dass es nicht zeigte auf meinem Szene, vielleicht ist das Problem, wenn ich es in die Szene hinzufüge, verwende ich addSubview.

Ist das falsch?

+0

versuchen, eine UITextView – LearnCocos2D

+0

@ LearnCocos2D mit immer noch die gleichen, es scheint nicht:/ – Vergmort

Antwort

15

können Sie Objekte hinzufügen, die UIView die innerhalb der didMoveToView:(SKView *)view Funktion

Fügen Sie einfach diese Funktion zu Ihrem SKScene erbt und Ihre UITextField innen bewegen:

-(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]; 
} 
+1

Große und effektivere Lösung. Vielen Dank! – Vergmort

+1

Danke für die Antwort. Wenn ich zu einer anderen Szene wechsel, bleibt das TextField in der Ansicht. Muss ich es manuell entfernen? – palme

+2

leider wirst du, @palme –

1

entfernen sie die

[myTextField removeFromSuperView]; 
verwenden

Wenn Sie nicht die Methode didMoveToView verwenden möchten, die das textField in die Szene presenta einfügt tion, können Sie diese Methode verwenden:

-(void)insertUI{ 

ttaAppDelegate *myDel = [[UIApplication sharedApplication] delegate]; 
UIViewController *vc = myDel.window.rootViewController; 
self.textField= [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)]; 
self.textField.center = self.view.center; 
self.textField.borderStyle = UITextBorderStyleRoundedRect; 
self.textField.textColor = [UIColor blackColor]; 
self.textField.font = [UIFont systemFontOfSize:17.0]; 
self.textField.placeholder = @"Enter your name here"; 
self.textField.backgroundColor = [UIColor whiteColor]; 
self.textField.autocorrectionType = UITextAutocorrectionTypeYes; 
self.textField.keyboardType = UIKeyboardTypeDefault; 
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
self.textField.delegate = self; 
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[self.button addTarget:self action:@selector(saveScore:) forControlEvents:UIControlEventTouchDown]; 
[self.button setTitle:@"Save" forState:UIControlStateNormal]; 
self.button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); 

[vc.view addSubview:self.textField]; 
[vc.view addSubview:self.button]; 
vc.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual; 
[vc requestInterstitialAdPresentation]; 
}