Cocos2d iphone 检测触摸时,在cocos2d中展开测试点区域

Cocos2d iphone 检测触摸时,在cocos2d中展开测试点区域,cocos2d-iphone,Cocos2d Iphone,我的游戏里有很多精灵,它们在一个世界里都有身体。为了检测触摸,我做下一步: currentPosition = [[CCDirector sharedDirector] convertToGL: currentPosition]; b2Vec2 locationWorld = b2Vec2(currentPosition.x/PTM_RATIO, currentPosition.y/PTM_RATIO); for (b2Body* b = world->GetBo

我的游戏里有很多精灵,它们在一个世界里都有身体。为了检测触摸,我做下一步:

currentPosition = [[CCDirector sharedDirector] convertToGL: currentPosition];    
    b2Vec2 locationWorld = b2Vec2(currentPosition.x/PTM_RATIO, currentPosition.y/PTM_RATIO);

    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
    {
        b2Fixture *bf1 = b->GetFixtureList();
        if (bf1->TestPoint(locationWorld) )
        {
            CCSprite *tempSprite = (CCSprite *) b->GetUserData();
            if (tempSprite.tag==2    ) 
            {
现在,因为我的精灵的身体太小了,而且他正在移动,当它移动的时候很难触摸到它,所以我需要修改这个代码,以便检测到这个精灵周围的广阔区域。 如何将测试点扩展为+-更多50像素


非常感谢。

您可以将一个更大的夹具连接到车身上,并将夹具的传感器标志设置为true。传感器夹具不会改变任何物理特性,但您可以检查一个点是否在其边界内

您可以从SensorTest创建如下传感器夹具。h:

b2CircleShape shape;
shape.m_radius = 5.0f;
shape.m_p.Set(0.0f, 10.0f);

b2FixtureDef fd;
fd.shape = &shape;
fd.isSensor = true;
body->CreateFixture(&fd);

请参阅Box2D手册第6.3节和测试台中的SensorTest.h。

谢谢,但他们在那里只写了4行,这似乎很有趣,你能给我一个简单的例子吗?我的方式不好?请参阅我的编辑,我从附带的Box2D示例中添加了一个代码示例。它们是学习的好资源。