2011年6月14日 星期二

cocos2d UITextField!!( 1.0.0 rc2)

想要在 cocos2d的環境下,呼叫 keyboard 出來
目前版本CCLabel似乎沒有支援,在拜請google神之後,到是找到另一個方法,所以先記錄下來


原文:http://stackoverflow.com/questions/693483/textfield-example-in-cocos2d


這個解法是 把UITextField 想辦法放到目前的畫面,自然就可以支援 keyboard 了~~


重點         (1) 創建一個 UITextField *myText;
(2)讓你的 layer <UITextFieldDelegate>
(3)把 myText 加到畫面上 (討厭的新舊版小細節)


[[[[CCDirector sharedDirector] openGLView] window] addSubview:myText];
    [myText becomeFirstResponder];

                 (4) 一些mothed


-(BOOL)ccTouchesBegan:(NSSet  *)touches withEvent:(UIEvent *)event 
-(BOOL)textFieldShouldReturn:(UITextField *)textField 
-(void)textFieldDidEndEditing: (UITextField *)textField 



主要code 節錄:
//------------------------------------------------------------------------------------------------------------------------------
.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



//------------------------------------------------------------------------------------------------------------------------------

.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

沒有留言:

張貼留言