1

It appears that my character seems to animate as expected using CCAnimation and Cocoa2d 3.x. The problem is when I set my CCAnimatedSprite to have the physicsBody, just as my previous CCSprite did, I get a crash

Aborting due to Chipmunk error: Body's moment is NaN.
    Failed condition: body->i == body->i && body->i_inv == body->i_inv
    Source:/Users/Jason/Desktop/RPGGame/RPG/Libraries/Chipmunk/chipmunk/src/cpBody.c:114

However if I delete this line:

mainChar.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, mainChar.contentSize} cornerRadius:0];

The crash goes away, but the physics aren't applied to my CCAnimatedSprite, how do I fix this, and why is this line causing it?

Full Code for initializing my CCAnimatedSprite:

 CCTiledMapObjectGroup *objects0  =    [levelOneMap objectGroupNamed:@"mainChar"];
    NSMutableDictionary *startPoint0 =    [objects0 objectNamed:@"startPosition"];
    int x0 = [[startPoint0 valueForKey:@"x"] intValue];
    int y0 = [[startPoint0 valueForKey:@"y"] intValue];
    self.mainChar     = [CCAnimatedSprite animatedSpriteWithPlist:@"AnimateChar.plist"];
    mainChar.position = ccp(x0,y0);
    mainChar.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, mainChar.contentSize} cornerRadius:0];
    mainChar.physicsBody.collisionGroup = @"groupPlayer";
    mainChar.physicsBody.collisionType = @"collisionPlayer";
    [mainChar addAnimationwithDelayBetweenFrames:0.1f name:@"AnimateChar"];
    [mainChar setFrame:@"AnimateChar-1.png"];

    [self.physicsWorldNode addChild: mainChar];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I haven't looked at Cocos2d in a long time, but what does `(CGRect){CGPointZero, mainChar.contentSize}` NSLog out as? I'm guessing that the contentSize is 0,0 at that point maybe? – Cooper Buckingham Jun 17 '14 at 18:54

1 Answers1

1

Simply fixed it by moving the

[mainChar setFrame] line above the physicsBody, and it worked fine. Rookie mistake ^^