我创建了一个用于移动精灵的类以及它的动画,非常基本的东西。
最后删除所有错误后,我通过底层逻辑检查了所有内容并且看到了运动矢量几乎是我想要的东西,一切似乎都没问题。问题是,一旦我按下精灵移动的按钮喜欢一秒钟,然后回到原来的位置。
我的班级移动(负责精灵的移动和动画)返回一个精灵,该精灵在主要源代码中获取,该源代码根据移动中设置的变量移动。
如果我将变量设置为足够高的值,那么我可以注意到精灵在瞬间移动它的原始位置然后返回然后再次移动并返回。我检查了代码。我没有重置精灵位置或类似的东西。 这是代码: -
源代码: -
#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()函数。没有这个类有一个处理程序异常所以我被迫使用它。
答案 0 :(得分:0)
好吧,你的代码有些问题。
这可以解决您的问题。
<强> 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;
}