我想让我Enemy搬到我家Player.
我知道的事情:
我需要做的事情:
所以我认为我需要做的是根据球员的位置"规范化"敌人的位置,这样我就知道要去哪里了,两者都有基于的位置Vector2f.
这就是我在敌人身上的代码:
void Enemy::Move()
{
//cout << "Move" << endl;
// Make movement
Vector2f playerPosition = EntityManager::Instance().player.GetPosition();
Vector2f thisPosition;
thisPosition.x = xPos;
thisPosition.y = yPos;
//Vector2f direction = normalize(playerPosition - thisPosition);
speed = 5;
//EntityManager::Instance().enemy.enemyVisual.move(speed * direction);
}
Vector2f normalize(const Vector2f& source)
{
float length = sqrt((source.x * source.x) + (source.y * source.y));
if (length != 0)
return Vector2f(source.x / length, source.y / length);
else
return source;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
'normalize': identifier not found
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
您的定义normalize直到您使用它之后才会出现.要么之前移动定义Enemy::Move,要么在包含之后将函数声明放在文件的顶部:
Vector2f normalize(const Vector2f& source);
Run Code Online (Sandbox Code Playgroud)
这是相同行为的一个小例子.