2011-12-16 22 views
0

如果我这样做,我可以从另一个类中获得变量:从其他类中取回变量不工作

--------------------- ------- HelloWorldLayer.h -------------------------------

@interface HelloWorldLayer : CCLayer 
{ 

... 
BOOL win; 
... 
} 

@property (nonatomic,readwrite) BOOL win; 

- -------------------------- HelloWorldLayer.m --------------------- ----------

@synthesize win; 

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (...){ 

    //do something with with variable win ----> IN <---- if statement 
    win = YES; 
    } 
//win is changed only in if statement 
} 

---------------------------- LevelDone.m - -----------------------------

-(void)nextLevel:(id)sender{ 
    NSLog(@"next level"); 

    HelloWorldLayer *obj = [[HelloWorldLayer alloc]init]; 
    if (obj.win==YES){ 
     NSLog(@"win = YES"); 
    }else { 
     NSLog(@"win = NO"); 
    } 
    win = NO; 
    [[CCDirector sharedDirector] popScene]; 

} 

我可以先获取和设置变量win这里,让其他类现在有NO分配赢变量或者是在if语句不是全局处理赢的分配?

,如果我在init方法分配变量NO,并改变它的功能,它只会采取了在init方法被赋予的价值......究竟为什么会这样?

回答

1

,如果我在init方法分配变量NO,改变它的 功能,它只会采取了在 init方法被赋予的价值......究竟为什么会这样?

因为你不使用同一个对象,你要创建一个新的一个

HelloWorldLayer *obj = [[HelloWorldLayer alloc]init]; 
    if (obj.win==YES){ 
     NSLog(@"win = YES"); 
    }else { 
     NSLog(@"win = NO"); 
    } 

运行此行代码每次:

HelloWorldLayer *obj = [[HelloWorldLayer alloc]init]; 

...你创建一个HelloWorldLayer的新实例。它会运行init方法中的代码,因为它发送的是init消息。这就是为什么你在init方法中设置的任何值都将被上面的代码记录下来。

你想要的是访问现有的 HelloWorldLayer的实例,而不是创建该类的新实例。我相信@Jeremy给了你一个满意的解决方案。另一种方法是将HelloWorldLayer类变为单例。

+0

谢谢。我以前曾经想过,但是我(被错误地)告知我实际上可以访问它们。 – 2011-12-16 17:42:04

0

你会想要考虑使用协议。这样,LevelDone会收到HelloWorldLayer是否获胜的通知。特别是,如果HelloWorldLayer是一个很长的进程并且异步运行。下面是一个粗略的例子:

HelloWorldLayer.h

@protocol HelloWorldLayerDelegate 
- (void)helloWorldLayer:(HelloWorldLayer *)layer didWin:(BOOL)win; 
@end 

@interface HelloWorldLayer : CCLayer 
{ 

... 
BOOL win; 
... 
} 

@property (nonatomic,readwrite) BOOL win; 
@property (nonatomic, assign) id <HelloWorldLayerDelegate>delegate; 

@end 

HelloWorldLayer.h

@implementation HelloWorldLayer 
@synthesize win; 
@synthesize delegate; 

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (...){ 

    //do something with with variable win ----> IN <---- if statement 
    win = YES; 

    [self.delegate helloWorldLayer:self didWin:win]; 


    } 
//win is changed only in if statement 
} 

@end 

LevelDone.h

@interface LevelDone <HelloWorldLayerDelegate> 
    HelloWorldLayer *_hello; 
@end 

LevelDone.m

@implementation LevelDone 

- (void)playLevel { 

    _hello = [HelloWorldLayer alloc] init]; 
    _hello.delegate = self; 

    [_hello start]; 

} 

- (void)helloWorldLayer:(HelloWorldLayer *)layer didWin:(BOOL)win { 

    //Rather than having a didWin: parameter, maybe you can just check the property of the layer object 

    NSLog(@"win = %@", win ? @"YES" : @"NO"); 


    [[CCDirector sharedDirector] popScene]; 

} 

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

@end 
+0

ok,非常感谢......如果我把`@protocol HelloWorldLayerDelegate - (void)helloWorldLayer:(HelloWorldLayer *)层didWin:(BOOL)win;`在`@ interface`之前,它不会承认类型`(HelloWorldLayer *)` – 2011-12-16 17:38:26

0

这是混乱的使用由@synthesize产生的存取器和直接引用该实例变量之间的示例。尝试声明你的财产是这样的:

---------------------------- HelloWorldLayer.h -------- -----------------------

@interface HelloWorldLayer : CCLayer 

@property (nonatomic) BOOL win; 

-------------------- -------- HelloWorldLayer.m -------------------------------

@synthesize win=_win; 

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (...){ 
     self.win = YES; 
    } 

} 

I认为布尔值始终默认为所以你真的不需要在init()中设置它,除非你想它默认为

无论如何,现在实例变量在您的类HelloWorldLayer中设置为_win。无论何时你想设置它总是指它作为self.win。在处理属性时,这是一个安全的习惯,以确保您不会遇到内存泄漏,因为当您处理指针时,生成的访问器将为您释放并保留。如果直接设置实例变量,则必须先记住先释放它。

希望这会有所帮助!