-
I am having some problems trying to show the moviment of my character after i have clicked 'A'.
This is all my code: https://github.com/vrige/sfml_1 (https://github.com/vrige/sfml_1).
This is the solution that i tried, but it didn't work:
chrono::milliseconds timespan(500);
/*
code
*/
else if (event.key.code == sf::Keyboard::A) {
cout << "tasto A" << endl;
if (!player.getIsMoved()) {
if (aStar.astar()) {
vector<char> path = aStar.getEasierToReadPath();
for (char iter : path) {
player.movePlayer(iter);
this_thread::sleep_for(timespan); //here
player.setIsMoved(false);
}
}
}
}
Sorry for my english.
-
What exactly do you mean by "didn't work"?
What is it actually doing and what do you expect it to do?
-
Now it shows that the character can go from the start to the goal, but i would like to show all the intermediate steps. I would like to show graphically the path chosen by AStar. So i have tried to make it sleeps in each steps, but it didn't work.
-
Does it move the entire movement immediate but you want it to travel in increments?
Try multiplying the movement by the time that has passed for that one frame/loop (in seconds):
It's possible, though, that you instead want to interpolate points. That is, given two points, you want to work out the points along the line between them.
You could use something like this (this code is from here (https://github.com/Hapaxia/Plinth/blob/master/Plinth/Tween.inl#L40-L48)):
template <class T>
T linear(const T& start, const T& end, const alphaT& alpha)
{
return static_cast<T>(start * (1 - alpha) + end * alpha);
}
-
I have found an old solution for my code.
https://en.sfml-dev.org/forums/index.php?topic=24242.0 (https://en.sfml-dev.org/forums/index.php?topic=24242.0) based on https://github.com/SFML/SFML-Game-Development-Book/blob/master/01_Intro/Source/Game.cpp (https://github.com/SFML/SFML-Game-Development-Book/blob/master/01_Intro/Source/Game.cpp)
Thank you for the help!
if (aStar.astar()) {
vector<char> path = aStar.getEasierToReadPath();
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
float frametime = 1.0f / 6.0f; //รจ un immagine al secondo
sf::Time frameTime = sf::seconds(frametime);
for (char iter : path) {
while(true){
sf::Time elapsedTime = clock.restart();
timeSinceLastUpdate += elapsedTime;
if (timeSinceLastUpdate > frameTime){
timeSinceLastUpdate -= frameTime;
player.movePlayer(iter);
view.setCenter(player.getPos().x * player.getPlayerSize().x, player.getPos().y * player.getPlayerSize().y);
player.setIsMoved(false);
window.setView(view);
window.clear(sf::Color::White);
window.draw(map);
window.draw(player);
window.display();
break;
}
}
}
}