Visual studio 精灵运动
我为一个精灵的运动和它的动画创建了一个类,非常基本的东西 在最终消除所有错误和所有内容后,我检查了底层逻辑和所有内容,发现运动矢量与我想要的几乎一样,一切似乎都正常。问题是,一旦我按下运动按钮,精灵会移动一秒钟,然后返回到其原始位置 我的类move(负责精灵的移动和动画)返回一个精灵,该精灵在主源代码中获取,根据move中设置的变量移动 如果我将变量设置为足够高的值,那么我可以注意到精灵在其原始位置移动一瞬间,然后返回,然后再次移动,然后返回。我已经检查了代码。我没有重置精灵位置或类似的东西。 代码如下:- 源代码:-Visual studio 精灵运动,visual-studio,visual-c++,sfml,Visual Studio,Visual C++,Sfml,我为一个精灵的运动和它的动画创建了一个类,非常基本的东西 在最终消除所有错误和所有内容后,我检查了底层逻辑和所有内容,发现运动矢量与我想要的几乎一样,一切似乎都正常。问题是,一旦我按下运动按钮,精灵会移动一秒钟,然后返回到其原始位置 我的类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()函数。如果没有这个类,则会出现处理程序异常,因此我不得不使用它。好的,代码中有一些问题
这将解决您的问题 检查.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类,还应该用您遇到的问题更新您的其他问题,因为这基本上是相同的代码。