2013-10-29 9 views
6

Voglio realizzare un Textfield per inserire il tuo nome sul mio SpriteKit gioco, in modo da avere qualcosa di simile:Come creare un UITextField su SpriteKit

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]; 

così, il problema è che non è mostrato sul mio scena, forse il problema è quando lo aggiungo nella scena, io uso addSubview.

È sbagliato?

+0

provare a utilizzare un UITextView – LearnCocos2D

+0

@ LearnCocos2D sempre lo stesso, non risulta:/ – Vergmort

risposta

15

È possibile aggiungere oggetti che eredita UIView di all'interno della funzione didMoveToView:(SKView *)view

solo aggiungere che la funzione al vostro SKScene e spostare il UITextField all'interno:

-(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

Grande e la soluzione più efficace. Grazie! – Vergmort

+1

Grazie per la risposta. Se cambio in un'altra Scena, il TextField rimane nella vista. Devo rimuoverlo manualmente? – palme

+2

sfortunatamente lo farai, @palme –

1

per rimuoverlo utilizzare il

[myTextField removeFromSuperView]; 

se non si desidera utilizzare il metodo didMoveToView che inserisce il campo di testo nella scena presenta zione, è possibile utilizzare questo metodo:

-(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]; 
} 
Problemi correlati