2012-12-26 77 views
1

我在努力解決對我提出的回調函數這個問題。 下面是我定義我的全球塊:不相容的嵌段指針類型發送「無效(^)(布爾)」到類型的參數「無效(^)()」

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

typedef void (^RestartBlock)(bool); 
RestartBlock block = ^(bool restart) 
{ 
    if (restart) { 
     // restart 
    } 
    else 
    { 
     // continue 
    } 
}; 

@interface RestartDialogLayer : CCLayer 
{ 
    RestartBlock m_block; 
    bool m_bRestart; 
} 

-(id) initWithBlock:(RestartBlock)block; 
-(void) restartButtonPressed:(id)sender; 
-(void) resumeButtonPressed:(id)sender; 

@end 

RestartDialogLayer的執行情況:

#import "RestartDialogLayer.h" 

@implementation RestartDialogLayer 

-(id) initWithBlock:(RestartBlock)block 
{ 
    if ((self = [super init])) 
    { 
     m_bRestart = YES; 
     m_block = block; 
    } 
    return self; 
} 

-(void) restartButtonPressed:(id)sender 
{ 
    m_bRestart = YES; 
    m_block(m_bRestart); 
    [self removeFromParentAndCleanup:YES]; 
} 

-(void) resumeButtonPressed:(id)sender 
{ 
    m_bRestart = NO; 
    m_block(m_bRestart); 
    [self removeFromParentAndCleanup:YES]; 
} 

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

@end 

我用的是塊另一個類的這樣的方法:

-(void) singlePlayerSceneSchedule:(ccTime) delta 
{ 
    CCLOG(@"demoSceneSchedule MainMenuScene"); 
    [self unschedule:_cmd]; 

    bool gameLeftActive = [Globals sharedGlobals].gameLeftActive; 

    if (gameLeftActive) 
    { 
     RestartDialogLayer* dialog = [[RestartDialogLayer node] initWithBlock:block]; 
    } 
    else 
    { 
     // Start a new game 
    } 
} 

任何幫助表示讚賞。

感謝,

+0

如何界定這種方法'? - (ID)initWithBlock:????(RestartBlock)塊{}'它'(RestartBlock)block'作爲輸入參數可以請你發佈此問題的截圖 – iDev

+1

你檢查'm_block'具有正確的類型 – Matthias

+0

是,RestartBlock m_block; –

回答

1

最後,我想通了什麼問題了!

有從cocos2d的庫另一個文件中的全局定義,將其與類的我initWithBlock方法發生衝突!

我只是改名爲我的init()方法,並且解決了這一問題,但浪費了我的時間,每天:-(

/** initialized the action with the specified block, to be used as a callback. 
The block will be "copied". 
*/ 
-(id) initWithBlock:(void(^)())block; 

感謝您的幫助......

相關問題