1

我正在尝试在触摸移动时移动精灵。但是当有两个精灵时,我会接触到两个精灵。这就是精灵不能正常移动的原因。

- (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。但是在这里,如果我触摸第二个精灵,我会触摸到第一个精灵和第二个精灵..两个精灵的位置是相同的......

4

1 回答 1

1

在触摸功能中,检查精灵的标签。如果那是您的正确精灵,请移动。给我看一些你用于触摸移动的代码。

…………

用下一个替换这些代码

            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 ......

于 2012-08-17T07:19:19.500 回答