SFML精灵移动

时间:2017-01-05 07:59:32

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

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

最后删除所有错误后,我通过底层逻辑检查了所有内容并且看到了运动矢量几乎是我想要的东西,一切似乎都没问题。问题是,一旦我按下精灵移动的按钮喜欢一秒钟,然后回到原来的位置。

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

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

源代码: -

#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();
};

以下是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()
{
}

显示只接受窗口并且在其中有window.display()函数。没有这个类有一个处理程序异常所以我被迫使用它。

1 个答案:

答案 0 :(得分:0)

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

  1. 您应该为float使用正确的语法。 0.0f表示浮点数,0.0表示双精度数。惊喜你的编译器没有给你一个警告。
  2. 你的精灵正在重新移动你的移动返回功能的每一帧,所以你的精灵只会向你告诉他去的任何方向移动。在下一帧中,它使用默认值(位置为0)进行重新制作。如果您使用内置的visual studio调试器,您会看到您的精灵播放位置的每一帧始终重置为其默认值,应该是对你的问题提出了巨大的暗示。
  3. 由于你限制它,所以玩家只能移动1个方向,他们不能对角移动(向左移动)你不需要所有其他的陈述。
  4. 始终使用括号,无论其1行if语句是否始终使用括号。你被允许逃脱它但是当你使用括号时它会使调试变得更加简单。
  5. 这可以解决您的问题。

    <强> 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;
    }
    

    如果您想要对角移动,可以在移动返回功能中将代码更改为:

    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;
    
    }