Swap UserData on double tap (iPhone cocos2d)

Hi all, I think this question has more to do with box2d than cocos2d, but apologies in advance if it's not on-topic. I'm running cocos2d 0.8.1 and box2d 2.1.0, compiling against SDK 2.2.1.

I have two arrays of CGPoints. For each one, I use fast enumeration to create an AtlasSprite (cocos2d object) and a b2Fixture for each point. I give each array's objects distinct UserData. With the help of some folks on the cocos2d forum, I've been able to figure out how to assign the UserData and later use it to filter conditional statements.

I'd like to extend this further and allow a user to double tap an object (b2Fixture with associated AtlasSprite) and make that object show a different AtlasSprite. I've been successful in adding the Sprite to the world and attaching it to the fixture, but afterwards, the "old" Sprite that should have been replaced by the selected Sprite remains on screen. But the old Sprite is not attached to a fixture any more... Here's some of the relevant code (I know it's a lot to read, sorry):

Code:
- (void) addSprites {
      
   myArray = [myGame.posArray mutableCopy];
   secondArray = [myGame.posArrayTwo mutableCopy];
   
   for (NSValue *value in myArray) {
      CGPoint point = [value CGPointValue];
      
      AtlasSpriteManager *mgr = (AtlasSpriteManager *) [self getChildByTag:kTagSpriteManager];
      mySprite = [AtlasSprite spriteWithRect:CGRectMake(1, 75, 74, 37) spriteManager:mgr];
      
      [mgr addChild: mySprite];
      
      mySprite.position = ccp(point.y, point.x);
      
      b2BodyDef bodyDef;
      bodyDef.position.Set(point.x/PTM_RATIO, point.y/PTM_RATIO);
      bodyDef.userData = mySprite;
      b2Body *body = world->CreateBody(&bodyDef);
      b2PolygonShape dynamicBox;
      dynamicBox.SetAsBox(0.7f, 0.3f);
      
      b2FixtureDef fixtureDef;
      fixtureDef.shape = &dynamicBox;
      fixtureDef.density = 5.0f;
      fixtureDef.friction = 10.0f;
      b2Fixture *fixture2;
        fixture2 = body->CreateFixture(&fixtureDef);
        fixture2->SetUserData(@"mySprite");
      body->SetMassFromShapes();
      
   }
   
   for (NSValue *value in secondArray) {
      // do the same thing for this array, assigning different UserData
   }
}

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   
   UITouch *touch = [touches anyObject];
   NSUInteger tapCount = [touch tapCount];
   CGPoint convertedTouch = [self convertTouchToNodeSpace: touch];
   b2Vec2 convertedPoint(convertedTouch.x/PTM_RATIO,convertedTouch.y/PTM_RATIO);
   
   if (tapCount == 2) {
      if (m_mouseJoint != NULL) {return YES;}
      
      // Make a small box.
      b2AABB aabb;
      b2Vec2 d;
      d.Set(0.001f, 0.001f);
      aabb.lowerBound = convertedPoint - d;
      aabb.upperBound = convertedPoint + d;
      
      // Query the world for overlapping shapes.
      QueryCallback callback(convertedPoint);
      world->QueryAABB(&callback, aabb);
      
      if (callback.m_fixture) {  // get results from the callback class, returning a fixture at point of user's touch
         
         AtlasSprite *ud = (AtlasSprite *)callback.m_fixture->GetUserData();
         if ([ud isEqual:@"mySprite"]) {               
               
            callback.m_fixture->SetUserData(@"mySpriteSel");
               
            b2Vec2 p = callback.m_fixture->GetBody()->GetPosition();
               
            AtlasSpriteManager *mgr = (AtlasSpriteManager *) [self getChildByTag:kTagSpriteManager];
            mySpriteSel = [AtlasSprite spriteWithRect:CGRectMake(1, 1, 74, 73) spriteManager:mgr];

            [mgr addChild:mySpriteSel];
            mySpriteSel.position = ccp(p.x * PTM_RATIO, p.y * PTM_RATIO);
               
            callback.m_fixture->GetBody()->SetUserData(mySpriteSel);
         }
      }      
   }
   return kEventHandled;
}

- (void) tick: (ccTime) dt {

   int32 velocityIterations = 10;
   int32 positionIterations = 10;
   
   // Instruct the world to perform a single step of simulation. It is
   // generally best to keep the time step and iterations fixed.
   world->Step(dt, velocityIterations, positionIterations);
   
   //Iterate over the bodies in the physics world
   for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
   {
      if ([GameHUD sharedHUD].toggleOn == YES) {
         b->ApplyImpulse([self randomVect], b2Vec2(0,0));      
      }

      //Synchronize the AtlasSprites position and rotation with the corresponding body
      
      AtlasSprite* myActor = (AtlasSprite*)b->GetUserData();
      myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
      myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
   }
}



I've found that the
Code:
callback.m_fixture->GetBody()->SetUserData(mySpriteSel);
line has to go after adding the mySpriteSel child to the layer. If I don't do that, its position doesn't get updated by the tick: method. But where it is now, it leaves the mySprite object in place as the rest of the bodies get updated.

Is there something obvious that I'm doing wrong here? Maybe my tick: method isn't looking for the right bodies? Or is this even the way UserData was intended to be used?

Any help is greatly appreciated, I'm still learning so other links/resources are welcome too. Sorry if this is too specific to cocos2d for this forum... Thanks!

Original thread: https://box2d.org/forum/viewtopic.php?t=3849