0

我正在尝试根据音频输入存储一个数组,然后在播放录音时播放与输入相对应的动画帧。

代码一直运行到现在,除了一段时间后它在模拟器中崩溃并突出显示

"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:)];

}
4

3 回答 3

0

显然,小的调试有很长的路要走。您能否在行前为myInt添加控制打印输出

if(myInt < 10){

在崩溃前查看myInt的值?

如果myInt is <= 0您的程序对这种情况没有保护,那么生成的图片将不存在。并且对于myInt > 10 程序将崩溃,因为NSString * characterImageString;是随机值的自动未初始化变量。

于 2012-12-22T20:44:57.713 回答
0

嗯……一些母性和苹果派,很难得到可用的信息。不确定初始浮点值是多少,因此请在某处声明您的最小和最大图像编号(例如,kMinFrameNumber 和 kMaxFrameNumber)。由于浮点值可以是算法开始时的任何值,因此在计算 myInt 后添加以下“防御性”行:

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 
}

C。在设置精灵之前检查您的纹理是否为 nil(它将在 cocos2d 2.0 版中接受 nil 纹理)但是,您对代码的状态一无所知。

于 2012-12-22T23:18:24.073 回答
0

characterImageStringnil如果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];        
}
于 2012-12-22T22:26:46.480 回答