【发布时间】:2017-02-07 04:36:53
【问题描述】:
我有以下课程:
//----------------------------------------------------------------------
// CHARACTER CLASS
//----------------------------------------------------------------------
class Character
{
private:
POINT2f char_position;
public:
VECTOR2f char_velocity;
Character();
~Character();
// Mutators
void SetChar_Position(POINT2f p);
void SetChar_Velocity(VECTOR2f v);
// Accessors
POINT2f GetChar_Position();
VECTOR2f GetChar_Velocity();
};
//----------------------------------------------------------------------
// PLAYER CLASS - INHERITED FROM "CHARACTER" CLASS
//----------------------------------------------------------------------
class Player : public Character
{
private:
public:
int health;
Player();
~Player();
void SetPlayer_Health(int h);
int GetPlayer_Health();
};
所以基本上玩家类继承自角色类。
Character 类的成员函数可以让你设置角色的位置和速度,它接受 POINT2f 和 VECTOR2f。
在我的主要代码中,我创建了一个玩家角色Player Player1,然后在按下按钮时设置球的速度和位置:
if (ui.isActive('A')) // Check to see if "A" key is pressed
{
// Player Ball
Player1.SetChar_Velocity.x = -12.0;
Player1.SetChar_Position.x += (Player1.GetChar_Velocity.x*dt);
Player1.SetChar_Velocity.x += (acceleration*dt);
}
我目前遇到的错误是 left of .x must have class/struct/union,所以我将 Player1.SetChar_Velocity.x = -12.0; 更改为 Player1.SetChar_Velocity().x = -12.0;,这会导致错误 expression must have class type 和 to few arguments in function call。
有谁知道一种方法可以让我只操纵char_velocity 的x 号。我也知道我的代码 Set .x 速度不正确,因为我没有传入 VECTOR2f 值。
【问题讨论】:
-
Player1.char_velocity.x += ...?顺便说一句,只要char_velocity是公开的,您的SetChar_Velocity和GetChar_Velocity函数就是多余的。 -
这很好,我只是喜欢有功能,因为在我看来它更简洁一些。那么如果我想更改位于私有的
char_position的x 值呢? -
那你必须做
POINT2f pos = Player1.GetChar_Position(); pos.x += ...; Player1.SetChar_Position(pos);。不太方便! -
公平点,非常感谢您提供的所有信息。
标签: c++ vector member mutators