iphone - cocos2d - CCSprite not aligned with b2Body -


if create sprite , body first batch of code, sprite centered on top of circular body. if create sprite second batch of code, bottom of circular body attached top of sprite. both batches of code use following

update 1 - changed createbody takes position input. think send correct position in each cause, still see top of sprite connected bottom of body in second batch of code.

createbody method:

- (void)createbody : (nsnumber *)xpos  ypos:(nsnumber *)ypos {  float radius = 16.0f;  b2bodydef bd; bd.type = b2_dynamicbody; bd.lineardamping = 0.1f; bd.fixedrotation = true; bd.position.set([xpos doublevalue]/ptm_ratio, [ypos doublevalue]/ptm_ratio);  body = _world->createbody(&bd);  b2circleshape shape; shape.m_radius = radius/ptm_ratio;  b2fixturedef fd; fd.shape = &shape; fd.density = 1.0f; fd.restitution = 0.0f; fd.friction = 0.2;   body->createfixture(&fd); 

}

first batch of code -

hero.mm -

- (id)initwithworld:(b2world *)world {     if ((self = [super initwithspriteframename:@"animal.png"])) {       nsnumber *xpos = [nsnumber numberwithdouble:self.position.x];       nsnumber *ypos = [nsnumber numberwithdouble:self.position.y];       [self createbody:xpos ypos:ypos];     } } 

hellowworld.mm

  _hero = [[[hero alloc] initwithworld:_world] autorelease];     [_terrain.batchnode addchild:_hero]; 

second batch of code -

hero.mm -

 ccsprite *sprite = [ccsprite spritewithspriteframename:@"animal.png"];  [self addchild:sprite];  nsnumber *xpos = [nsnumber numberwithdouble: sprite.position.x];  nsnumber *ypos = [nsnumber numberwithdouble: sprite.position.y];  [self createbody:xpos ypos:ypos]; 

the main difference between 2 batches of code first uses initwithspriteframename , second uses spritewithspriteframename. know how center sprite on body using spritewithspriteframename?

the strange thing line in createbody method

     bd.position.set([xpos doublevalue]/ptm_ratio, [ypos doublevalue]/ptm_ratio); 

doesn't seem position body on sprite, position body , sprite in world coordinates.

the difference notice follows.

in first case initialize hero sprite , call createbody in turn uses self.position, ie. position of sprite itself.

in second case, add sprite child - createbody uses position of parent object , not sprite adding.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -