gpt4 book ai didi

c++ - 使用 SDL2 的函数返回错误的枚举值

转载 作者:行者123 更新时间:2023-11-28 05:49:07 27 4
gpt4 key购买 nike

我在 C++ 中使用 SDL2。

我做了一个 Player类(class)。它包含来自 Input 的对象类。

我做了两个 Player对象。

Player构造函数 Player() ,我调用setControls()关于成员(member)Input对象 m_Controls .然后我调用keyPressed()在同一个对象上。这两个函数都属于 Input 类.

我的“错误”在第 89 行,我调用了 m_Controls.keyPressed(SDL_SCANCODE_W) .

函数循环遍历 Input成员数组 m_Keys -- 玩家可以按下的键。如果它迭代的元素匹配 SDL_Scancode传递给 keyPressed() , 它应该从 Controls 返回相应的值枚举。

#include <SDL2/SDL.h>
#include <iostream>

enum Controls {
CONTROLS_INVALID= -1,
CONTROLS_QUIT_GAME,
CONTROLS_UP,
CONTROLS_RIGHT,
CONTROLS_DOWN,
CONTROLS_LEFT,
CONTROLS_CONFIRM
};


class Input {
private:
enum {m_NumberOfKeys= 6};
SDL_Scancode m_Keys[m_NumberOfKeys];
Controls m_PressedKey;
public:
Input(){}
~Input(){}

void setControls(SDL_Scancode up, SDL_Scancode right, SDL_Scancode down, SDL_Scancode left, SDL_Scancode confirm){
m_Keys[0]= SDL_SCANCODE_ESCAPE;
m_Keys[1]= up;
m_Keys[2]= right;
m_Keys[3]= down;
m_Keys[4]= left;
m_Keys[5]= confirm;
}

Controls keyPressed(SDL_Scancode userInput){
std::cout << "userInput: " << userInput << std::endl;
for (int i = 0; i < m_NumberOfKeys; ++i){
std::cout << i << ' ' << m_Keys[i] << std::endl;
if (m_Keys[i] == userInput){
switch (i) {
case CONTROLS_QUIT_GAME:
m_PressedKey= CONTROLS_QUIT_GAME;
break;
case CONTROLS_UP:
m_PressedKey= CONTROLS_UP;
break;
case CONTROLS_RIGHT:
m_PressedKey= CONTROLS_RIGHT;
break;
case CONTROLS_DOWN:
m_PressedKey= CONTROLS_DOWN;
break;
case CONTROLS_LEFT:
m_PressedKey= CONTROLS_LEFT;
break;
case CONTROLS_CONFIRM:
m_PressedKey= CONTROLS_CONFIRM;
break;
default:
m_PressedKey= CONTROLS_INVALID;
break;
}
}
}
std::cout << "m_PressedKey: " << m_PressedKey << std::endl;
return m_PressedKey;
}
};


class Player {
private:
static int s_IdGenerator;
int m_Id;
Input m_Controls;
public:
Player() {
m_Id= s_IdGenerator++;
std::cout << "Making player " << m_Id << std::endl;
switch (m_Id) {
case 1:
m_Controls.setControls(SDL_SCANCODE_W, SDL_SCANCODE_D, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_SPACE);
break;
case 2:
m_Controls.setControls(SDL_SCANCODE_UP, SDL_SCANCODE_RIGHT, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_SPACE);
break;
default:
break;
}

m_Controls.keyPressed(SDL_SCANCODE_W);
std::cout << "==\n";
}
~Player(){}



Input& getControls(){
return m_Controls;
}
};

int Player::s_IdGenerator= 1;


int main(int argc, char **argv) {
SDL_Init(SDL_INIT_EVERYTHING);


Player player1;
Player player2;


return 0;
}

鉴于上面的代码,keyPressed()在我制作 `player`` 后返回以下内容:

Making player 1
userInput: 26
0 41
1 26
2 7
3 22
4 4
5 44
m_PressedKey: 1

目前还不错。 SDL_SCANCODE_Wplayer1 之一的控件,所以 m_PressedKey正确设置为 1 .但这是 player2 时的输出已创建:

Making player 2
userInput: 26
0 41
1 82
2 79
3 81
4 80
5 44
m_PressedKey: 0

SDL_SCANCODE_W不是 player2 的一部分的控件,我要 m_PressedKey设置为 -1 .它设置为 0相反。

我必须更改什么才能使此代码集 m_PressedKey-1什么时候keyPressed()得到一个无效的 SDL_Scancode

最佳答案

在ctor或keyPressed方法中初始化它; m_PressedKey = CONTROLS_INVALID;默认的 ctors 是邪恶的:)

关于c++ - 使用 SDL2 的函数返回错误的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com