2011-12-16 47 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。在處理屬性時,這是一個安全的習慣,以確保您不會遇到內存泄漏,因爲當您處理指針時,生成的訪問器將爲您釋放並保留。如果直接設置實例變量,則必須先記住先釋放它。

希望這會有所幫助!

相關問題