0

我在将数据(文本字段文本)从一个场景放置到另一个场景时遇到问题。我在这封邮件中附上了我的源代码。如果我的代码有任何问题,请帮助我。

//
//  PlayerSettingsAppDelegate.m
//  PlayerSettings
//
// 
//

#import "PlayerSettingsAppDelegate.h"
#import "TestScene.h"

NSString *result;

@implementation MYSCENE
-(id) init
{
      [super init];

        id layer = [ColorLayer layerWithColor: 0x2266FFff];
        [self add:layer z:0];
        [MenuItemFont setFontSize:15];
        [MenuItemFont setFontName:@"Helvetica"];
        MenuItem *backMenu = [MenuItemFont itemFromString:@"Back" target:self selector:@selector(menuBack:)]
;
        Menu *menu = [Menu menuWithItems:backMenu,nil];

        [menu alignItemsVertically];
        [self add:menu];

    /*Text field creation*/
    myTextField = [[UITextField alloc]initWithFrame:CGRectMake(60, 165, 100, 10)];
    myTextField.placeholder=@"<Enter text>";
    [myTextField setDelegate:self];
    [window addSubview:myTextField];


    return self;
}

-(void)menuBack:(id)sender{
    NSLog(@"Back");
    TestScene *ts = [TestScene node];
    [[Director sharedDirector]replaceScene:ts];
}

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

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(textField==myTextField){
        [myTextField endEditing:YES];

        result = myTextField.text;
        [label setString:result];
        [myTextField removeFromSuperview];
    }
}

-(void) dealloc
{
    [super dealloc];
}
@end

@implementation MenuLayer

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



    return self;
}




@end


@implementation PlayerSettingsAppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    // NEW: Init the window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window setUserInteractionEnabled:YES];
    //[window setMultipleTouchEnabled:YES];

    //[[Director sharedDirector] setLandscape: YES];
    [[Director sharedDirector] setDisplayFPS:YES];

    [[Director sharedDirector] attachInWindow:window];

    Scene *scene = [Scene node];
    [scene add: [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

-------------------------------------------------------------------------
//
//  TestScene.m
//  PlayerSettings
//
// 
//

#import "TestScene.h"
#import "cocos2d.h"
#import "PlayerSettingsAppDelegate.h"

UITextField *levelEntryTextField;

@implementation TestScene
-(id)init{
    [super init];
    Sprite *bg = [Sprite spriteWithFile:@"image1.jpg"];
    [bg setPosition:cpv(160,240)];
    [self add:bg z:0];

    label = [Label labelWithString:@"result" fontName:@"Helvetica" fontSize:20.0];
    label.position = cpv(100,100);
    [self add:label z:1];

    return self;
}



@end
4

1 回答 1

0

不完全确定,但我相信

label = [Label labelWithString:@"result" fontName:@"Helvetica" fontSize:20.0];

应该读

label = [Label labelWithString:result fontName:@"Helvetica" fontSize:20.0];
于 2009-04-10T23:59:39.907 回答