How to access b2body of CCPhysicsSprite using its tag

238 Views Asked by At

i`m working on a platformer game , i have A character who jumps on different platforms , until he reaches the end of the level. one type of platform is a tree log floating on water. the log slowly moves up and down along the tide of water . each platform is actually a b2body. here is how i define a 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 am using a loop ,so i create more than one of these platforms .

but the problem is that when the sprite runs the action sequence , the position of the b2body associated with it does`nt change and this obviously causes lots of problems. is there anyway i can access the b2body of the sprite using its tag and change the position of the body instead?

1

There are 1 best solutions below

1
On BEST ANSWER

First thing is that the sprite follows the body and not the other way round. When you are repositioning the b2Body via setTransform you have to be very careful as you can find some really odd behaviours. The collision is not being performed in the proper fashion, so if the moved body will intersect with something else some crazy stuff can happen.

Why are you using sequence to move the body on the water. You are much better off using a prismatic joint to push it along and at the end of the track place a sensor which will revert the motor of the joint to move it in the other direction. Let the physics engine deal with the movement. This way you allow the system to work in the way it was intended and the sprite will updates its position to the body automatically.

Edit: I would also advised using a visual box2d editor. You are using Cocos2d as an engine, so there is quite few to choose from. I would personally recommend "Really Useful Box2D Editor" or "R.U.B.E." in short.