Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何平稳地移动雪碧? 我用C++和SFML编写了一个简单的程序。我的问题是,当我移动我的雪碧,它是非常颠簸。我的精灵将移动一点,然后加速到平滑移动,直到我按下另一个键,它将再次停止,然后继续。我希望这是有意义的,但简而言之,我只希望我的精灵运动更流畅。我的代码: #include <SFML/Graphics.hpp> #include <math.h> #include <iostream> int main() { float x = 0; float y = 0; sf::Vector2f position; sf::Vector2f velocity; float maxspeed = 3.0f; float accel = 1.0f; float decel = 0.02f; sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!"); sf::Texture tplayer; if (!tplayer.loadFromFile("character.png")) { // error... } sf::Sprite splayer; splayer.setTexture(tplayer); splayer.setOrigin(sf::Vector2f(0, 0)); splayer.setTextureRect(sf::IntRect(0, 0, 16, 38)); sf::Vector2f pos = splayer.getPosition(); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); { if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) velocity.x -= accel; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) velocity.x += accel; else velocity.x *= decel; if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) velocity.y -= accel; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) velocity.y += accel; else velocity.y *= decel; if (velocity.x < -maxspeed) velocity.x = -maxspeed; if (velocity.x > maxspeed) velocity.x = maxspeed; if (velocity.y < -maxspeed) velocity.y = -maxspeed; if (velocity.y > maxspeed) velocity.y = maxspeed; position += velocity; splayer.setPosition(position); } } window.clear(); window.draw(splayer); window.display(); window.setFramerateLimit(60); } return 0; } #包括 #包括 #包括 int main() { 浮动x=0; 浮动y=0; sf::矢量2f位置; 矢量2f速度; 浮动最大速度=3.0f; 浮动加速度=1.0f; 浮动减速=0.02f; sf::RenderWindow窗口(sf::VideoMode(400400),“SFML工作!”; sf::纹理层; 如果(!tplayer.loadFromFile(“character.png”)) { //错误。。。 } sf::精灵splayer; splayer.settxture(tplayer); setOrigin(sf::Vector2f(0,0)); setTextureRect(sf::IntRect(0,0,16,38)); sf::Vector2f pos=splayer.getPosition(); while(window.isOpen()) { sf::事件; while(window.pollEvent(事件)) { 如果(event.type==sf::event::Closed) window.close(); { 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) 速度x-=加速度; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::D))则为else 速度x+=加速度; 其他的 速度x*=减速; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) 速度y-=加速度; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) 速度y+=加速度; 其他的 速度y*=减速; 如果(velocity.xmaxspeed)velocity.x=maxspeed; 如果(velocity.ymaxspeed)velocity.y=maxspeed; 位置+=速度; splayer.setPosition(位置); } } window.clear(); 窗口绘制(splayer); window.display(); 设置帧率限制(60); } 返回0; }_C++_Animation_Sprite_Codeblocks_Sfml - Fatal编程技术网

如何平稳地移动雪碧? 我用C++和SFML编写了一个简单的程序。我的问题是,当我移动我的雪碧,它是非常颠簸。我的精灵将移动一点,然后加速到平滑移动,直到我按下另一个键,它将再次停止,然后继续。我希望这是有意义的,但简而言之,我只希望我的精灵运动更流畅。我的代码: #include <SFML/Graphics.hpp> #include <math.h> #include <iostream> int main() { float x = 0; float y = 0; sf::Vector2f position; sf::Vector2f velocity; float maxspeed = 3.0f; float accel = 1.0f; float decel = 0.02f; sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!"); sf::Texture tplayer; if (!tplayer.loadFromFile("character.png")) { // error... } sf::Sprite splayer; splayer.setTexture(tplayer); splayer.setOrigin(sf::Vector2f(0, 0)); splayer.setTextureRect(sf::IntRect(0, 0, 16, 38)); sf::Vector2f pos = splayer.getPosition(); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); { if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) velocity.x -= accel; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) velocity.x += accel; else velocity.x *= decel; if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) velocity.y -= accel; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) velocity.y += accel; else velocity.y *= decel; if (velocity.x < -maxspeed) velocity.x = -maxspeed; if (velocity.x > maxspeed) velocity.x = maxspeed; if (velocity.y < -maxspeed) velocity.y = -maxspeed; if (velocity.y > maxspeed) velocity.y = maxspeed; position += velocity; splayer.setPosition(position); } } window.clear(); window.draw(splayer); window.display(); window.setFramerateLimit(60); } return 0; } #包括 #包括 #包括 int main() { 浮动x=0; 浮动y=0; sf::矢量2f位置; 矢量2f速度; 浮动最大速度=3.0f; 浮动加速度=1.0f; 浮动减速=0.02f; sf::RenderWindow窗口(sf::VideoMode(400400),“SFML工作!”; sf::纹理层; 如果(!tplayer.loadFromFile(“character.png”)) { //错误。。。 } sf::精灵splayer; splayer.settxture(tplayer); setOrigin(sf::Vector2f(0,0)); setTextureRect(sf::IntRect(0,0,16,38)); sf::Vector2f pos=splayer.getPosition(); while(window.isOpen()) { sf::事件; while(window.pollEvent(事件)) { 如果(event.type==sf::event::Closed) window.close(); { 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) 速度x-=加速度; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::D))则为else 速度x+=加速度; 其他的 速度x*=减速; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) 速度y-=加速度; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) 速度y+=加速度; 其他的 速度y*=减速; 如果(velocity.xmaxspeed)velocity.x=maxspeed; 如果(velocity.ymaxspeed)velocity.y=maxspeed; 位置+=速度; splayer.setPosition(位置); } } window.clear(); 窗口绘制(splayer); window.display(); 设置帧率限制(60); } 返回0; }

如何平稳地移动雪碧? 我用C++和SFML编写了一个简单的程序。我的问题是,当我移动我的雪碧,它是非常颠簸。我的精灵将移动一点,然后加速到平滑移动,直到我按下另一个键,它将再次停止,然后继续。我希望这是有意义的,但简而言之,我只希望我的精灵运动更流畅。我的代码: #include <SFML/Graphics.hpp> #include <math.h> #include <iostream> int main() { float x = 0; float y = 0; sf::Vector2f position; sf::Vector2f velocity; float maxspeed = 3.0f; float accel = 1.0f; float decel = 0.02f; sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!"); sf::Texture tplayer; if (!tplayer.loadFromFile("character.png")) { // error... } sf::Sprite splayer; splayer.setTexture(tplayer); splayer.setOrigin(sf::Vector2f(0, 0)); splayer.setTextureRect(sf::IntRect(0, 0, 16, 38)); sf::Vector2f pos = splayer.getPosition(); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); { if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) velocity.x -= accel; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) velocity.x += accel; else velocity.x *= decel; if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) velocity.y -= accel; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) velocity.y += accel; else velocity.y *= decel; if (velocity.x < -maxspeed) velocity.x = -maxspeed; if (velocity.x > maxspeed) velocity.x = maxspeed; if (velocity.y < -maxspeed) velocity.y = -maxspeed; if (velocity.y > maxspeed) velocity.y = maxspeed; position += velocity; splayer.setPosition(position); } } window.clear(); window.draw(splayer); window.display(); window.setFramerateLimit(60); } return 0; } #包括 #包括 #包括 int main() { 浮动x=0; 浮动y=0; sf::矢量2f位置; 矢量2f速度; 浮动最大速度=3.0f; 浮动加速度=1.0f; 浮动减速=0.02f; sf::RenderWindow窗口(sf::VideoMode(400400),“SFML工作!”; sf::纹理层; 如果(!tplayer.loadFromFile(“character.png”)) { //错误。。。 } sf::精灵splayer; splayer.settxture(tplayer); setOrigin(sf::Vector2f(0,0)); setTextureRect(sf::IntRect(0,0,16,38)); sf::Vector2f pos=splayer.getPosition(); while(window.isOpen()) { sf::事件; while(window.pollEvent(事件)) { 如果(event.type==sf::event::Closed) window.close(); { 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) 速度x-=加速度; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::D))则为else 速度x+=加速度; 其他的 速度x*=减速; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) 速度y-=加速度; 如果(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) 速度y+=加速度; 其他的 速度y*=减速; 如果(velocity.xmaxspeed)velocity.x=maxspeed; 如果(velocity.ymaxspeed)velocity.y=maxspeed; 位置+=速度; splayer.setPosition(位置); } } window.clear(); 窗口绘制(splayer); window.display(); 设置帧率限制(60); } 返回0; },c++,animation,sprite,codeblocks,sfml,C++,Animation,Sprite,Codeblocks,Sfml,不要在轮询事件循环中使用sf::Keyboard::isKeyPressed-方法!此方法提供了一种在程序中的任何位置请求密钥状态的方法。如果之前检查当前轮询事件是否与您的想法一致,则仅在轮询事件循环中写入代码。您可以这样做if(event.type==sf::event::Closed),正如您已经发现的那样。 因此,只需将所有代码(窗口关闭事件除外)移出轮询事件循环。 我感觉您不理解事件循环的确切功能,因此我将向您解释: 前一帧中出现的每个事件(例如鼠标事件、按键、窗口大小调整等)都将排队,

不要在轮询事件循环中使用
sf::Keyboard::isKeyPressed
-方法!此方法提供了一种在程序中的任何位置请求密钥状态的方法。如果之前检查当前轮询事件是否与您的想法一致,则仅在轮询事件循环中写入代码。您可以这样做
if(event.type==sf::event::Closed)
,正如您已经发现的那样。 因此,只需将所有代码(窗口关闭事件除外)移出轮询事件循环。 我感觉您不理解事件循环的确切功能,因此我将向您解释: 前一帧中出现的每个事件(例如鼠标事件、按键、窗口大小调整等)都将排队,轮询事件循环将遍历每个排队事件。例如,首先是鼠标移动事件,然后是按键事件。您只想在一个特定事件发生后执行一次操作,因此您必须使用if块询问当前事件是否与搜索的事件匹配。而且他们只做一次

顺便说一句:你可以调用
window.setFramerateLimit(60),这样您就不会浪费资源来设置每个帧的限制。

Google


非常感谢。让我的精灵运动参加投票活动是一个很大的错误。非常感谢你听到这个消息。
//Before main loopp
sf::Clock clock;

// Start main loop

         //Somewhere in the main loop
         sf::Time deltaTime = clock.restart().asSeconds();
         velocity.x *= deltaTime.asSeconds();
         velocity.y *= deltaTime.asSeconds();
         //move is just a setPosition but it adds given argument to actual position so it's basically splayer.setPosition(sf:Vector2f(position.x + velocity.x,position.y + velocity.y));
         splayer.move(velocity);