让我的敌人转向我的玩家C ++

问题描述 投票:0回答:2

我正在尝试让我的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

我在做什么错?

c++ sfml game-ai
2个回答
6
投票

您对normalize的定义只有在使用后才会出现。可以将定义移到Enemy::Move之前,也可以将函数声明放在包含文件之后的文件顶部:

Vector2f normalize(const Vector2f& source);

这是相同行为的small example


0
投票

为您的函数创建原型,它将摆脱“未知函数”。

© www.soinside.com 2019 - 2024. All rights reserved.