我正在尝试在触摸移动时移动精灵。 但是当两个精灵在那里时,我会触摸到两个精灵。 这就是精灵无法正常移动的原因。
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
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);
for(int i=0;i<[mutArrFixtures count];i++)
{
b2Fixture *fixture;
[[mutArrFixtures objectAtIndex:i] getValue:&fixture];
if (fixture->TestPoint(locationWorld)){
for(int j=0; j<[mutArrPaddleBody count]; j++)
{
b2Body *body;
[[mutArrPaddleBody objectAtIndex:j] getValue:&body];
b2MouseJointDef md;
if(body == fixture->GetBody())
{
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);
}
}
}
}
}
我有一个 b2Fixture mutArrFixtures
数组,还有一个 b2body 的数组mutArrPaddleBody
。 但是在这里,如果我触摸第二个精灵,我会触摸第一个精灵和第二个精灵..两个精灵位置是相同的...
在触摸功能中,检查精灵的标签。如果这是你的右精灵,那就移动。给我看一些你用于触摸移动的代码。
.......
将这些代码替换为下一个代码
b2Body *body;
[[mutArrPaddleBody objectAtIndex:j] getValue:&body];
b2MouseJointDef md;
if(body == fixture->GetBody())
跟
b2Body* body = fixture->GetBody();
CCSprite *sprite = (CCSprite*)body->GetUserData();
if( sprite && sprite.tag == kTagHero)
{
}
确保你为你的移动精灵添加了标签kTagHero。
....
enum gameTag {
kTagHero = 1001
};
并分配 sprite.tag = kTagHero......