我有点儿干了。我正在构建一个游戏,由于一些奇怪的原因,我无法调用从一个类到另一个类的变量。它只是在这个特定的代码部分中,它不起作用,所以任何帮助都是值得赞赏的。提前致谢:
这是代码:
class HeadsUpDisplay
{
private:
int frame;
public:
SDL_Rect HealthBar[20];
SDL_Rect offset;
void init();
void render();
}HUD;
void HeadsUpDisplay::init(){
offset.x = SCREENW*.9;
offset.y = SCREENH/10;
HealthBar[0].x = 0;
HealthBar[0].y = 0;
HealthBar[0].w = 204;
HealthBar[0].h = 14;
HealthBar[1].x = 204*1;
HealthBar[1].y = 0;
HealthBar[1].w = 204;
HealthBar[1].h = 14;
HealthBar[2].x = 204*2;
HealthBar[2].y = 0;
HealthBar[2].w = 204;
HealthBar[2].h = 14;
HealthBar[3].x = 204*3;
HealthBar[3].y = 0;
HealthBar[3].w = 204;
HealthBar[3].h = 14;
HealthBar[4].x = 204*4;
HealthBar[4].y = 0;
HealthBar[4].w = 204;
HealthBar[4].h = 14;
HealthBar[5].x = 204*5;
HealthBar[5].y = 0;
HealthBar[5].w = 204;
HealthBar[5].h = 14;
HealthBar[6].x = 204*6;
HealthBar[6].y = 0;
HealthBar[6].w = 204;
HealthBar[6].h = 14;
HealthBar[7].x = 204*7;
HealthBar[7].y = 0;
HealthBar[7].w = 204;
HealthBar[7].h = 14;
HealthBar[8].x = 204*8;
HealthBar[8].y = 0;
HealthBar[8].w = 204;
HealthBar[8].h = 14;
HealthBar[9].x = 204*9;
HealthBar[9].y = 0;
HealthBar[9].w = 204;
HealthBar[9].h = 14;
HealthBar[10].x = 204*10;
HealthBar[10].y = 0;
HealthBar[10].w = 204;
HealthBar[10].h = 14;
HealthBar[11].x = 204*11;
HealthBar[11].y = 0;
HealthBar[11].w = 204;
HealthBar[11].h = 14;
HealthBar[12].x = 204*12;
HealthBar[12].y = 0;
HealthBar[12].w = 204;
HealthBar[12].h = 14;
HealthBar[13].x = 204*13;
HealthBar[13].y = 0;
HealthBar[13].w = 204;
HealthBar[13].h = 14;
HealthBar[14].x = 204*14;
HealthBar[14].y = 0;
HealthBar[14].w = 204;
HealthBar[14].h = 14;
HealthBar[15].x = 204*15;
HealthBar[15].y = 0;
HealthBar[15].w = 204;
HealthBar[15].h = 14;
HealthBar[16].x = 204*16;
HealthBar[16].y = 0;
HealthBar[16].w = 204;
HealthBar[16].h = 14;
HealthBar[17].x = 204*17;
HealthBar[17].y = 0;
HealthBar[17].w = 204;
HealthBar[17].h = 14;
HealthBar[18].x = 204*18;
HealthBar[18].y = 0;
HealthBar[18].w = 204;
HealthBar[18].h = 14;
HealthBar[19].x = 204*19;
HealthBar[19].y = 0;
HealthBar[19].w = 204;
HealthBar[19].h = 14;
}
void HeadsUpDisplay::render(){
if(character.characterhealth >95 && character.characterhealth <= 100)
{
frame = 19;
}
if(character.characterhealth >90 && character.characterhealth <=95)
{
frame = 18;
}
if(character.characterhealth >85 && character.characterhealth <= 90)
{
frame = 17;
}
if(character.characterhealth >80 && character.characterhealth <= 85)
{
frame = 16;
}
if(character.characterhealth >75 && character.characterhealth <= 80)
{
frame = 15;
}
if(character.characterhealth >70 && character.characterhealth <= 75)
{
frame = 14;
}
if(character.characterhealth >65 && character.characterhealth <= 70)
{
frame = 13;
}
if(character.characterhealth >60 && character.characterhealth <= 65)
{
frame = 12;
}
if(character.characterhealth >55 && character.characterhealth <= 60)
{
frame = 11;
}
if(character.characterhealth >50 && character.characterhealth <= 55)
{
frame = 10;
}
if(character.characterhealth >45 && character.characterhealth <= 50)
{
frame = 9;
}
if(character.characterhealth >40 && character.characterhealth <= 45)
{
frame = 8;
}
if(character.characterhealth >35 && character.characterhealth <= 40)
{
frame = 7;
}
if(character.characterhealth >30 && character.characterhealth <= 35)
{
frame = 6;
}
if(character.characterhealth >25 && character.characterhealth <= 30)
{
frame = 5;
}
if(character.characterhealth >20 && character.characterhealth <= 25)
{
frame = 4;
}
if(character.characterhealth >15 && character.characterhealth <= 20)
{
frame = 3;
}
if(character.characterhealth >10 && character.characterhealth <= 15)
{
frame = 2;
}
if(character.characterhealth >5 && character.characterhealth <= 10)
{
frame = 1;
}
if(character.characterhealth >0 && character.characterhealth <= 5)
{
frame = 0;
}
SDL_BlitSurface(health, &HealthBar[frame], screen, &offset);
}
所以问题是,在每个if语句中,我得到错误“erro:'character'未在此范围内声明”;我也尝试了这个类的原型,但没有解决问题。我感谢任何帮助,甚至是负面的反馈,至少它是什么。
答案 0 :(得分:1)
你的班级没有“人物”字段。亲眼看看:
class HeadsUpDisplay
{
private:
int frame;
public:
SDL_Rect HealthBar[20];
SDL_Rect offset;
void init();
void render();
};
您有frame
,HealthBar
和offset
。然而,在您试用的每种方法中,您都尝试访问“角色”。
字符是来自另一个类的对象。
C ++类不知道另一个对象存在。要访问另一个对象,您应该通过引用或指针将其传递给方法:
void HUD::render(Character &character){
if (character.whatever)
....
}
void HUD::render(Character *character){
if (character->whatever)
....
}
或者它应该被声明为尝试访问它的函数体之前的全局变量。
Character character;//or extern Character character;
void HUD::render(){
if (character.whatever)
....
}
如果在编译器遇到试图访问字符的函数体之前没有以任何形式提及“字符”,编译器将不知道“字符”是什么。
在可能的情况下避免使用全局变量通常是个好主意。