I have added 7 sprites and give them tag and zorder.. but i can't get touch on third object and at last there is only one object remained then i get touch of it.. But when i added 6 sprites i'll get touch properly. all sprites positions are same but zorder is different.. i can't understand what is problem.. I have used query callback class to get touch.. here i add some of my code..
code of adding sprite..
zIndex = 7;
for(int i = 0; i<zIndex; i++)
{
CCSprite *paddle = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%d.png",i+1]];
paddle.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:paddle z:zIndex-i tag:i+1];
[mutArrSprites addObject:paddle];
NSLog(@"Z = %d tag = %d ",zIndex-i , i+1);
}
for(CCSprite *sprite in mutArrSprites)
{
[self addUserDataToNode:sprite];
}
in touchbegan method
if (_mouseJoint != NULL) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = locationWorld - d;
aabb.upperBound = locationWorld + d;
// Query the world for overlapping shapes.
QueryCallback callback(locationWorld);
_world->QueryAABB(&callback, aabb);
b2Body *body = callback.m_object;
if (body)
{
//pick the body
CCSprite *sprite = (CCSprite *)body->GetUserData();
[self reorderChild:sprite z:zIndex++];
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = body;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f * body->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
body->SetAwake(true);
}
Use below method to get the top most touched object based on z-order
-(b2Body *) getTopTouchBody:(b2Vec2)location{
b2Body *touchObject;
int zOrder = 0;
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
b2Fixture *f = b->GetFixtureList();
BOOL isinsize = f->TestPoint(location);
CCSprite *sprite = (CCSprite *)b->GetUserData();
if (isinsize) {
if (sprite.zOrder > zOrder) {
zOrder = sprite.zOrder;
touchObject = b;
}
}
}
return touchObject;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With