2009-03-28 33 views
12

Bất kỳ ai cũng có thể đề xuất một số liên kết để sử dụng UITextField trong cocos2d. Tôi muốn nhấn vào nhãn, sau đó chọn UITextField và tôi cần chỉnh sửa trên đó UITextField.Ví dụ về UITextField trong Cocos2d

Trả lời

15

Tôi đang làm điều này trong một dự án hiện tại để cho phép nhập số cấp để bắt đầu chơi tại, vì vậy đó là lý do tại sao các biến và phương thức của tôi được đặt tên theo cách của chúng; bạn có lẽ nên điều chỉnh chúng để có ý nghĩa cho bạn.

Trong điều khiển ứng dụng của bạn, xác định đây là một biến Ví dụ:

UITextField *levelEntryTextField; 

Tạo nó bên trong applicationDidFinishLaunching:

levelEntryTextField = [[UITextField alloc] initWithFrame: 
               CGRectMake(60, 165, 200, 90)]; 
    [levelEntryTextField setDelegate:self]; 

Xác định một phương pháp để kích hoạt các lĩnh vực văn bản. Bạn cũng nên khai báo nó trong tệp tiêu đề cho bộ điều khiển ứng dụng của bạn.

- (void)specifyStartLevel 
{ 
    [levelEntryTextField setText:@""]; 
    [window addSubview:levelEntryTextField]; 
    [levelEntryTextField becomeFirstResponder];  
} 

này sẽ làm cho bức xúc "trở lại" trên bàn phím chỉnh sửa cuối

- (BOOL)textFieldShouldReturn:(UITextField*)textField { 
    //Terminate editing 
    [textField resignFirstResponder]; 
    return YES; 
} 

này được kích hoạt khi chỉnh sửa được thực sự thực hiện.

- (void)textFieldDidEndEditing:(UITextField*)textField { 
    if (textField==levelEntryTextField) { 
     [levelEntryTextField endEditing:YES]; 
     [levelEntryTextField removeFromSuperview]; 
     // here is where you should do something with the data they entered 
     NSString *result = levelEntryTextField.text; 
    } 
} 

Bây giờ để thực sự thiết lập mọi thứ đang chuyển động, bạn đặt nó ở đâu đó. Tôi gọi đây là từ bên trong một lớp học Scene của tôi, để đáp ứng với một hành động người dùng:

[[[UIApplication sharedApplication] delegate] specifyStartLevel]; 
+0

hey, tôi đã thử cách này nhưng chỉ có bàn phím xuất hiện. tôi không thấy trường văn bản – OMGPOP

+1

Thử [textBox setTextColor: [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 1.0]]; [textBox setBackgroundColor: [UIColor colorWithRed: 255 màu xanh lá cây: 255 màu xanh lam: 255 alpha: 1.0]]; – RSH1

+0

'window' trong' specifyStartLevel' là gì? ok cho những người mới bắt đầu thực sự đó là '[[[CCDirector sharedDirector] xem] cửa sổ]'. – Claudiu

10

Tôi lấy ví dụ mà Jack cung cấp và thực sự tạo ra một dự án làm việc, điều này đã được thực hiện bằng cách sử dụng Cocos2D 0.7.1 XCode Template và sau đó chỉ chỉnh sửa các tệp * AppDelegate.m/.h, được cung cấp bên dưới trong toàn bộ. Tôi cũng sửa đổi một số những gì Jack nói, bởi vì tôi cảm thấy rằng việc tạo ra UITextField trong appDidFinishLoading sẽ sử dụng quá nhiều bộ nhớ, đặc biệt nếu trường text không được sử dụng mọi lúc ... giải pháp này chỉ tạo trường text khi nó là cần thiết, mẫu vẽ một cảnh Cocos2D Lớp trống và trên màn hình cảm ứng, nó sẽ hiển thị trường văn bản để bạn bắt đầu nhập văn bản vào. Nó sẽ nhổ ra kết quả của những gì bạn đã nhập vào Bàn điều khiển - bạn có thể truyền điều này cho bất kỳ điều gì là cần thiết trong mã của riêng bạn.

các .h

#import <UIKit/UIKit.h> 
#import "cocos2d.h" 
@interface MYSCENE : Layer <UITextFieldDelegate> 
{ 
    UITextField *myText; 
} 
-(void)specificStartLevel; 
@end 
@interface textFieldTestAppDelegate : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate> 
{ 
    UIWindow *window; 
} 
@end 

và sau đó là .m

#import "textFieldTestAppDelegate.h" 
@implementation MYSCENE 
-(id) init 
{ 
    self = [super init]; 
    isTouchEnabled = YES; 
    return self; 
} 
-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self specifyStartLevel]; 
    return kEventHandled; 
} 
-(void)specifyStartLevel { 
    myText = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)]; 
    [myText setDelegate:self]; 
    [myText setText:@""]; 
    [myText setTextColor: [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0]]; 
    [[[[Director sharedDirector] openGLView] window] addSubview:myText]; 
    [myText becomeFirstResponder]; 
} 
-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [myText resignFirstResponder]; 
    return YES; 
} 
-(void)textFieldDidEndEditing: (UITextField *)textField { 
    if(textField == myText) { 
     [myText endEditing:YES]; 
     [myText removeFromSuperview]; 
     NSString *result = myText.text; 
     NSLog([NSString stringWithFormat:@"entered: %@", result]); 
    } else { 
     NSLog(@"textField did not match myText"); 
    } 
} 
-(void) dealloc 
{ 
[super dealloc]; 
} 
@end 
@implementation textFieldTestAppDelegate 
- (void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [window setUserInteractionEnabled:YES]; 
    [[Director sharedDirector] setDisplayFPS:YES]; 
    [[Director sharedDirector] attachInWindow:window]; 
    Scene *scene = [Scene node]; 
    [scene addChild: [MYSCENE node]]; 
    [window makeKeyAndVisible]; 
    [[Director sharedDirector] runWithScene: scene]; 
} 
-(void)dealloc 
{ 
    [super dealloc]; 
} 
-(void) applicationWillResignActive:(UIApplication *)application 
{ 
    [[Director sharedDirector] pause]; 
} 
-(void) applicationDidBecomeActive:(UIApplication *)application 
{ 
    [[Director sharedDirector] resume]; 
} 
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application 
{ 
    [[TextureMgr sharedTextureMgr] removeAllTextures]; 
} 
@end 
+0

cảm ơn bro. thực hiện tốt đẹp – OMGPOP

+0

Điều đó phải là [[[[[CCDirector sharedDirector] openGLView] cửa sổ] addSubview: myText]; thay cho [[[[[Giám đốc sharedDirector] openGLView] cửa sổ] addSubview: myText]; –

0

Hãy thử phân lớp CCNode sau, CCMenuItemTextField, sử dụng các trường văn bản trong cocos2d.

Lớp được trực tiếp phân lớp từ CCMenuItemSprite. Khi khai thác, phương thức "được chọn" được gọi và một UITextField được thêm vào cửa sổ chính. Sau khi chỉnh sửa xong, phương thức "không được chọn" được gọi và UITextField bị xóa khỏi màn hình. Dữ liệu đầu vào của người dùng được lưu vào nút CCLabelTTF, vị trí của nó chính xác như UITextField gốc.

CCMenuItemTextField.h

@interface CCMenuItemTextField : CCMenuItemSprite<UITextFieldDelegate> { 
    UITextField  *textField_; 
    CCLabelTTF  *label_; 

    CGFloat   paddingLeft_; 
} 

@property (readonly, nonatomic) UITextField  *textField; 
@property (readonly, nonatomic) CCLabelTTF  *label; 
@property (assign, nonatomic) CGFloat   paddingLeft; 

- (void)selected; 
- (void)unselected; 
- (void)setFontSize:(CGFloat)size; 

- (NSString*)text; 
- (void)setText:(NSString*)text; 

@end 

CCMenuItemTextField.m

#import "CCMenuItemTextField.h" 

@implementation CCMenuItemTextField 

@synthesize 
textField = textField_, 
label = label_, 
paddingLeft = paddingLeft_; 

- (id)init 
{ 
    CCSprite *normalSprite = [CCSprite spriteWithFile:@"text_field_background.png"]; 
    CCSprite *selectedSprite = [CCSprite spriteWithFile:@"text_field_background.png"]; 
    CCSprite *disabledSprite = [CCSprite spriteWithFile:@"text_field_background.png"]; 

    return [self initWithNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:disabledSprite]; 
} 

- (id)initWithNormalSprite:(CCNode<CCRGBAProtocol> *)normalSprite 
      selectedSprite:(CCNode<CCRGBAProtocol> *)selectedSprite 
      disabledSprite:(CCNode<CCRGBAProtocol> *)disabledSprite 
{ 
    self = [super initWithNormalSprite:normalSprite 
         selectedSprite:selectedSprite 
         disabledSprite:disabledSprite 
           target:self 
           selector:@selector(selected)]; 

    if (self) { 
     paddingLeft_ = 3.0; 

     textField_ = [[UITextField alloc] init]; 
     [textField_ setTextColor:[UIColor blackColor]]; 
     [textField_ setFont:[UIFont systemFontOfSize:18]]; 

     label_ = [[CCLabelTTF node] retain]; 
     [label_ setAnchorPoint:ccp(0,0.5)]; 
     [label_ setFontSize:18]; 
     [label_ setVisible:NO]; 
     [label_ setColor:ccBLACK]; 
     [self addChild:label_]; 
    } 

    return self; 
} 

- (void)dealloc 
{ 
    [label_ release]; 
    [textField_ release]; 
    [super dealloc]; 
} 

// -------------------------------- 
// Public 
// -------------------------------- 

- (void)selected 
{ 
    [super selected]; 

    [label_ setVisible:NO]; 

    CGAffineTransform transform = [self nodeToWorldTransform]; 
    float textFieldHeight = textField_.font.lineHeight; 
    float width = self.contentSize.width; 
    float height = self.contentSize.height; 
    float left = transform.tx + paddingLeft_; 
    float top = 480 - transform.ty - height + (height - textFieldHeight)/2; 

    [textField_ setFrame:CGRectMake(left, top, width, height)]; 
    [[[[CCDirector sharedDirector] view] window] addSubview:textField_]; 
    [textField_ becomeFirstResponder]; 
    [textField_ setDelegate:self]; 
} 

- (void)unselected 
{ 
    [super unselected]; 

    [label_ setVisible:YES]; 
    [label_ setPosition:ccp(paddingLeft_, self.contentSize.height/2)]; 

    NSString *text = textField_.text ? textField_.text : @""; 
    [label_ setString:text]; 

    [textField_ resignFirstResponder]; 
    [textField_ removeFromSuperview]; 
} 

- (NSString*)text 
{ 
    return [label_ string]; 
} 

- (void)setText:(NSString*)text 
{ 
    [label_ setString:text]; 
    [textField_ setText:text]; 
} 

// -------------------------------- 
// UITextFieldDelegate 
// -------------------------------- 

- (BOOL)textFieldShouldReturn:(UITextField*)textField { 
    [self unselected]; 
    return YES; 
} 

- (void)textFieldDidEndEditing:(UITextField*)textField { 
    [self unselected]; 
} 

- (void)setFontSize:(CGFloat)size 
{ 
    [label_ setFontSize:size]; 
    [textField_ setFont:[UIFont systemFontOfSize:size]]; 
} 

// -------------------------------- 
// CCNode 
// -------------------------------- 

- (void)onExitTransitionDidStart 
{ 
    [super onExitTransitionDidStart]; 
    [self unselected]; 
} 

@end 
+1

Bạn đã sử dụng phiên bản Cocos2d nào? Nó dường như không hoạt động trực tiếp với v1.1. –

2

Để thêm lĩnh vực văn bản trong cocos2d như sau đang

đầu tiên của tất cả các bạn thêm xem trong Scene và afetr add textfield thêm theo quan điểm Thats rất dễ dàng.

-(id) init 
{ 
    if((self=[super init])) 
    { 

     // add view in scene 

     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; 
     view.backgroundColor = [UIColor redColor]; 

    // add textfield in view 

     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 140, 300, 30)]; 
     textField.borderStyle = UITextBorderStyleRoundedRect; 
     textField.font = [UIFont systemFontOfSize:15]; 
     textField.placeholder = @"enter text"; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.keyboardType = UIKeyboardTypeDefault; 
     textField.returnKeyType = UIReturnKeyDone; 
     textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
     textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
     textField.delegate = self; 
     [view addSubview:textField]; 

    // add view in scene 

     [[[CCDirector sharedDirector] view] addSubview:view]; 
} 
    return self; 
} 

bạn cũng có thể sử dụng CCTextfield trong cocos2d ví dụ tốt nhất là https://github.com/iNinja/CCTextField