1

I have written a code to display using CCLog the exact position of a sprite when a mousejoint moving it is released. Below is the Sprite.mm class and the ccTouchesEnded method (which is in the HelloWorldLayer.mm class). The sprite position is not updating, the output is constantly x: 0.00 and y: 0.00.

Sprite.mm:

-(id)addSprite:(CCLayer *)parentLayer
                 inWorld:(b2World *)world
{
PhysicsSprite *aSprite = [PhysicsSprite spriteWithFile:@"spriteIm.png"];

aSprite.tag = 1;
[parentLayer addChild:aSprite];

b2BodyDef spriteBodyDef;
spriteBodyDef.userData = aSprite;
spriteBodyDef.type = b2_dynamicBody;
CGSize s = [CCDirector sharedDirector].winSize;
spriteBodyDef.position = [Convert toMeters:ccp(s.width * 0.25,s.height-400)];
b2FixtureDef fixtureDef;
fixtureDef.density = 0.01;
b2CircleShape circleShape;
circleShape.m_radius = aSprite.contentSize.width/2 / PTM_RATIO;
fixtureDef.shape = &circleShape;

spriteBody = world->CreateBody( &spriteBodyDef );
spriteFixture = spriteBody->CreateFixture( &fixtureDef );

[aSprite setPhysicsBody:spriteBody];

return aSprite;
}

ccTouchesEnded:

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

if (mouseJoint)
{
  for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
        CCSprite *mySprite = (CCSprite *)b->GetUserData();
        if (mySprite.tag == 1) {
            CGPoint spritePosition = mySprite.position;
            CCLOG(@"the sprite position is x:%0.2f, y:%0.2f", spritePosition.x, spritePosition.y);
        }
    }
}        

world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}

Please help. I've been at it for a few days.

newbie
  • 23
  • 5
  • It would be easier to get the body like: b = mouseJoint->GetBodyB() than to loop over every body in the world. Not sure about the rest of the question sorry :) – iforce2d Aug 13 '12 at 12:53
  • Pls post your `- (void)update:(ccTime)dt` .. I think that's where the problem is. – Lukman Aug 13 '12 at 13:59
  • @Lukman: I solved the issue partly. Other sprites are also responding to it. That basically means my tag is not working. Do you see anything wrong with the way I am setting or accessing the sprite tag? – newbie Aug 13 '12 at 14:18

1 Answers1

0

I finally managed to get it working. I guess the x:0.00 and y:0.00 were because I was taking the position of the sprite not the body. The sprite is parented to the body so it was giving its position inside it's parent, which is 0,0. This is how I understand it. Here are the changes I made to the ccTouchesEnded code.

ccTouchesEnded:

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

if (mouseJoint)
{
    for(b2Body *b = mouseJoint->GetBodyB(); b; b=b->GetNext())    {
        if (b->GetUserData() != NULL)
        {
            CCSprite *mySprite = (CCSprite*)b->GetUserData();

            if (mySprite.tag == 1) {
                mySprite.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
                CCLOG(@"the sprite postion is x:%0.2f , y:%0.2f", mySprite.position.x, mySprite.position.y);

            }
        }
    }

    world->DestroyJoint(mouseJoint);
    mouseJoint = NULL;
}
}
newbie
  • 23
  • 5
  • the only hitch is other sprites are also responding to the touch. That leaves me with one conclusion, the tag is not really working. Does anyone see anything wrong with how I set or how I am accessing the tag? It looks okay to me. – newbie Aug 13 '12 at 14:21
  • 1
    Because all the sprites that are added using `addSprite:inWorld:` has tag = 1. – Lukman Aug 13 '12 at 14:41
  • @Lukman: the add sprite is only used once to add a one sprite. Another sprite which has a tag = 3 is also added. This sprite is also responding to the code I wrote in ccTouchesEnded. I end up creating a weldjoint between the two sprites. Could this also have an effect? How can I solve this? – newbie Aug 13 '12 at 15:03