【发布时间】: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