cocos2d iphone - How to access b2body of CCPhysicsSprite using its tag -


i`m working on platformer game , have character jumps on different platforms , until reaches end of level. 1 type of platform tree log floating on water. log moves , down along tide of water . each platform b2body. here how define platform :

                b2body *platformbody;             b2bodydef platforbodydef;             b2fixturedef platformfixdef;             b2polygonshape dynamicbox;             dynamicbox.setasbox(0.5f, 0.5f);             platforbodydef.type=b2_staticbody;             platforbodydef.position.set(blockplatx[i][j], blockplaty[i][j]);             platformbody= world->createbody(&platforbodydef);             platformfixdef.shape=&dynamicbox;                 platformsprite=[ccphysicssprite spritewithfile:@"treelog.png"];             dynamicbox.setasbox(platformsprite.texture.contentsize.width/ptm_ratio/2,platformsprite.texture.contentsize.height/ptm_ratio/2);             platformfixdef.friction=1;             platformfixdef.density=1;             platformfixdef.restitution=0;             platformbody->createfixture(&platformfixdef);             if(platforbodydef.position.y < watersprite.contentsize.height/2)             {                 platforbodydef.position.set(prevplatx + 300, watersprite.contentsize.height/2 + 10);                 cgpoint point=cgpointmake(platforbodydef.position.x,watersprite.contentsize.height/2);                 ccmoveto *watermove=[ccmoveto actionwithduration:3 position:point];                 point=cgpointmake(platforbodydef.position.x,watersprite.contentsize.height/2+ 10);                 ccmoveto *watermoveback=[ccmoveto actionwithduration:3 position:point];                 ccsequence* sequence = [ccsequence actions:watermove,watermoveback, nil];                 ccrepeatforever* repeat = [ccrepeatforever actionwithaction:sequence];                 [platformsprite runaction:repeat];             }             [platformsprite setptmratio:ptm_ratio];             [platformsprite setb2body:platformbody];             [platformsprite setposition:cgpointmake(platforbodydef.position.x, platforbodydef.position.y)];             [self addchild:platformsprite z:4 tag: 10000 + i*100 + j]; 

i using loop ,so create more 1 of these platforms .

but problem when sprite runs action sequence , position of b2body associated does`nt change , causes lots of problems. there anyway can access b2body of sprite using tag , change position of body instead?

first thing sprite follows body , not other way round. when repositioning b2body via settransform have careful can find odd behaviours. collision not being performed in proper fashion, if moved body intersect else crazy stuff can happen.

why using sequence move body on water. better off using prismatic joint push along , @ end of track place sensor revert motor of joint move in other direction. let physics engine deal movement. way allow system work in way intended , sprite updates position body automatically.

edit: advised using visual box2d editor. using cocos2d engine, there quite few choose from. recommend "really useful box2d editor" or "r.u.b.e." in short.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -