【问题标题】:SFML sprite movementSFML 精灵移动
【发布时间】:2017-05-19 16:39:34
【问题描述】:

我创建了一个用于精灵移动的类以及它的动画,非常基本的东西。

在最终消除所有错误和所有之后,我检查了底层逻辑和所有内容,发现运动矢量几乎是我想要的,一切似乎都很好。问题是一旦我按下运动按钮,精灵就会移动喜欢一秒钟,然后回到原来的位置。

我的移动类(负责精灵的移动和动画)返回一个精灵,它在主源代码中根据移动中设置的变量移动。

如果我将变量设置为足够高的值,那么我可以注意到精灵在其原始位置附近移动了一会儿,然后返回,然后再次移动并返回。我检查了代码。我没有重置精灵位置或类似的东西。 这是代码:-

源代码:-

#include"SFML\Graphics.hpp"
#include"check.h"
#include"display.h"
int main()
{
sf::Sprite plop;
sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default);
sf::Texture texture;
texture.loadFromFile("bahamut.png");
texture.setRepeated(false);
float fraps = 0.0;
check playa;
sf::Clock fps;
while (window.isOpen())
{
    fraps = fps.restart().asSeconds();
    plop = playa.movereturn(100000.,fraps,&texture);  
    window.clear();
    window.draw(plop);
    display dis(window);
}

return 0;

}

这是检查的标题:-

    #pragma once
#include"SFML\Graphics.hpp"
class check
{
public:
    check();
    sf::Sprite movereturn(float speed,float fps,sf::Texture* texture);
    ~check();
};

这里是检查的定义:-

#include "check.h"



check::check()
{
}

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture)
{
    sf::Sprite playas;
    playas.setTexture(*texture);
    sf::Vector2f movements = { 0.,0. };
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    {
        movements = { -speed*fps,0. };
        playas.move(movements);
    }
    else
    {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
        {
            movements = { speed*fps, 0. };
            playas.move(movements);
        }
        else
        {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
            {
                movements = { 0.,speed*fps };
                playas.move(movements);
            }
            else
            {

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
                {
                    movements = { 0.,-speed*fps };
                    playas.move(movements);
                }
                else
                    movements = { 0., 0. };

            }
        }
    }
    return playas;

}


check::~check()
{
}

Display 只接收窗口并在其中具有 window.display() 函数。没有这个类有一个处理程序异常,所以我不得不使用它。

【问题讨论】:

  • 你应该可以只调用 window.display() 而不需要你的显示类,你还应该用你遇到的问题更新你的其他问题,因为这基本上是相同的代码。跨度>

标签: visual-studio visual-c++ sfml


【解决方案1】:

好的,你的代码有一些问题。

  1. 您应该使用正确的浮点语法。 0.0f 是浮点数,0.0 是双精度数。令您惊讶的是,您的编译器并未就此发出警告。
  2. 在 movereturn 函数中的每一帧都会重新制作您的 Sprite,所以是的,您的 Sprite 只会向您告诉他的任何方向移动一点点。下一帧将使用其默认值(位置为 0)重新制作。如果您使用内置的 Visual Studio 调试器,您会看到您的精灵 playas 位置的每一帧始终重置为其默认值,这应该是对您的问题的巨大提示。
  3. 由于您将其限制为玩家只能移动 1 个方向,因此他们不能沿对角线移动(左上),您不需要所有 else 语句。
  4. 始终使用括号,无论它是否为 1 行 if 语句始终使用括号。您可以不使用它,但使用括号会使调试变得更加简单。

这应该可以解决您的问题。

Check.h

#pragma once
#include"SFML\Graphics.hpp"
class check
{
    sf::sprite playas;
public:
    check();
    sf::Sprite movereturn(float speed,float fps,sf::Texture* texture);
    ~check();
};

Check.cpp

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture)
{
    playas.setTexture(*texture);
    sf::Vector2f movements = { 0.0f,0.0f };
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    {
        movements = { -speed*fps,0.0f };
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
    {
        movements = { speed*fps, 0.0f };        
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
    {
        movements = { 0.0f,speed*fps };
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
    {
        movements = { 0.0f,-speed*fps };
    }
    playas.move(movements);
    return playas;
}

如果您想要对角线移动,您可以在 movereturn 函数中将代码更改为:

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture)
{
    playas.setTexture(*texture);
    sf::Vector2f movements = { 0.0f,0.0f };
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    {
        movements.x = -speed*fps;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
    {
        movements.x = speed*fps;        
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
    {
        movements.y = speed*fps;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
    {
        movements.y = -speed*fps;
    }
    playas.move(movements);
    return playas;

}

【讨论】: