我正在制作一个游戏,其中我用于控制游戏对象的主要类是“inGame”。在“inGame”中还会有其他几个定制的类。
喜欢,
class mouse
{
int x, y;
bool alive;
float speed;
public:
mouse(int xx, int yy, float spd, bool alv)
{
x = xx; y = yy; speed = spd; alive = alv;
}
// member functions
};
class inGame
{
mouse m1;
dog d1; // single object which are easy to construct
public:
inGame() : d1(200, 400, 1.5, false), m1(40, 40, 0.5, false) //This works fine
{} //coordinates fixed and set by me
// member functions
};
但现在可以说我想要 3 只鼠标。所以我想到了 m1[3] 或者可能是一个向量。
class inGame
{
mouse m1[3]; // like i want 3 mouse(animals) in the game screen
dog d1; // single object which are easy to construct
public:
inGame() : d1(200, 400, 1.5, false) //Confusion here m1(40, 40, 0.5, false)
{}
// members
};
所以我尝试了以下方法:
inGame() : m1[0](1,2,3,true), m1[1](2,3,4,true), m1[2](3,4,5,true) { } //NO
inGame() : m1 { (1,2,3,true), (2,3,4,true), (3,4,5,true) } { } //NO
inGame() : m1 { (1,2,3,true), (2,3,4,true), (3,4,5,true); } { }
即使我使用 std::vector m1 那么如何通过 inGame 默认构造函数进行初始化?它会在构造函数中写入吗?
mouse temp(1,2,3,true);
m1.push_back(temp);
哪种方法更好?主要我会做:
inGame GAME; //Just to construct all animals without user requirement
谢谢。
更新:
没有运气
inGame() : m1 { mouse(1,2,3,true), mouse(2,3,4,true), mouse(3,4,5,true) } { }
inGame() : m1 { {1,2,3,true}, {2,3,4,true}, {3,4,5,true} } { }
在“m1 {”之后有一个错误,即“期望一个 )”。而 m1 { "}<--" 表示“期望一个 ;”