1

我一直在尝试调用下面的函数。似乎每当在函数playNote中我试图访问我作为参数(myNum)传递的对象时,它总是崩溃。我很新,我可能不明白如何通过 CCCallFuncND 传递参数。感谢所有评论。

这是传递参数myNum的调用:

id action2 = [CCCallFuncND actionWithTarget:self selector:@selector(playNote:data:) data:(NSNumber *)myNum];

这是整个块:

- (void)muteAndPlayNote:(NSInteger)noteValue :(CCLayer*)currentLayer
{
myNum = [NSNumber numberWithInteger:noteValue];

NSLog(@"Test the number: %d", [myNum integerValue]);

id action1 = [CCCallFunc actionWithTarget:self selector:@selector(muteAudioInput)];

id action2 = [CCCallFuncND actionWithTarget:self selector:@selector(playNote:data:) data:(NSNumber *)myNum];

id action3 = [CCDelayTime actionWithDuration:3];

id action4 = [CCCallFunc actionWithTarget:self selector:@selector(unmuteAudioInput)];

[currentLayer runAction: [CCSequence actions:action1, action2, action3, action4, nil]];

}

NSLog 从不显示它在此行崩溃的任何内容。

- (void) playNote:(id)sender data:(NSNumber *)MIDInoteValue

{
NSLog(@"Test number 2: %d", [MIDInoteValue integerValue]);
int myInt = [MIDInoteValue floatValue];
[PdBase sendFloat: 55 toReceiver:@"midinote"];
[PdBase sendBangToReceiver:@"trigger"];
}
4

3 回答 3

2

请注意,如果您使用 ARC,CCCallFunc* 操作本质上是不安全的。

不管怎样,通常最好使用 CCCallBlock* 操作(在 ARC 下可以安全使用),因为这样您通常甚至不需要将数据作为参数传递,您只需在块内使用本地范围的变量即可:

myNum = [NSNumber numberWithInteger:noteValue];
[CCCallBlock actionWithBlock:^{
    NSInteger myInt = [myNum integerValue];
    // do something with myInt, or just use noteValue directly
}];

PS:检查您的代码的数据类型一致性。您将 NSNumber 创建myNum为一个NSInteger值,稍后您可以通过floatValue将数字隐式转换为float然后再转换回intintegerValue改为使用)的方法获取它。您将其分配给int仅在 32 位系统上与 int 相同的值,在 iPhone 5S 等 64 位系统上 NSInteger 实际上是 64 位类型(使用NSInteger而不是int)。

如果您在使用完全相同的数据类型时不一致,您可能会遇到令人讨厌的值转换问题(以及为 64 位设备构建时的问题)。另外,您甚至可能已经收到关于此的警告 - 认真对待这些警告。

于 2013-10-07T16:23:40.230 回答
1

对于那些遇到此功能问题的人,这里是工作版本:

id action2 = [CCCallFuncND actionWithTarget:self selector:@selector(playNote:data:) data:(void *)noteValue];

然后是定义:

- (void) playNote:(id)sender data:(void *)midiNoteCode

{
int myNum = midiNoteCode; //void * to int conversion may cause problems on 64bit platform, wrap it into NSInteger
[PdBase sendFloat: (float)myNum toReceiver:@"midinote"];
[PdBase sendBangToReceiver:@"trigger"];

}

于 2013-10-10T00:25:04.753 回答
1

您的方法签名是:

-(void)playNote:(id)sender data:(NSNumber*)MIDInoteValue

但应该是:

-(void)playNote:(id)sender data:(void*)data

这在 CCActionInstant.h 中定义为:

typedef void (*CC_CALLBACK_ND)(id, SEL, id, void *);

另外,我很确定您会从崩溃中获得一些信息,例如调用堆栈结束控制台输出,如果我错了,将其粘贴在这里会很有帮助;)

于 2013-10-07T15:33:30.177 回答