我正在努力解决我正在制作的回调函数上的这个问题。 以下是我定义全局 block 的方式:
#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
我在另一个类的方法中使用该 block ,如下所示:
-(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
}
}
感谢任何帮助。
谢谢
最佳答案
终于知道问题出在哪里了!
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;
感谢您的帮助...
关于objective-c - 不兼容的 block 指针类型将 'void (^)(bool)' 发送到类型 'void(^)()' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14035271/