2012-11-18 159 views
0

我在HelloWorldLayer類中初始化了一個方法播放器方法(繼承自NSObject)。我想以這種方式聲明同一類的其他方法,我不需要分配它們。例如:在沒有alloc的情況下訪問另一個類中的方法

Player.mm

-(id)spritePlayer:(CCLayer *)parentLayer 
        inWorld:(b2World *)world 
{ 
    creation of sprite body and other stuff 
} 

我想在接觸監聽器類中使用這種方法,但我已經宣佈它的播放器類:

-(void) touchingFix:(b2Fixture *)touchedFix 
{ 
    bodyTouched=TRUE; 
    bodyFix=touchedFix; 
} 

在聯繫監聽器我只能通過以下方式訪問它:

[[Player alloc] touchingFix:fixtureA]; 

我不能以某種方式訪問​​它,而無需在另一個位置分配它 方法?如果是的話,我該怎麼做。

回答

2

將其更改爲一個類的方法:去掉「 - 」在前面的方法,並將其更改爲「+」

+(void) touchingFix:(b2Fixture *)touchedFix 
{ 
    bodyTouched=TRUE; 
    bodyFix=touchedFix; 
} 

可以使用類名,而不是訪問一個類的方法指針類的一個實例:

[Player touchingFix:fixtureA]; 
+0

我已經改變了「 - 」到「+」,但我在'touchingFix'方法,說行收到一個錯誤'實例變量「bodyTouched」在類變量中訪問,'bodyFix'也一樣。 – oopology

+0

對不起,我的意思是「實例變量」bodyTouched「在類方法中訪問」 – oopology

+0

將變量設置爲靜態。 –

相關問題