我正在尝试存储基于音频输入的数组,然后在播放录音时播放与输入相对应的动画帧。
代码工作到现在,除了一段时间后它在模拟器中崩溃并突出显示
"CCLOG(@"adding image: %@", characterImageString);";
用这个:
EXC_BAD_ACCESS (code=1, address=0xd686be8)
这是我知道的内存管理,但我绝对难过。
if(isRecording){
int myInt;
NSString * characterImageString;
//get a number based on the volume input
float f = audioMonitorResults * 200; //convert max(0.06) to 12
f=((f/12)*10);
NSNumber *myNumber = [NSNumber numberWithDouble:(f+0.5)];
myInt = [myNumber intValue] + 1;
//create the image file name from the intiger we
//created from the audiomonitor results
if(myInt < 10){
characterImageString = [NSString stringWithFormat:@"fungus000%i.png",myInt];
} else if (myInt == 10){
characterImageString = [NSString stringWithFormat:@"fungus00%i.png",myInt];
}
CCLOG(@"adding image: %@", characterImageString);
//add each frame
[animationSequence addObject:characterImageString];
// print array contents
NSLog(@"animationSequence Array: %@", animationSequence);
// print array size
NSLog(@"animationSequence Number of Objects in Array: %u", [animationSequence count]); }
这是播放音频时播放的代码:
-(void) updateAnimation:(ccTime) delta{
myFrame ++;
NSString *imageToDisplay;
imageToDisplay = animationSequence[myFrame];
CCTexture2D *currentTextureToDisplay = [[CCTextureCache sharedTextureCache] addImage:imageToDisplay];
[character setTexture:currentTextureToDisplay];
CCLOG(@"current texture to display: %@", currentTextureToDisplay);
if (myFrame >= [animationSequence count]) {
[self unschedule:@selector(updateAnimation:)];
}
答案 0 :(得分:0)
显然,小型调试还有很长的路要走。你能否在行
之前为 myInt 添加控制打印输出 if(myInt < 10){
在崩溃前查看 myInt 的值?
如果myInt is <= 0
您的程序对此类案例没有保护,那么结果图片将不存在。
对于myInt > 10
,程序将崩溃,因为NSString * characterImageString;
是随机值的自动未初始化变量。
答案 1 :(得分:0)
如果characterImageString
nil
为myInt > 10
抛出异常,因为您正在尝试打印尚未初始化的变量。
您可以尝试将代码更改为以下内容:
if(myInt < 10)
{
characterImageString = [NSString stringWithFormat:@"fungus000%i.png",myInt];
}
else if (myInt >= 10 && myInt < 100)
{
characterImageString = [NSString stringWithFormat:@"fungus00%i.png",myInt];
}
else if (myInt >= 100 && myInt < 1000)
{
characterImageString = [NSString stringWithFormat:@"fungus0%i.png",myInt];
}
else
{
characterImageString = [NSString stringWithFormat:@"fungus%i.png",myInt];
}
答案 2 :(得分:0)
myInt=MAX(kMinFrameNumber,myInt);
myInt=MIN(kMaxFrameNumber,myInt);
然后格式化:
characterImageString = [NSString stringWithFormat:@"fungus%04i.png",myInt];
最后,我怀疑在突出显示的行(即检测到的位置)处抛出异常。
一个。你是如何声明数组animationSequence的(它是否保留?)。如果没有,它可能会在某个随机间隔内自动释放,并且您将尝试向已解除分配的实例发送消息。
湾您还应该在寻址animationSequence
之前检查边界if(myFrame<[animationSequence count]-1) {
imageToDisplay = animationSequence[myFrame];
} else {
CCLOGERROR(@"Yelp ! addressing out of bounds!");
// terminate neatly here ! as in unschedule and return
}
℃。在设置精灵之前检查你的纹理是否为零(它将接受cocos2d版本2.0中的nil纹理)但是,你对代码的状态一无所知。