我想用C++編寫一個小遊戲。訪問兩個實例
我想寫一個方法「攻擊」,設置攻擊後數字的狀態。 我有兩個實例的玩家和敵人。該類看起來像:
class figure {
private:
string name;
int hp;
int strength;
int defense;
public:
void attack(int xstrength) {
// This method gets the Input values of the player and should calculate
// and set the new hp stats of the enemy after an attack, sort of
hp = hp - (xstrength - defense);
}
};
但是我怎樣才能調用這個方法?我是否需要將程序單獨梅索德只得到 實例的srength價值?因爲我不能調用情況是這樣的:
enemy.attack();
,因爲我需要輸入實例球員的實力。 或者我要訪問實例這樣的一個值樣
enemy.attack(player->get_xstrength)
與方法:
void get_strength() {
return stength
};
如果我擴展類數字與更像值,電阻,電平,狀態等我必須 程序很多得到和設置methodes。
是的,你可以做到這一點。使用'get_xstrength'會將玩家的力量返回到敵人,你可以攻擊他。但是你也需要使用'set_xstrength()'設置敵人攻擊後的玩家強度。你需要傳遞玩家對象。 – ha9u63ar
我不明白這個問題。 –