2014-01-09 72 views
0

我无法理解变量范围的概念。什么是可以接受的,什么是不可接受的?我知道,我已经离开了所有相关的代码的图形,而且我知道我有无限的游戏循环,但包涵:有关变量范围的问题

#include "LList.h" 
#include "Snake.h" 
#undef main 



int main() 
{ 

float dt;    // time since last update. 
int start_time; 
bool paused = false; 
float originalTime = 1.0f; 
float timer = originalTime; 
Snake p1Snake(10, false); 



    // Start the 'stopwatch' 
    start_time = SDL_GetTicks(); 

    /////////////////////// 
    // The 'game loop' // 
    /////////////////////// 
    while (!done) 
    { 
     ////////////////////// 
     // Update variables // 
     ////////////////////// 
     // Update the dt value (to be the time since the last update) 
     dt = (SDL_GetTicks() - start_time)/1000.0f; 
     start_time = SDL_GetTicks(); 

      //increment the movement timer 
     timer-=dt; 
     if(timer<=0) When timer hits zero the snake is moved north. 
      { 
       p1Snake.goNorth(); 
       timer = originalTimer; //reset timer. 
      } 
    } 

    return 0; 
} 

好吧!所以我的问题是关于变量'originalTimer'。它在定时器重置的范围之外,所以我能做些什么不同?对不起,如果这是一个非常基本的问题。

+0

你的意思是'originalTime'? –

+1

假设你实际上是指'originalTime',那么你为什么认为它超出了范围? –

+2

'#undef main'看起来很可疑,我甚至无法描述它。 – chris

回答

1

可能是笔误,但有两个不同的变量originalTimeoriginalTimer

在下面的代码更改应该为你工作..

timer = originalTime; //reset timer. 
2

您使用了不同的名称。 originalTimeoriginalTimer

#include "LList.h" 
#include "Snake.h" 
#undef main 



int main() 
{ 

    float dt;    // time since last update. 
    int start_time; 
    bool paused = false; 
    float originalTimer = 1.0f; //Changed to originalTimer 
    float timer = originalTimer; //Changed to originalTimer 
    Snake p1Snake(10, false); 

    // Start the 'stopwatch' 
    start_time = SDL_GetTicks(); 

    /////////////////////// 
    // The 'game loop' // 
    /////////////////////// 
    while (!done) 
    { 
     ////////////////////// 
     // Update variables // 
     ////////////////////// 
     // Update the dt value (to be the time since the last update) 
     dt = (SDL_GetTicks() - start_time)/1000.0f; 
     start_time = SDL_GetTicks(); 

     //increment the movement timer 
     timer-=dt; 
     if(timer<=0) //When timer hits zero the snake is moved north. 
     { 
      p1Snake.goNorth(); 
      timer = originalTimer; //reset timer. 
     } 
    } 

    return 0; 
}