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

ResetDialogLayerの実装:

#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

最後に、私は問題が何であるかを理解しました!

クラスの私のinitWithBlockメソッドと衝突していたCocos2Dライブラリの別のファイルにグローバル定義がありました!

initメソッドの名前を変更しただけで問題は解決しましたが、1日を無駄にしました:-(

/** 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 に答える