奇怪的行为发现了 c++ sdl2
Strange behavior spotted c++ sdl2
使用 sdl2,我设法为游戏构建了"即将成为意大利面条"的类。然而,当涉及到使用类的函数时,我偶然发现了这种怪异之处。
class Player{
public:
Player();
const SDL_Rect *getPositionPtr() const { return &position; }
const SDL_Rect * getClip(){ return &clip; }
void eventHandle(SDL_Event & e);
void move();
private:
SDL_Rect position;
SDL_Rect clip;
float velocity;
bool leftkeydown;
bool rightkeydown;
};
Player::Player(){
position = {100, 300, 64, 64};
clip = {0, 0, 64, 64};
velocity = 0.3;
leftkeydown = false;
rightkeydown = false;
}
void Player::eventHandle(SDL_Event & e){
if( e.type == SDL_KEYDOWN && e.key.repeat == 0 ){
switch( e.key.keysym.sym ){
case SDLK_a:
leftkeydown = true;
break;
case SDLK_d:
rightkeydown = true;
break;
}
}
else if( e.type == SDL_KEYUP && e.key.repeat == 0 ){
//Adjust the velocity
switch( e.key.keysym.sym ){
case SDLK_a:
leftkeydown = false;
break;
case SDLK_d:
rightkeydown = false;
break;
}
}
}
void Player::move(){
if(leftkeydown) position.x -= velocity;
if(rightkeydown) position.x += velocity; // <----- problem here
}
LeftKeyDown 似乎按预期工作,但 RightKeyDown 不会对 position.x 变量执行任何操作。
知道为什么它不递增吗?
正如@keltar所称赞的那样,这是因为在int +(float <0(处,int保持不变,因为它将结果(100.3(从float转换为int(100((这是因为其中一个值是int(,因此位置x将保持不变,除非您将速度设置为int或大于0。
相关文章:
- 如何创建事件驱动的 SDL2 应用程序
- 使用 SDL2 和SDL_image C++加载图像
- 将 SDL2 与 CMake 链接起来
- SDL2 透明背景
- 如何在使用 SDL2 的 Eclipse 中构建 c++ 程序
- 不带轮询的 SDL2 事件回调
- C++ SDL2:如何将矩形渲染到多个视口中
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- SDL2 二维纹理分配/池化
- 如何使用 sdl2 快速绘制像素网格?
- 看不到文件'SDL2.lib'
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- SDL2 无法正确绘制矩形
- 实施英特尔实感和 SDL2 时出现问题
- 找不到 sdl2.dll
- SDL2 程序只写入部分字符串
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 在另一台 PC 上执行程序时加载 SDL2 共享库时出错
- SDL2 (C++) 如何使图像变小
- SDL2 不是基于 VS 代码构建的