开发者

Terminating NSInvalidArgumentException

开发者 https://www.devze.com 2023-03-29 04:40 出处:网络
I feel really bad that I am asking a question like this. I\'ve been searching but i couldn\'t find a solution for it. I was going through a tutorial (http://www.raywenderlich.com/1271/how-to-use-anima

I feel really bad that I am asking a question like this. I've been searching but i couldn't find a solution for it. I was going through a tutorial (http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d) and had a problem like this : * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0. And the output is :

  This GDB was configured as "x86_64-apple-darwin".Attaching to process 10110.
2011-08-20 17:24:52.489 AnimBear[10110:207] cocos2d: cocos2d v1.0.0-beta
2011-08-20 17:24:52.493 AnimBear[10110:207] cocos2d: Using Director Type:CCDirectorDisplayLink
2011-08-20 17:24:52.575 AnimBear[10110:207] cocos2d: OS version: 4.3 (0x04030000)
2011-08-20 17:24:52.577 AnimBear[10110:207] cocos2d: GL_VENDOR:   Apple Computer, Inc.
2011-08-20 17:24:52.578 AnimBear[10110:207] cocos2d: GL_RENDERER: Apple Software Renderer
2011-08-20 17:24:52.579 AnimBear[10110:207] cocos2d: GL_VERSION:  OpenGL ES-CM 1.1 APPLE
2011-08-20 17:24:52.581 AnimBear[10110:207] cocos2d: GL_MAX_TEXTURE_SIZE: 2048
2011-08-20 17:24:52.582 AnimBear[10110:207] cocos2d: GL_MAX_MODELVIEW_STACK_DEPTH: 16
2011-08-20 17:24:52.583 AnimBear[10110:207] cocos2d: GL_MAX_SAMPLES: 4
2011-08-20 17:24:52.583 AnimBear[10110:207] cocos2d: GL supports PVRTC: YES
2011-08-20 17:24:52.584 AnimBear[10110:207] cocos2d: GL supports BGRA8888 textures: YES
2011-08-20 17:24:52.585 AnimBear[10110:207] cocos2d: GL supports NPOT textures: YES
2011-08-20 17:24:52.586 AnimBear[10110:207] cocos2d: GL supports discard_framebuffer: YES
2011-08-20 17:24:52.587 AnimBear[10110:207] cocos2d: compiled with NPOT support: NO
2011-08-20 17:24:52.589 AnimBear[10110:207] cocos2d: compiled with VBO support in TextureAtlas : YES
2011-08-20 17:24:52.589 AnimBear[10110:207] cocos2d: compiled with Affine Matrix transformation in CCNode : YES
2011-08-20 17:24:52.592 AnimBear[10110:207] cocos2d: compiled with Profiling Support: NO
[Switching to process 10110 thread 0x580b]
[Switching to process 10110 thread 0x207]
2011-08-20 17:24:52.621 AnimBear[10110:207] cocos2d: surface size: 480x320
2011-08-20 17:24:52.635 AnimBear[10110:207] cocos2d: CCSpriteFrameCache: Trying to use file 'AnimBear.png' as texture
2011-08-20 17:24:52.735 AnimBear[10110:207] cocos2d: Frame interval: 1
2011-08-20 17:24:52.787 AnimBear[10110:207] -[NSCFString rect]: unrecognized selector sent to instance 0xdc570
2011-08-20 17:24:52.796 AnimBear[10110:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString rect]: unrecognized selector sent to instance 0xdc570'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x0140f5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01563313 objc_exception_throw + 44
    2   CoreFoundation                      0x014110bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01380966 ___forwarding___ + 966
    4   CoreFoundation                      0x01410502 __forwarding_prep_1___ + 50
    5   AnimBear                            0x0005447a -[CCSprite isFrameDisplayed:] + 44
    6   AnimBear                            0x000128f3 -[CCAnimate update:] + 316
    7   AnimBear                            0x0000d187 -[CCActionInterval step:] + 269
    8   AnimBear                            0x0000322b -[CCRepeatForever step:] + 60
    9   AnimBear                            0x000170c4 -[CCActionManager update:] + 287
    10  AnimBear                            0x0004f1d4 -[CCScheduler tick:] + 259
    11  AnimBear                            0x0006f766 -[CCDirectorIOS drawScene] + 133
    12  AnimBear                            0x00071449 -[CCDirectorDisplayLink mainLoop:] + 36
    13  QuartzCore                          0x0023fa88 _ZN2CA7Display11DisplayLink8dispatchEyy + 128
    14  QuartzCore                          0x0023fbcd _ZN2CA7Display19EmulatorDisplayLink8callbackEP16__CFRunLoopTimerPv + 145
    15  CoreFoundation                      0x013f08c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    16  CoreFoundation                      0x013f1e74 __CFRunLoopDoTimer + 1220
    17  CoreFoundation                      0x0134e2c9 __CFRunLoopRun + 1817
    18  CoreFoundation                      0x0134d840 CFRunLoopRunSpecific + 208
    19  CoreFoundation                      0x0134d761 CFRunLoopRunInMode + 97
    20  GraphicsServices                    0x01f291c4 开发者_C百科GSEventRunModal + 217
    21  GraphicsServices                    0x01f29289 GSEventRun + 115
    22  UIKit                               0x0066fc93 UIApplicationMain + 1160
    23  AnimBear                            0x000931e2 main + 104
    24  AnimBear                            0x00002bad start + 53
)
terminate called after throwing an instance of 'NSException'

Also I used zwoptex for spritesheets.And don't have any error. This is the first time I am asking a question here. Anyone help please?

// HelloWorldLayer implementation
@implementation HelloWorldLayer
@synthesize bear=_bear;
@synthesize moveAction = _moveAction;
@synthesize walkAction = _walkAction;
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimBear.plist"];
        CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimBear.png"];
        [self addChild:spriteSheet];
        NSMutableArray *walkAnimFrames = [[NSMutableArray alloc] initWithObjects:@"0",nil];
        [walkAnimFrames insertObject:@"2"  atIndex:0];
        for(int i = 1; i <= 8; i++) {
            [walkAnimFrames addObject:
             [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
              [NSString stringWithFormat:@"bear%d.png", i]]];
        }
        CCAnimation *walkAnim = [CCAnimation 
                                 animationWithFrames:walkAnimFrames delay:0.1f];
            CGSize winSize = [CCDirector sharedDirector].winSize;
            self.bear =[CCSprite spriteWithSpriteFrameName:@"bear1.png"];
            _bear.position = ccp(winSize.width/2,winSize.height/2);
            self.walkAction =[CCRepeatForever actionWithAction:
                              [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
            [_bear runAction:_walkAction];
            [spriteSheet addChild:_bear];

    }
    return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    self.bear = nil;
    self.walkAction=nil;
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}
@end


According to the logs you are tring to use the file 'AnimBear.png' but it doesn't exist. Then into your code you trying to use the 'AnimBear.jpg'. So I don't know which one is missing, but you have an issue with your AnimBear sprite.

Also I will not recommend the use of jpg, use png files instead.


did u define NSMutableArray in this way:

 NSMutableArray *array1=[[NSMutableArray alloc] initWithObjects:@"0",nil];
 [array1 insertObject:@"2" atIndex:0];


because u insert @"2" string in walkAnimFrames.

if( (self=[super init])) {
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimBear.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimBear.png"];
    [self addChild:spriteSheet];
    NSMutableArray *walkAnimFrames = [[NSMutableArray alloc] init];

    for(int i = 1; i <= 8; i++) {
        [walkAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"bear%d.png", i]]];
    }
    CCAnimation *walkAnim = [CCAnimation 
                             animationWithFrames:walkAnimFrames delay:0.1f];
        CGSize winSize = [CCDirector sharedDirector].winSize;
        self.bear =[CCSprite spriteWithSpriteFrameName:@"bear1.png"];
        _bear.position = ccp(winSize.width/2,winSize.height/2);
        self.walkAction =[CCRepeatForever actionWithAction:
                          [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
        [_bear runAction:_walkAction];
        [spriteSheet addChild:_bear];

}

0

精彩评论

暂无评论...
验证码 换一张
取 消