我正在努力解决我正在制作的回调函数的这个问题。这是我定义全局块的方式:
#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
}
}
任何帮助表示赞赏。
谢谢,