1

我有点卡住了。我正在开发一款游戏,出于某种奇怪的原因,我无法将变量从一个类调用到另一个类。仅在此特定代码部分中它不起作用,因此不胜感激。提前致谢:

这是代码:

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 语句中,我收到错误“错误:'字符'未在此范围内声明”;我也尝试对这个类进行原型设计,但这并没有解决问题。我感谢任何帮助,甚至是负面反馈,至少它是一些东西。

4

1 回答 1

1

您的班级没有“字符”字段。你自己看:

class HeadsUpDisplay
{
    private:
       int frame;
    public:
       SDL_Rect HealthBar[20];
       SDL_Rect offset;
       void init();
       void render();
};

你有frame和。然而,在你贴花的每一种方法中,你都试图访问“角色”。HealthBaroffset

character 是另一个类的对象。

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)
        ....
}

如果在编译器遇到试图访问字符的函数体之前没有以任何形式提及“字符”,编译器将不知道“字符”是什么。

尽可能避免使用全局变量通常是个好主意。

于 2013-11-15T05:22:27.227 回答