关于可变范围的问题
issue regarding variable scope
我很难掌握变量范围的概念。什么是可以接受的,什么是不可接受的?我知道我省略了所有与图形相关的代码,我知道我有一个无限的游戏循环,但请耐心等待:
#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;
}
好!所以我的问题是关于变量"原始计时器"。计时器的重置超出了范围,那么我可以做些什么不同的事情?对不起,如果这是一个非常基本的问题。
你使用了不同的名称。 originalTime
和originalTimer
#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;
}
可能是
错别字,但有两个不同的变量originalTime
和originalTimer
更改下面的代码应该对您有用..
timer = originalTime; //reset timer.
相关文章:
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 使用基于范围的循环迭代矢量时出现问题
- 示例代码中使用分隔符将 std::string 拆分为 std::vector 的范围问题
- C++ 字符串订阅超出范围的问题
- 命名空间范围问题
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 断言失败错误,C++中的矢量下标超出范围问题
- 给定数组范围的选择排序问题
- 指针范围问题和返回类中封装的指针向量内的指针引用
- C++范围?将数组传递给函数时出现问题
- C++奇怪的问题.数据成员超出范围
- PlatformIO 的范围问题(?)
- C++. 面试. 关于返回 int* 值问题的范围
- 编程:原理与实践 使用C++ 第 4 章演练步骤 6:关于数字范围的一般问题
- 为什么BKDFHash不关心超出范围的问题?
- 访问函数中的使用声明:与范围相关的问题
- 函数返回本地变量,尽管变量不超出范围,没有编译器问题,并且代码执行
- 无法编译 - 范围问题