user3727511
user3727511

Reputation: 31

CCAnimation and moving Sprite in Cocos2d Properly with physicsBody?

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];

Upvotes: 1

Views: 227

Answers (1)

user3727511
user3727511

Reputation: 31

Simply fixed it by moving the

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

Upvotes: 1

Related Questions