我遇到的问题是,当我更改枚举但它没有改变时 它看起来只是一个本地变化。
如果我这样做,下面的报价很好。
level = new level_1();
while(true)
{
cout << level->state << endl; // show '1' at first loop. Another loop show '2'
level->state = STATE_INITIALIZE;
}
但我必须在他的功能中改变状态
level = new level_1();
while(true)
{
cout << level->state << endl; // show '1' only
level.do();
}
level.do功能
void Level_1::do()
{
state = STATE_INITIALIZE; // If this work, it should be '2'
// this->state = STATE_INITIALIZE;
}
所以对我来说这是一个很大的问题,它不会改变!! T_T
这是一些代码(几乎是完整的代码)
enum LEVEL_STATE
{ STATE_LOAD = 1,
STATE_INITIALIZE = 2,
STATE_UPDATE = 3,
STATE_DRAW = 4,
STATE_FREE = 5,
STATE_UNLOAD = 6
};
class Level {
public:
LEVEL_STATE state = STATE_LOAD;
vector<Shader> shader;
vector<Model> model;
virtual void load()=0;
virtual void initialize()=0;
virtual void update()=0;
virtual void draw(Shader)=0;
virtual void free()=0;
virtual void unload()=0;
virtual Level* next()=0;
};
#include"Level.h"
class Level_1 : public Level {
public:
LEVEL_STATE state;
vector<Shader> shader;
vector<Model> model;
void load();
void initialize();
void update();
void draw(Shader);
void free();
void unload();
Level* next();
};
#pragma once
#include"Map_Level_0.h"
#include"Map_Level_1.h"
extern Camera camera;
extern float SCR_WIDTH;
extern float SCR_HEIGHT;
void Level_1::load()
{
shader.push_back(Shader("../Shader/alpha.vertex", "../Shader/alpha.fragment")); // this worked!
model.push_back(Model (FileSystem::getPath("res/arcade/arcade.obj"))); // this worked!
state = STATE_INITIALIZE; // this is not worked! the value doesn't change!
}
void Level_1::initialize()
{
shader[0].use();
state = STATE_UPDATE; // this is not worked! the value doesn't change!
}
void Level_1::update()
{
shader[0].use();
state = STATE_DRAW; // this is not worked! the value doesn't change!
}
void Level_1::draw(Shader shader)
{
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 10000.0f);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 model;
shader.setMat4("projection", projection);
shader.setMat4("view", view);
shader.setInt("mode", 0);
//model = scale(vec3(5.0f, 5.0f, 5.0f));
shader.setMat4("model", model);
shader.setInt("fragMode", 1);
shader.setVec3("fragColor", vec3(0.92f, 0.92f, 0.92f));
this->model[0].Draw(shader);
state = STATE_UPDATE; // this is not worked! the value doesn't change!
}
void Level_1::free()
{
}
void Level_1::unload()
{
}
Level* Level_1::next() {
return new Level_0();
}
level = new Level_1();
while (!glfwWindowShouldClose(window))
{
DEV_processInput(window);
cout << level->state;
switch (level->state)
{
case STATE_LOAD:
level->load();
case STATE_INITIALIZE:
level->initialize();
case STATE_UPDATE:
level->update();
case STATE_DRAW:
level->draw(shader_1);
case STATE_FREE:
level->free();
case STATE_UNLOAD:
level->unload();
}
glfwSwapBuffers(window);
glfwPollEvents();
}
如果我 1.)做错了很容易修复 2.)重复任何问题
我会道歉..
答案 0 :(得分:1)
您在基类中定义了一个state
成员,然后在子类中重新定义了相同的成员,这导致了这个问题。
如果你有一个Level * level
指针,无论它指向哪个子类实例,你总是会读到基类 state
成员,而你写的是他们方法中的子类成员(即do
)。
我建议仅在基类中定义state
(并使其成为protected
。)