2009-03-28 14 views
12

में कोई भी कृपया cocos2d में UITextField का उपयोग करने के लिए कुछ लिंक सुझा सकता है। मैं लेबल पर प्रेस करना चाहता हूं, तो UITextField चयनित होना चाहिए और मुझे उस UITextField पर संपादित करने की आवश्यकता है।UITextField उदाहरण कोकोस 2 डी

उत्तर

15

मैं इसे चालू प्रोजेक्ट में कर रहा हूं ताकि खेल शुरू करने के लिए स्तर की संख्या दर्ज करने की अनुमति दी जा सके, इसलिए यही कारण है कि मेरे चर और विधियों का नाम उनके जैसा है; आपको शायद यह समझने के लिए इन्हें समायोजित करना चाहिए।

अपने अनुप्रयोग नियंत्रक में, एक उदाहरण चर के रूप में यह निर्धारित करें:

UITextField *levelEntryTextField; 

applicationDidFinishLaunching अंदर यह बनाएँ:

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

पाठ क्षेत्र को सक्रिय करने के लिए एक विधि को परिभाषित करें। आपको इसे अपने ऐप नियंत्रक के लिए हेडर फ़ाइल में भी घोषित करना चाहिए।

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

यह "वापसी" कीपैड अंत संपादन

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

यह जब संपादन वास्तव में किया जाता है शुरू हो रहा है पर दबाव बनाएगा।

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

अब वास्तव में गति में चीजों को सेट करने के लिए, आप इसे कहीं और डाल देते हैं। मैं एक उपयोगकर्ता कार्रवाई के जवाब में मेरी दृश्य वर्गों में से एक के भीतर से इस फोन:

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

हे, मैं इस तरह की कोशिश की है, लेकिन केवल कुंजीपटल दिखाता है। मुझे टेक्स्ट फ़ील्ड नहीं देखा गया – OMGPOP

+1

कोशिश करें [टेक्स्टबॉक्स सेटटेक्स्टकॉलर: [यूआईसीओलर रंगविड्थेड: 0 हरा: 0 नीला: 0 अल्फा: 1.0]]; [टेक्स्टबॉक्स सेटबैकग्राउंड रंग: [यूआईसीओलर रंगविड्थ: 255 हरा: 255 नीला: 255 अल्फा: 1.0]]; – RSH1

+0

'निर्दिष्ट स्टार्टलेवल' में 'विंडो' क्या है? ठीक उन शुरुआती शुरुआती लोगों के लिए यह है [[[[सीसीडी डायरेक्टर साझा डायरेक्टर] दृश्य] विंडो] '। – Claudiu

10

मैं उदाहरण है कि जैक प्रदान की ली और वास्तव में एक काम प्रोजेक्ट बनाया, इस Cocos2D 0.7.1 XCode टेम्प्लेट का उपयोग करके किया गया था , और फिर बस * AppDelegate.m/.h फ़ाइलों को संपादित करना, जो पूरी तरह से नीचे प्रदान किए जाते हैं। मैंने जैक ने जो कुछ कहा, उसे भी संशोधित किया, क्योंकि मुझे लगता है कि एपडिडफिनिश लोडिंग में यूआईटीएक्स्टफिल्ड बनाना थोड़ा अधिक स्मृति का उपयोग करेगा, खासकर अगर टेक्स्ट फ़ील्ड का हर समय उपयोग नहीं किया जाता है ... यह समाधान केवल टेक्स्ट फ़ील्ड बनाता है जब यह समाधान केवल टेक्स्ट फ़ील्ड बनाता है जरूरी है, नमूना एक खाली कोकोस 2 डी परत दृश्य खींचता है, और स्क्रीन टच पर, यह पाठ दर्ज करने के लिए आपके लिए टेक्स्ट फ़ील्ड प्रदर्शित करता है। यह कंसोल में आपके द्वारा दर्ज किए गए परिणामों के परिणाम को थूक देगा - आप इसे अपने कोड में जो भी आवश्यक हो उसे पास कर सकते हैं।

#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 

और फिर मीटर

#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

धन्यवाद भाई। अच्छा किया – OMGPOP

+0

यह होना चाहिए [[[[सीसीडी डायरेक्टर साझा डायरेक्टर] openGLView] विंडो] addSubview: myText]; [[[[निदेशक साझा डायरेक्टर] openGLView] विंडो के स्थान पर] addSubview: myText]; –

0

Cocos2d में पाठ फ़ील्ड का उपयोग करने के लिए निम्न CCNode उपवर्ग, CCMenuItemTextField, कोशिश करो।

कक्षा सीधे CCMenuItemSprite से उपclassed है। टैप किए जाने पर, "चयनित" विधि कहा जाता है और मुख्य विंडो में UITextField जोड़ा जाता है। संपादन करने के बाद, "अचयनित" विधि कहा जाता है और स्क्रीन से UITextField हटा दिया जाता है। उपयोगकर्ता का इनपुट एक CCLabelTTF नोड में सहेजा जाता है, जो मूल रूप से मूल UITextField के रूप में स्थित होता है।

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।मीटर

#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

आपने किस कोकोस 2 डी संस्करण का उपयोग किया था? यह v1.1 के साथ सीधे काम नहीं कर रहा प्रतीत होता है। –

2

कोड सभी के

पहले आप दृश्य में और ऐड पाठ फ़ील्ड afetr दृश्य जोड़ने बहुत आसान दृश्य thats में जोड़ने के नीचे के रूप में Cocos2d में पाठ क्षेत्र को जोड़ने के लिए।

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

आप भी Cocos2d सबसे अच्छा उदाहरण में CCTextfield उपयोग कर सकते हैं https://github.com/iNinja/CCTextField

संबंधित मुद्दे