0
votes

I'm trying to create 3 different difficulty levels (easy, medium and hard) for a game I'm trying to develop. I use a flag to differentiate the 3 (easy = 1, medium = 2, hard = 3). Now, I'm trying to figure out how to set the speed for easy as constant, then increase it after 20 collisions while medium, then increase after 10 when hard is selected. This is how I'm trying to implement it:

-(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;
}}

The counter does work but the speed does not seem to increment whenever counter is divisible by 20 or 10 and I can't get a constant speed for the easy level too. It starts off quite fast then gradually slows down. What am I doing wrong here? Please help.

1

1 Answers

0
votes

Someone suggested this to me and it works so I'll just post it in case anybody else need it:

- (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());
}}}

That's basically the only part of my code I made modifications to. I let the updateSpeed method do the computations to increase and set the max speed for the ball