Visual studio 精灵运动

Visual studio 精灵运动,visual-studio,visual-c++,sfml,Visual Studio,Visual C++,Sfml,我为一个精灵的运动和它的动画创建了一个类,非常基本的东西 在最终消除所有错误和所有内容后,我检查了底层逻辑和所有内容,发现运动矢量与我想要的几乎一样,一切似乎都正常。问题是,一旦我按下运动按钮,精灵会移动一秒钟,然后返回到其原始位置 我的类move(负责精灵的移动和动画)返回一个精灵,该精灵在主源代码中获取,根据move中设置的变量移动 如果我将变量设置为足够高的值,那么我可以注意到精灵在其原始位置移动一瞬间,然后返回,然后再次移动,然后返回。我已经检查了代码。我没有重置精灵位置或类似的东西。

我为一个精灵的运动和它的动画创建了一个类,非常基本的东西

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

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

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

源代码:-

#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()函数。如果没有这个类,则会出现处理程序异常,因此我不得不使用它。

好的,代码中有一些问题

  • 您应该为浮点使用正确的语法。0.0f表示浮点数,0.0表示双精度。令人惊讶的是,您的编译器没有就此向您发出警告
  • 你的精灵在你的movereturn函数中的每一帧都被重新制作,所以你的精灵只是朝着你告诉他的方向移动了一点点。下一帧将使用默认值(位置为0)重新生成。如果您使用内置的visual studio调试器,您将看到您的sprite playas位置的每一帧都会重置为默认值,这将是一个巨大的问题提示
  • 因为你限制了它,所以玩家只能移动一个方向,他们不能沿对角线(左上)移动,你不需要所有的else语句
  • 始终使用方括号,无论其是否为1行if语句始终使用方括号。您可以侥幸逃脱,但当您使用括号时,调试会简单得多

  • 这将解决您的问题

    检查.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();
    };
    

    检查.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;
    
    }
    

    您应该能够只调用window.display(),而不需要您的display类,还应该用您遇到的问题更新您的其他问题,因为这基本上是相同的代码。