如何在精灵 sheet 的两个不同部分之间来回切换,同时用户仍然使用 SFML 按住键
How to switch back and forth between two different sections on a sprite sheet while user still holds a key using SFML
所以我在这里的唯一目标是左右移动精灵,当精灵移动时,我想每半秒在精灵的两个不同部分之间切换sheet。
我已经尝试了所有我能想到的方法,所以我让它继续工作,而没有只实现所需的功能。 (编辑:我已经更改它以尝试遵循另一个用户给出的建议)我在网上看过,但据我所知,我找不到任何东西回答我的问题。我确定我忽略了一些东西,而且它很简单,但这就是为什么我需要你的意见。
我要使用的精灵的第三部分 sheet 在 x = 800 处,面向左的动画在 y = 0 处在顶部,面向右的动画在 y = 600 处在底部
Here is the sprite sheet I am using
第一个是站着不动的,后面两排是我想切换的,而"walking"
无论如何,这是我的代码:
#include "pch.h"
#include <iostream>
#include "SFML/Graphics.hpp"
#include <random>
#include <unordered_map>
enum State
{
Walking,
Standing
};
enum Direction
{
Left,
Right
};
int main(int argc, char ** argv)
{
/* Gregory */
sf::Texture GregorySpriteSheet_T;
GregorySpriteSheet_T.loadFromFile("Images/GregorySpriteSheet.png");
sf::IntRect GregorySpriteRect(0, 600, 400, 600);
sf::Sprite Gregory(GregorySpriteSheet_T, GregorySpriteRect);
sf::RenderWindow renderWindow(sf::VideoMode(1600,800), "SFML 2 Demo");
sf::Event event;
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Clock deltaClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
State isWalking{ Standing };
sf::Clock walkClock;
Direction direction = Right;
/* RENDER WINDOW LOOP */
while (renderWindow.isOpen())
{
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
renderWindow.close();
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Walking;
direction = Right;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Standing;
}
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Walking;
direction = Left;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Standing;
}
}
}
if (isWalking == Walking)
{
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
if (GregorySpriteRect.left == 0)
GregorySpriteRect.left = 400;
if ((int(walkClock.getElapsedTime().asSeconds() / 1.5f) % 2) == 1)
{
if (GregorySpriteRect.left == 400)
GregorySpriteRect.left == 800;
if (GregorySpriteRect.left == 800)
GregorySpriteRect.left == 400;
walkClock.restart();
}
}
{
using kb = sf::Keyboard;
if (kb::isKeyPressed(kb::Right))
{
Gregory.move(400 * timePerFrame.asSeconds(), 0.0f);
direction = Right;
isWalking = Walking;
}
if (kb::isKeyPressed(kb::Left))
{
Gregory.move(-400 * timePerFrame.asSeconds(), 0.0f);
direction = Left;
isWalking = Walking;
}
if (kb::isKeyPressed(kb::Right) && kb::isKeyPressed(kb::Left))
{
isWalking = Standing;
}
}
if (isWalking == Standing)
{
GregorySpriteRect.left = 0;
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
}
}
Gregory.setTextureRect(GregorySpriteRect);
renderWindow.clear();
renderWindow.draw(Gregory);
renderWindow.display();
}
/* END RENDER WINDOW LOOP */
}
这是一个建议,但还有很多替代方案:
首先,稍微重写您的事件处理,这样您就可以区分第一次按下键和随后按下键的时间。一种简单的方法是使用 enum State { Standing, Walking }
并将其设置为密钥处理程序中的适当值(例如 "not walking and key is held? set state to walking")
然后,当玩家开始行走时,(重新)启动一个 "walk" 时钟。
最后,当你实际渲染你的播放器时,检查这个时钟并将自行走以来的过期时间除以你的行走周期(这决定了一个行走框架应该保持多长时间)。如果有偶数个这样的行走周期,则使用左侧行走精灵,否则使用右侧行走精灵。
当然,这种技术很容易推广到更大的步行框架集。
所以对于将来来到这里的任何人,我错过了一条重要的台词:
Gregory.setTextureRect(GregorySpriteRect);
在我的 "if" 时钟声明中 "if (isWalking == Walking)" 下,或者在它下面(就像我在这里所做的那样)。粘贴下面的完整代码。现在适用于两个方向。不确定这有多优化,但如果你是像我这样的业余爱好者,这应该有所帮助。
另请注意,我已经恢复了 Botje 的回答中建议的一些更改。尽管我必须感谢他在建议使用枚举进行行走和站立以及方向时为我提供了很多帮助。由于浮点变量的舍入误差,我还使用了“>”运算符而不是教授建议的“==”运算符。
干杯!
#include "pch.h"
#include <iostream>
#include "SFML/Graphics.hpp"
#include <random>
#include <unordered_map>
enum State
{
Walking,
Standing
};
enum Direction
{
Left,
Right
};
int main(int argc, char ** argv)
{
/* Gregory */
sf::Texture GregorySpriteSheet_T;
GregorySpriteSheet_T.loadFromFile("Images/GregorySpriteSheet.png");
sf::IntRect GregorySpriteRect(0, 600, 400, 600);
sf::Sprite Gregory(GregorySpriteSheet_T, GregorySpriteRect);
sf::RenderWindow renderWindow(sf::VideoMode(1600,800), "SFML 2 Demo");
sf::Event event;
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Clock deltaClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
State isWalking{ Standing };
sf::Clock walkClock;
Direction direction = Right;
/* RENDER WINDOW LOOP */
while (renderWindow.isOpen())
{
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
renderWindow.close();
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Walking;
direction = Right;
GregorySpriteRect.top = 600;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Standing;
}
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Walking;
direction = Left;
GregorySpriteRect.top = 0;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Standing;
}
}
}
if (isWalking == Walking)
{
if (direction == Right)
GregorySpriteRect.top = 600;
else if (direction == Left)
GregorySpriteRect.top = 0;
else
GregorySpriteRect.top = 600;
if (GregorySpriteRect.left == 0)
GregorySpriteRect.left = 400;
if (walkClock.getElapsedTime().asSeconds() > 0.5f)
{
if (GregorySpriteRect.left == 400)
GregorySpriteRect.left = 800;
else if (GregorySpriteRect.left == 800)
GregorySpriteRect.left = 400;
else
GregorySpriteRect.left += 0;
walkClock.restart();
}
Gregory.setTextureRect(GregorySpriteRect);
}
{
using kb = sf::Keyboard;
if (kb::isKeyPressed(kb::Right))
{
Gregory.move(400 * timePerFrame.asSeconds(), 0.0f);
direction = Right;
}
if (kb::isKeyPressed(kb::Left))
{
Gregory.move(-400 * timePerFrame.asSeconds(), 0.0f);
direction = Left;
}
if (kb::isKeyPressed(kb::Right) && kb::isKeyPressed(kb::Left))
{
isWalking = Standing;
direction = Right;
}
}
if (isWalking == Standing)
{
GregorySpriteRect.left = 0;
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
}
}
Gregory.setTextureRect(GregorySpriteRect);
renderWindow.clear();
renderWindow.draw(Gregory);
renderWindow.display();
}
/* END RENDER WINDOW LOOP */
}
所以我在这里的唯一目标是左右移动精灵,当精灵移动时,我想每半秒在精灵的两个不同部分之间切换sheet。
我已经尝试了所有我能想到的方法,所以我让它继续工作,而没有只实现所需的功能。 (编辑:我已经更改它以尝试遵循另一个用户给出的建议)我在网上看过,但据我所知,我找不到任何东西回答我的问题。我确定我忽略了一些东西,而且它很简单,但这就是为什么我需要你的意见。
我要使用的精灵的第三部分 sheet 在 x = 800 处,面向左的动画在 y = 0 处在顶部,面向右的动画在 y = 600 处在底部
Here is the sprite sheet I am using
第一个是站着不动的,后面两排是我想切换的,而"walking"
无论如何,这是我的代码:
#include "pch.h"
#include <iostream>
#include "SFML/Graphics.hpp"
#include <random>
#include <unordered_map>
enum State
{
Walking,
Standing
};
enum Direction
{
Left,
Right
};
int main(int argc, char ** argv)
{
/* Gregory */
sf::Texture GregorySpriteSheet_T;
GregorySpriteSheet_T.loadFromFile("Images/GregorySpriteSheet.png");
sf::IntRect GregorySpriteRect(0, 600, 400, 600);
sf::Sprite Gregory(GregorySpriteSheet_T, GregorySpriteRect);
sf::RenderWindow renderWindow(sf::VideoMode(1600,800), "SFML 2 Demo");
sf::Event event;
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Clock deltaClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
State isWalking{ Standing };
sf::Clock walkClock;
Direction direction = Right;
/* RENDER WINDOW LOOP */
while (renderWindow.isOpen())
{
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
renderWindow.close();
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Walking;
direction = Right;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Standing;
}
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Walking;
direction = Left;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Standing;
}
}
}
if (isWalking == Walking)
{
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
if (GregorySpriteRect.left == 0)
GregorySpriteRect.left = 400;
if ((int(walkClock.getElapsedTime().asSeconds() / 1.5f) % 2) == 1)
{
if (GregorySpriteRect.left == 400)
GregorySpriteRect.left == 800;
if (GregorySpriteRect.left == 800)
GregorySpriteRect.left == 400;
walkClock.restart();
}
}
{
using kb = sf::Keyboard;
if (kb::isKeyPressed(kb::Right))
{
Gregory.move(400 * timePerFrame.asSeconds(), 0.0f);
direction = Right;
isWalking = Walking;
}
if (kb::isKeyPressed(kb::Left))
{
Gregory.move(-400 * timePerFrame.asSeconds(), 0.0f);
direction = Left;
isWalking = Walking;
}
if (kb::isKeyPressed(kb::Right) && kb::isKeyPressed(kb::Left))
{
isWalking = Standing;
}
}
if (isWalking == Standing)
{
GregorySpriteRect.left = 0;
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
}
}
Gregory.setTextureRect(GregorySpriteRect);
renderWindow.clear();
renderWindow.draw(Gregory);
renderWindow.display();
}
/* END RENDER WINDOW LOOP */
}
这是一个建议,但还有很多替代方案:
首先,稍微重写您的事件处理,这样您就可以区分第一次按下键和随后按下键的时间。一种简单的方法是使用 enum State { Standing, Walking }
并将其设置为密钥处理程序中的适当值(例如 "not walking and key is held? set state to walking")
然后,当玩家开始行走时,(重新)启动一个 "walk" 时钟。
最后,当你实际渲染你的播放器时,检查这个时钟并将自行走以来的过期时间除以你的行走周期(这决定了一个行走框架应该保持多长时间)。如果有偶数个这样的行走周期,则使用左侧行走精灵,否则使用右侧行走精灵。
当然,这种技术很容易推广到更大的步行框架集。
所以对于将来来到这里的任何人,我错过了一条重要的台词:
Gregory.setTextureRect(GregorySpriteRect);
在我的 "if" 时钟声明中 "if (isWalking == Walking)" 下,或者在它下面(就像我在这里所做的那样)。粘贴下面的完整代码。现在适用于两个方向。不确定这有多优化,但如果你是像我这样的业余爱好者,这应该有所帮助。
另请注意,我已经恢复了 Botje 的回答中建议的一些更改。尽管我必须感谢他在建议使用枚举进行行走和站立以及方向时为我提供了很多帮助。由于浮点变量的舍入误差,我还使用了“>”运算符而不是教授建议的“==”运算符。
干杯!
#include "pch.h"
#include <iostream>
#include "SFML/Graphics.hpp"
#include <random>
#include <unordered_map>
enum State
{
Walking,
Standing
};
enum Direction
{
Left,
Right
};
int main(int argc, char ** argv)
{
/* Gregory */
sf::Texture GregorySpriteSheet_T;
GregorySpriteSheet_T.loadFromFile("Images/GregorySpriteSheet.png");
sf::IntRect GregorySpriteRect(0, 600, 400, 600);
sf::Sprite Gregory(GregorySpriteSheet_T, GregorySpriteRect);
sf::RenderWindow renderWindow(sf::VideoMode(1600,800), "SFML 2 Demo");
sf::Event event;
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Clock deltaClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
State isWalking{ Standing };
sf::Clock walkClock;
Direction direction = Right;
/* RENDER WINDOW LOOP */
while (renderWindow.isOpen())
{
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
renderWindow.close();
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Walking;
direction = Right;
GregorySpriteRect.top = 600;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Standing;
}
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Walking;
direction = Left;
GregorySpriteRect.top = 0;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Standing;
}
}
}
if (isWalking == Walking)
{
if (direction == Right)
GregorySpriteRect.top = 600;
else if (direction == Left)
GregorySpriteRect.top = 0;
else
GregorySpriteRect.top = 600;
if (GregorySpriteRect.left == 0)
GregorySpriteRect.left = 400;
if (walkClock.getElapsedTime().asSeconds() > 0.5f)
{
if (GregorySpriteRect.left == 400)
GregorySpriteRect.left = 800;
else if (GregorySpriteRect.left == 800)
GregorySpriteRect.left = 400;
else
GregorySpriteRect.left += 0;
walkClock.restart();
}
Gregory.setTextureRect(GregorySpriteRect);
}
{
using kb = sf::Keyboard;
if (kb::isKeyPressed(kb::Right))
{
Gregory.move(400 * timePerFrame.asSeconds(), 0.0f);
direction = Right;
}
if (kb::isKeyPressed(kb::Left))
{
Gregory.move(-400 * timePerFrame.asSeconds(), 0.0f);
direction = Left;
}
if (kb::isKeyPressed(kb::Right) && kb::isKeyPressed(kb::Left))
{
isWalking = Standing;
direction = Right;
}
}
if (isWalking == Standing)
{
GregorySpriteRect.left = 0;
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
}
}
Gregory.setTextureRect(GregorySpriteRect);
renderWindow.clear();
renderWindow.draw(Gregory);
renderWindow.display();
}
/* END RENDER WINDOW LOOP */
}