2012-12-21 27 views
1
@implementation demoScene{ 

-(void) initializeScene { 
moon *_moon=[[moon alloc]init]; 
} 
-(void) updateBeforeTransform: (CC3NodeUpdatingVisitor*) visitor { 

    deltaTime =deltaTime+visitor.deltaTime; 
    NSLog(@"delta time=%0.12f",deltaTime); 
    [_moon print:deltaTime/100000]; 
} 
@end 

這是我的問題。「未使用的變量」

我想在initializeScene方法中從月球類創建一個對象,並且我想發送消息給updateBeforeTransform方法中的那個對象。

當我鍵入這樣的代碼時,我無法發送消息到_moon對象並獲得「未使用的變量」警告消息。

我知道對象超出了範圍,但如果我需要發送消息從updateBeforeTransform方法。並且方法在一秒鐘內被稱爲60次。所以我不想在一秒鐘內創建一個對象60次。

任何建議,將不勝感激。

+0

語法錯誤!擺脫'@ implementation'塊的大括號。 – Linuxios

回答

2

你需要一個實例變量,而不是在initializeScene方法創建一個新的變量:

@implementation demoScene { 
    moon *_moon; // You may already have this in the .h file - just have it in 1 place. 
} 

- (void)initializeScene { 
    _moon = [[moon alloc] init]; // assign to ivar 
} 

- (void)updateBeforeTransform:(CC3NodeUpdatingVisitor*) visitor { 
    deltaTime = deltaTime + visitor.deltaTime; 
    NSLog(@"delta time=%0.12f", deltaTime); 
    [_moon print:deltaTime/100000]; 
} 

@end 

附註 - 類名稱應以大寫字母開頭。變量和方法名稱以小寫字母開頭。