我正在尝试让我的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;
}
错误是:
'normalize': identifier not found
我在做什么错?
您对normalize
的定义只有在使用后才会出现。可以将定义移到Enemy::Move
之前,也可以将函数声明放在包含文件之后的文件顶部:
Vector2f normalize(const Vector2f& source);
这是相同行为的small example。
为您的函数创建原型,它将摆脱“未知函数”。