gpt4 book ai didi

iphone - cocos2d 和 box2d : sprite touch not get properly

转载 作者:行者123 更新时间:2023-12-03 18:45:50 28 4
gpt4 key购买 nike

我添加了 7 个 Sprite ,并给了它们标签和 zorder.. 但我无法触摸第三个对象,最后只剩下一个对象,然后我就触摸到了它.. 但是当我添加 6 个 Sprite 时,我会得到正确的接触。所有 Sprite 位置都相同,但 zorder 不同..我不明白有什么问题..我已经使用查询回调类来联系..在这里我添加一些我的代码..

添加 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 方法

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);
}

最佳答案

使用下面的方法根据 z 顺序获取最上面触摸的对象

-(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;
}

关于iphone - cocos2d 和 box2d : sprite touch not get properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017631/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com