由参数C++SDL实例化的播放器类
Player classes instantiated by argument C++ SDL
所以,再次制作一个pong克隆,并遇到一个凸起。我正在为我的状态机创建game
类gamestate,在定义中,我实例化了我为不同的玩家划桨创建的类的两个类,并给它们两个不同的参数,这些参数将决定该类是成为玩家一还是玩家二对象。
但程序似乎不允许我在类定义中传递参数。我试图将类实例化放在game
类构造函数中,但事件/逻辑/渲染函数无法访问桨板类。这让我认为它必须在game
类定义中。这是出现问题的代码。
class Ball
{
public:
void events();
void logic();
void render();
Ball(int server);
~Ball();
};
class Paddle
{
private:
SDL_Rect Paddle_Bound;
int yVel;
SDL_Rect *clip;
int player_key;
public:
void events();
void logic();
void render();
Paddle(int player);
~Paddle();
};
class Game : public GameState
{
private:
int server;
TTF_Font *score = NULL;
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball Ball(2);
public:
void events();
void logic();
void render();
Game();
~Game();
};
写的行
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball Ball(2);
是那些引发错误的人,上面写着"需要一个类型说明符"。现在我知道这意味着它想要类似int player
的东西,但我需要传递一个数字,这样构造函数就可以决定它将是哪个播放器,最终决定用什么键在屏幕上上上下移动类。因此,为了非常清楚,我希望能够将一个参数传递给类实例化,这样类构造函数就可以将类变成左或右桨。那么,我该如何在另一个类中使用参数实例化一个类呢?
谢谢!
EDIT:对于构造函数的问题,我有另一个例子,它是完全编码的,我不能把类初始值设定项放在构造函数中。因此,基本上在标题屏幕中有一个按钮,它的属性存储在Button
类中,我创建该类来管理按钮。
class GameState
{
public:
virtual void events() = 0;
virtual void logic() = 0;
virtual void render() = 0;
virtual ~GameState(){};
};
class Button
{
public:
SDL_Rect button_clip[2];
SDL_Rect button;
SDL_Surface *button_sprite = NULL;
Button();
};
class Title : public GameState
{
private:
SDL_Surface *Title_Message = NULL;
SDL_Rect *clip;
// THIS IS WHERE I HAVE TO PLACE THIS LINE
//Button Title_Button;
public:
void events();
void logic();
void render();
Title();
~Title();
};
Title::Title()
{
//THIS IS WHERE IT SHOULD BE ACCORDING TO WHAT YOU'RE TELLING ME
Button Title_Message;
//text/sprites
Title_Message = TTF_RenderText_Solid(font, "PONG", color);
Title_Button.button_sprite = load_image("Start.png");
//region for button
Title_Button.button.x = 200;
Title_Button.button.y = 350;
Title_Button.button.w = 100;
Title_Button.button.h = 50;
//clips not hover
Title_Button.button_clip[0].x = 0;
Title_Button.button_clip[0].y = 0;
Title_Button.button_clip[0].w = 100;
Title_Button.button_clip[0].h = 50;
//clips hover
Title_Button.button_clip[1].x = 0;
Title_Button.button_clip[1].y = 50;
Title_Button.button_clip[1].w = 100;
Title_Button.button_clip[1].h = 50;
}
Title::~Title()
{
SDL_FreeSurface(Title_Message);
SDL_FreeSurface(Title_Button.button_sprite);
}
void Title::events()
{
int x = 0;
int y = 0;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEMOTION)
{
x = event.motion.x;
y = event.motion.y;
if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
{
clip = &Title_Button.button_clip[1];
}
else
{
clip = &Title_Button.button_clip[0];
}
}
if (event.type == SDL_QUIT)
{
quit = true;
}
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
{
set_next_state(GAME);
}
}
}
}
}
void Title::logic()
{
}
void Title::render()
{
apply_surface(Title_Button.button.x, Title_Button.button.y, Title_Button.button_sprite, screen, clip);
apply_surface((SCREEN_WIDTH - Title_Message->w) / 2, 100, Title_Message, screen);
}
这里的问题是,当我把Title_Message
放在void Title::Title()
构造函数中时,事件/逻辑/渲染函数根本不知道它存在。但是当我把它放在类定义中时,我不能通过它传递参数
值应该在构造函数定义中初始化:而不是在类定义中初始化。
class Game : public GameState
{
private:
int server;
TTF_Font *score;
Paddle PlayerOne;
Paddle Playertwo;
Ball ball; // same class name and variable name won't work
public:
void events();
void logic();
void render();
Game();
~Game();
};
...
Game::Game()
: score(NULL)
, PlayerOne(1)
, PlayerTwo(2)
, ball(2)
{
...
}
编辑:对于标题消息内容
class Title : public GameState
{
private:
SDL_Surface *Title_Message = NULL;
SDL_Rect *clip;
// THIS IS WHERE I HAVE TO PLACE THIS LINE
Button Title_Button;
Button Title_Message;
public:
void events();
void logic();
void render();
Title();
~Title();
};
Title::Title()
{
//text/sprites
Title_Message = TTF_RenderText_Solid(font, "PONG", color);
Title_Button.button_sprite = load_image("Start.png");
您不能在方法之外执行此操作。在构造函数中执行此操作
class Game : public GameState
{
private:
int server;
TTF_Font *score;
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball ballObject(2);
public:
void events();
void logic();
void render();
Game(): PlayerOne(1), Playertwo(2), ballObject(2){};
~Game();
};
编辑:已经发布了一个答案,很抱歉发布了同样的答案。我不太会使用这个界面。因此,计算代码块的生成需要时间。
编辑2:重复您的编辑。您必须在类本身中声明对象。只是你不能在声明中实例化它。你需要学习c++类和普通类是如何工作的。
class class1
{
private:
class class2Obj(args);
public:
class1();
~class1();
}
上面的例子是错误的。要做到这一点,你应该像这样做
class class1
{
private:
class2 class2Obj;
public:
class1();
~class1();
}
class1::class1()
{
class2Obj(args);
}
- C++的解析器在可以区分比较和模板实例化之前会做什么?
- 无法使用迭代器标记调度实例化模板
- 当我们实例化Caffe2预测器时,应用程序被卡住了
- 如何避免为模板化迭代器的每个可能的实例化专门化iterator_traits?
- 链接器如何处理跨翻译单元的相同模板实例化
- std::在类包装器实例化时设置的数组大小
- BOOST.SPIRIT.QI:定义太多解析器的组合达到模板实例化限制
- 模板链接器错误的显式实例化
- 克里昂未初始化记录类型:播放器
- 模板标准::映射::迭代器实例化
- 枚举器而不是用于类实例化的构造函数
- 由参数C++SDL实例化的播放器类
- C++ 无法实例化编解码器以在 txt 文件中写入 unicode
- 调用实例化函数时发生链接器错误
- 为什么没有询问priority_queue实例化中比较器的模板参数
- 实例化模板类时出现链接器错误
- 如何使用实例化渲染将每个实例的数据(如定位)传递给 OpenGL 3.2 中的着色器
- g++模板实例化器有多聪明(避免模板膨胀)
- 为什么为模板实例化声明运行时多态性会导致链接器错误
- c++ dynamic_cast对装饰器实例化失败