看不见的身体?

时间:2014-01-10 13:28:34

标签: box2d cocos2d-x rigid-bodies

在我的游戏中,我有大约20个街区,角色可以走路。每个街区都有一个身体,角色也有一个身体。

有时当我的角色走路时,它被某些东西挡住了。当发生这种情况时,我可以通过走到另一边稍微“删除”看不见的墙,然后再回去。

据我所知,我没有做错任何事。这是我用来绘制块/字符的代码:

字符:

    b2BodyDef raymanBodyDef;


    raymanBodyDef.type = b2_dynamicBody;
    raymanBodyDef.position.Set(winsize.width/2/PTM_RATIO, winsize.height/2/PTM_RATIO);


    raymanBody = world->CreateBody(&raymanBodyDef);
    bodyUserData *bud = new bodyUserData;
    bud->bodytype = 1;
    bud->sprite = rayman;
    raymanBody->SetUserData(bud);

    b2PolygonShape raymanShape;
    raymanShape.SetAsBox(16.5/ PTM_RATIO, 32.5/PTM_RATIO);



    b2FixtureDef raymanShapeDef;
    raymanShapeDef.shape = &raymanShape;
    raymanShapeDef.density = 10.0f;
    raymanShapeDef.friction = 0.0f;
    raymanShapeDef.restitution = 0;
    raymanBody->SetFixedRotation(true);
    raymanBody->CreateFixture(&raymanShapeDef);

块:

Block *b = new Block(40, 125);
this->addChild(b->sprite);
blocks.push_back(b);
b2Body *blockbody;
    for(int i = 0; i<blocks.size();i++)
    {
        cocos2d::CCLog("%d", i);
        b2BodyDef def = blocks[i]->blockbodydef;
        bodyUserData *bud2 = new bodyUserData;
        bud2->bodytype = 3;
        bud2->sprite = blocks[i]->sprite;
        bud2->canDelete = false;

        blockbody = world->CreateBody(&def);
        blockbody->SetUserData(bud2);


        blockbody->CreateFixture(&blocks[i]->blockshapedef);
    }

块构造函数:

Block::Block(float x, float y)
{
sprite = cocos2d::CCSprite::create("Block.png", cocos2d::CCRect(0,0,50,50));
sprite->setPosition(ccp(x,y));

blockbodydef.type = b2_kinematicBody;
blockbodydef.position.Set(x/PTM_RATIO,y/PTM_RATIO);
//blockbodydef.userData = sprite;



blockPoly.SetAsBox(50/PTM_RATIO , 50/ PTM_RATIO);

blockshapedef.shape = &blockPoly;
blockshapedef.density = 2.0f;
blockshapedef.friction = 0.0f;
blockshapedef.restitution = 0.0f;
}

字符精灵为33x65,块精灵为50x50。

0 个答案:

没有答案