为什么精灵不动?

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

我正在使用 SFML 作为我的图形库,用 C++ 制作横向卷轴平台游戏。因此,它不是让玩家移动,而是让周围的世界移动。我为 Tile Map 创建了一个类,并为 Tiles 创建了一个类,使其位于一个向量中。问题在于,当玩家调用 Tile Map 的公共函数来移动所有图块时,一切似乎都很顺利,直到矢量中的实际图块移动时,它才没有移动。它只更改一次值,然后卡在同一位置。

玩家代码部分:

void Player::updatePhysics()
{
    if (this->physics.getElapsedTime().asSeconds() >= 0.01)
    {
        falling++;
        this->velocity.y += 0.30f;
        this->camera.y += 0.30f;
        this->tileMap.move(-this->camera.x, -this->camera.y); //The problem
        for (auto& i : this->entities)
        {
            i.entity.move(-this->camera.x, -this->camera.y);
        }
        this->ready = true;
        //this->player.move(this->velocity.x, this->velocity.y);

        this->velocity.x *= 0.94f;
        this->camera.x *= 0.94f;

        if (std::abs(this->camera.x) < 0.1)
            this->camera.x = 0.f;

        this->physics.restart();
    }
    if (camera.x == 0.1)
    {
        this->dir = NOTMOVING;
    }
    if (camera.y > 0)
    {
        this->dir = DOWN;
    }
}

TileMap.cpp 函数:

void TileMap::move(float x, float y)
{
    for (auto& i : this->tiles)
    {
        i.move(x, y);
    }
}

瓷砖移动功能:

void Tile::move(float x, float y)
{
    this->tile.move(x, y);
    if (this->type == 1)
    {
        this->hitbox.setPoint(0, sf::Vector2f(this->tile.getGlobalBounds().left, this->tile.getGlobalBounds().top));
        this->hitbox.setPoint(1, sf::Vector2f(this->tile.getGlobalBounds().left + this->tile.getGlobalBounds().width, this->tile.getGlobalBounds().top));
        this->hitbox.setPoint(2, sf::Vector2f(this->tile.getGlobalBounds().left + this->tile.getGlobalBounds().width, this->tile.getGlobalBounds().top + this->tile.getGlobalBounds().height));
        this->hitbox.setPoint(3, sf::Vector2f(this->tile.getGlobalBounds().left, this->tile.getGlobalBounds().top + this->tile.getGlobalBounds().height));
    }
    else if (this->type == 2)
    {
        this->hitbox.setPoint(0, sf::Vector2f(this->tile.getGlobalBounds().left + this->tile.getGlobalBounds().width, this->tile.getGlobalBounds().top));
        this->hitbox.setPoint(1, sf::Vector2f(this->tile.getGlobalBounds().left, this->tile.getGlobalBounds().top + this->tile.getGlobalBounds().height));
        this->hitbox.setPoint(2, sf::Vector2f(this->tile.getGlobalBounds().left + this->tile.getGlobalBounds().width, this->tile.getGlobalBounds().top + this->tile.getGlobalBounds().height));
    }
}

我尝试了 TileMap 类可以接收该函数的不同方式,因为当我在 TileMap 内部调用函数时它实际上移动了。

c++ sfml
© www.soinside.com 2019 - 2024. All rights reserved.