2013-03-05 38 views
0

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

-(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,我不能得到一个恒定的速度轻松水平了。它起步非常快,然后逐渐变慢。我在这里做错了什么?请帮忙。

回答

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方法进行计算来增加并设置球的最大速度