2

我正在努力解决我正在制作的回调函数的这个问题。这是我定义全局块的方式:

#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
    }
}

任何帮助表示赞赏。

谢谢,

4

1 回答 1

2

最后,我弄清楚了问题所在!

来自 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;

谢谢你的帮助...

于 2012-12-26T23:24:03.887 回答