Apologies for being a little too vague in my original post - I hope this one's not too long-winded. :) I think I'm actually just confused about how the UserData is handled altogether. PhilM, your post helped me understand a little more and Codemmatic, your instinct was right, I was referencing a private instance variable that I just needed to declare in my .h file. I no longer get any warnings, but I still can't get it to work.
I've also been reading badawe's post on UserData and AtlasSprites and it just hasn't "clicked" yet for me. Any other places I should look to get a better understanding?
Here's all the relevant (I think) code that I'm using. Again, my goal seems pretty simple. I have two sets of bodies in the world, and I only want to be able to "grab" ones that match the mySprite UserData. I've been trying to use something like:
if (grabbedBody->GetUserData() == mySprite) { // create joint }
But the joint never gets created, leading me to believe the condition can't find any UserData that matches. Lots of code to follow, let me know if you need to see more.
- (void) addSpritesWithCoords {
myArray = [myGame.posArray mutableCopy];
otherArray = [myGame.otherPosArray mutableCopy];
for (NSValue *value in myArray) {
CGPoint point = [value CGPointValue];
AtlasSpriteManager *mgr = (AtlasSpriteManager *) [self getChildByTag:kTagSpriteManager];
mySprite = [AtlasSprite spriteWithRect:CGRectMake(1, 1, 75, 39) spriteManager:mgr];
[mgr addChild:mySprite];
mySprite.position = ccp(point.x, point.y);
b2BodyDef bodyDef;
bodyDef.position.Set(point.x/PTM_RATIO, point.y/PTM_RATIO);
bodyDef.userData = mySprite;
bodyDef.fixedRotation = FALSE;
b2Body *body = world->CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.8f, 0.4f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 5.0f;
fixtureDef.friction = 10.0f;
body->CreateFixture(&fixtureDef);
body->SetMassFromShapes();
}
// then I do the same thing, but for all values in otherArray
// assigning different UserData to the bodyDef.
}
class QueryCallback : public b2QueryCallback
{
public:
QueryCallback(const b2Vec2& point)
{
m_point = point;
m_fixture = NULL;
}
bool ReportFixture(b2Fixture* fixture)
{
b2Body* body = fixture->GetBody();
if (body->IsStatic() == false)
{
bool inside = fixture->TestPoint(m_point);
if (inside)
{
m_fixture = fixture;
// We are done, terminate the query.
return false;
}
}
// Continue the query.
return true;
}
b2Vec2 m_point;
b2Fixture* m_fixture;
};
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {
UITouch *touch = [touches anyObject];
CGPoint convertedTouch = [self convertTouchToNodeSpace: touch];
b2Vec2 convertedPoint(convertedTouch.x/PTM_RATIO,convertedTouch.y/PTM_RATIO);
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)
{
// Only create grabbedBody if body->UserData == mySprite ???
// or set grabbedBody, then check for its UserData ???
grabbedBody = callback.m_fixture->GetBody();
b2MouseJointDef md;
md.body1 = groundBody;
md.body2 = grabbedBody;
md.target = convertedPoint;
#ifdef TARGET_FLOAT32_IS_FIXED
md.maxForce = (grabbedBody->GetMass() < 16.0)?
(1000.0f * grabbedBody->GetMass()) : float32(16000.0);
#else
md.maxForce = 1000.0f * grabbedBody->GetMass();
#endif
m_mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
grabbedBody->WakeUp();
return YES;
}
}
return kEventHandled;
}
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {
// drag methods
UITouch* touch = [touches anyObject];
CGPoint convertedTouch = [self convertTouchToNodeSpace: touch];
b2Vec2 convertedPoint(convertedTouch.x/PTM_RATIO,convertedTouch.y/PTM_RATIO);
if (m_mouseJoint)
{
// Or would it go here ???
// if m_mouseJoint->GetBody2()->GetUserData() == mySprite ???
m_mouseJoint->SetTarget(convertedPoint);
}
}
return kEventHandled;
}