在box2d中设置精灵的恒定和增加速度

时间:2013-03-05 03:53:16

标签: c++ cocos2d-iphone box2d

我正在尝试为我正在尝试开发的游戏创建3个不同的难度级别(简单,中等和难度)。我用一个标志来区分3(easy = 1,medium = 2,hard = 3)。现在,我试图弄清楚如何将速度设置为常数,然后在中等后20次碰撞时增加速度,然后在选择硬时10后增加速度。这就是我试图实现它的方式:

-(id)init)
{vel = 8;
counter = 0;}

-(void)update:(ccTime)dt{
_world->Step(dt, vel, 10);

    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
        if (b->GetUserData() != NULL) {
            CCSprite *sprite = (CCSprite *)b->GetUserData();
            sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
                              b->GetPosition().y * PTM_RATIO);
            sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        }
    }
    if((contact.fixtureA == _paddleFixture && contact.fixtureB == _ballFixture) || (contact.fixtureA == _ballFixture && contact.fixtureB == _paddleFixture))
    {
        counter++;
        [self updateSpeed];
    }
}

-(void)updateSpeed{
if(diffLevel == 2)
{
    if(counter%20 == 0)
    {
        vel = vel + 5;
    }
}
else if(diffLevel == 3)
{
    if(counter%10 == 0)
    {
        vel = vel + 10;
    }
}
else
{
    vel = 8;
}}

计数器确实有效但速度似乎没有增加,只要计数器可以被20或10整除,我也无法获得一个简单的速度。它开始很快然后逐渐减慢。我在这做错了什么?请帮忙。

1 个答案:

答案 0 :(得分:0)

有人向我建议并且它有效,所以我会发布它以防其他人需要它:

- (void)update:(ccTime) dt {
    _world->Step(dt, 10, 10);
    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
        if (b->GetUserData() != NULL) {
            CCSprite *sprite = (CCSprite *)b->GetUserData();
            if(sprite.tag == 2)
            {
                int maxSpeed = 140;

                b2Vec2 dir = b->GetLinearVelocity();
                dir.Normalize();

                float currentSpeed = dir.Length();
                float accelerate = vel;

                if(currentSpeed < maxSpeed)
                {
                    b->SetLinearVelocity(accelerate * dir);
                }

                sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
                                      b->GetPosition().y * PTM_RATIO);
                sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}}}

这基本上是我修改代码的唯一部分。我让updateSpeed方法进行计算以增加并设置球的最大速度

相关问题